- 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:
+28
-21
@@ -89,30 +89,37 @@ class MyRgbProcess(RgbProcess):
|
|||||||
data = self.read(1.0)
|
data = self.read(1.0)
|
||||||
if data is not None:
|
if data is not None:
|
||||||
gray = cv2.cvtColor(data,cv2.COLOR_BGR2GRAY)
|
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()
|
img1_objects = data.copy()
|
||||||
for contour in contours:
|
if 1:
|
||||||
x,y,w,h = cv2.boundingRect(contour)
|
# Min / max approach
|
||||||
roi = gray[y:y+h, x:x+w]
|
gray_blurred = cv2.medianBlur(gray,5)
|
||||||
mean_color = cv2.mean(gray)
|
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(gray_blurred)
|
||||||
mean_color_roi = cv2.mean(roi)
|
x = maxLoc[0]
|
||||||
if mean_color_roi[0] > 2*mean_color[0]:
|
y = maxLoc[1]
|
||||||
print (mean_color)
|
if maxVal > 100*minVal:
|
||||||
print (mean_color_roi)
|
img1_objects = cv2.rectangle(img1_objects,(x-5,y-5),(x+5,y+5),(0,0,255),2)
|
||||||
img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(0,0,255),2)
|
print(maxLoc)
|
||||||
pass
|
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.imshow('Objects', img1_objects)
|
||||||
cv2.waitKey(1)
|
cv2.waitKey(1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user