Last active
October 19, 2016 09:57
-
-
Save a0726h77/f958138f0ecaa992f3d52df15a60fbc5 to your computer and use it in GitHub Desktop.
bad_rice_tracking.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import cv2 | |
import numpy as np | |
READ_PICTURE = 1 # 0 => read from camera | |
PICTURE_PATH = "sample.jpg" | |
THRESHOLD = 180 | |
MIN_AREA = 60 | |
MAX_AREA = 800 | |
BLACK_BLANK = 0 | |
SAVE_IMAGE = 1 # Don't use in camera mode | |
img = None | |
if READ_PICTURE: | |
img = cv2.imread(PICTURE_PATH) | |
black_blank = np.zeros((768, 1024, 3), np.uint8) # draw contours on black blank | |
else: | |
# capturing video through webcam | |
cap = cv2.VideoCapture(0) | |
# definig the range of black color | |
black_lower = np.array([0, 0, 0], np.uint8) | |
black_upper = np.array([1, 1, 1], np.uint8) | |
while(1): | |
min_area = 0 | |
max_area = 0 | |
total_contours = 0 | |
if not READ_PICTURE: | |
# read webcam | |
_, img = cap.read() | |
black_blank = np.zeros((768, 1024, 3), np.uint8) # (768, 1024, 3) | |
# reduce to two color through a threshold | |
ret, thresh = cv2.threshold(img, THRESHOLD, 255, cv2.THRESH_BINARY) | |
if SAVE_IMAGE: | |
cv2.imwrite('bad_rice_thresh.png', thresh) | |
# finding the range of black color int the image | |
black = cv2.inRange(thresh, black_lower, black_upper) | |
kernal = np.ones((5, 5), "uint8") | |
black = cv2.dilate(black, kernal) | |
res = cv2.bitwise_and(img, img, mask=black) | |
(contours, _) = cv2.findContours(black, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
for pic, contour in enumerate(contours): | |
area = int(cv2.contourArea(contour)) | |
print "area: %d" % area | |
if min_area == 0 and max_area == 0 and area >= MIN_AREA and area <= MAX_AREA: | |
min_area = area | |
max_area = area | |
if min_area != 0 and max_area != 0 and area >= MIN_AREA and area < min_area: | |
min_area = area | |
if min_area != 0 and max_area != 0 and area <= MAX_AREA and area > max_area: | |
max_area = area | |
if area >= MIN_AREA and area <= MAX_AREA: | |
x, y, w, h = cv2.boundingRect(contour) | |
# draw contour | |
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) | |
cv2.rectangle(black_blank, (x, y), (x + w, y + h), (0, 255, 0), 2) | |
total_contours = total_contours + 1 | |
print "min area: %d, max area: %d" % (min_area, max_area) | |
print "total contours: %d" % total_contours | |
# show image | |
if BLACK_BLANK: | |
cv2.imshow("", black_blank) # for projector | |
else: | |
cv2.imshow("", img) # for debug | |
# save image | |
if SAVE_IMAGE: | |
cv2.imwrite('bad_rice.png', img) | |
cv2.imwrite('bad_rice_black.png', black_blank) | |
if cv2.waitKey(10) & 0xFF == ord('q'): | |
if not READ_PICTURE: | |
cap.release() | |
cv2.destroyAllWindows() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment