123456789101112131415161718192021222324252627 |
- import cv2
- from scipy.spatial import distance
- general_count = 0
- for i in range(1, 13):
- img = cv2.imread(f"./res/img ({i}).jpg")
- image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- _, thresh = cv2.threshold(image, 120, 255, 0)
- contours, _ = cv2.findContours(
- thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
- )
- p_count = 0
- for cnt in contours:
- x, y, w, h = cv2.boundingRect(cnt)
- points = cv2.boxPoints(cv2.minAreaRect(cnt))
- w_euclid = distance.euclidean(points[0], points[1])
- h_euclid = distance.euclidean(points[0], points[3])
- if (h_euclid > 3 * w_euclid and h_euclid > 900) or (
- w_euclid > 3 * h_euclid and w_euclid > 900
- ):
- p_count += 1
- general_count += p_count
- print(f"Img {i}, pencils count: {p_count}")
- print(f"Pencils total: {general_count}")
|