From 77e322e3f82439dea6d41fb5991ae5bcde6295e4 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Wed, 19 Oct 2016 15:24:57 +0000 Subject: [PATCH] - added min/max approach git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@325 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- lasertrack.py | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/lasertrack.py b/lasertrack.py index 9a15e03..9ab842b 100755 --- a/lasertrack.py +++ b/lasertrack.py @@ -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)