- added min/max approach

git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@325 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2016-10-19 15:24:57 +00:00
parent 7b083ccd14
commit 77e322e3f8
+28 -21
View File
@@ -89,30 +89,37 @@ class MyRgbProcess(RgbProcess):
data = self.read(1.0)
if data is not None:
gray = cv2.cvtColor(data,cv2.COLOR_BGR2GRAY)
# gray_blurred = cv2.medianBlur(gray,5)
gray_blurred = cv2.GaussianBlur(gray,(5,5),0)
ret,thresh = cv2.threshold(gray_blurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# thresh = cv2.adaptiveThreshold(gray_blurred,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
# thresh = cv2.adaptiveThreshold(gray_blurred,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
# Contours
(_, contours, hierachy) = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img1_contours = np.zeros((height,width,3), np.uint8)
img1_contours = cv2.drawContours(img1_contours, contours, -1, (0,255,0), 1)
img1_objects = data.copy()
for contour in contours:
x,y,w,h = cv2.boundingRect(contour)
roi = gray[y:y+h, x:x+w]
mean_color = cv2.mean(gray)
mean_color_roi = cv2.mean(roi)
if mean_color_roi[0] > 2*mean_color[0]:
print (mean_color)
print (mean_color_roi)
img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(0,0,255),2)
pass
if 1:
# Min / max approach
gray_blurred = cv2.medianBlur(gray,5)
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(gray_blurred)
x = maxLoc[0]
y = maxLoc[1]
if maxVal > 100*minVal:
img1_objects = cv2.rectangle(img1_objects,(x-5,y-5),(x+5,y+5),(0,0,255),2)
print(maxLoc)
else:
# Contour approach
gray_blurred = cv2.GaussianBlur(gray,(5,5),0)
ret,thresh = cv2.threshold(gray_blurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Contours
(_, contours, hierachy) = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img1_contours = np.zeros((height,width,3), np.uint8)
img1_contours = cv2.drawContours(img1_contours, contours, -1, (0,255,0), 1)
for contour in contours:
x,y,w,h = cv2.boundingRect(contour)
roi = gray[y:y+h, x:x+w]
mean_color = cv2.mean(gray)
mean_color_roi = cv2.mean(roi)
if mean_color_roi[0] > 2*mean_color[0]:
print (mean_color)
print (mean_color_roi)
img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('Contours', img1_contours)
cv2.imshow('Contours', img1_contours)
cv2.imshow('Objects', img1_objects)
cv2.waitKey(1)