From 21056ba966f8c81af81654a57c960d202867bd87 Mon Sep 17 00:00:00 2001 From: Rui Jie Li <rui-jie.li@polymtl.ca> Date: Sun, 25 Oct 2020 18:01:00 -0400 Subject: [PATCH] WIP fixed a problem with importlib + made the code for area detection --- .../find_contour/blobAreaDetector.py | 31 ++++++++++++++----- .../find_contour/blobEdgeDetector.py | 7 ++--- .../find_contour/greyBlobDetector.py | 17 +++++----- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/robots-at-robots/research/robots_at_robots/armor_color/find_contour/blobAreaDetector.py b/robots-at-robots/research/robots_at_robots/armor_color/find_contour/blobAreaDetector.py index 8884c8e..0882997 100644 --- a/robots-at-robots/research/robots_at_robots/armor_color/find_contour/blobAreaDetector.py +++ b/robots-at-robots/research/robots_at_robots/armor_color/find_contour/blobAreaDetector.py @@ -2,11 +2,11 @@ import numpy as np import cv2 as cv import importlib -Colors = importlib.import_module(".greyBlobDetector", "Colors") -GreyBlobDetector = importlib.import_module(".greyBlobDetector", "GreyBlobDetector") +Colors = importlib.import_module("greyBlobDetector", "Colors").Colors +GreyBlobDetector = importlib.import_module("greyBlobDetector", "GreyBlobDetector").GreyBlobDetector -class BlobAreaHSVDetector: +class ContourAreaDetector: # HSV value for red RED_THRESHOLD = 8 @@ -22,14 +22,29 @@ class BlobAreaHSVDetector: # gaussian blur is basically there to reduce the noise if gaussian_blur: # apply gaussian blur here - pass + image = cv.GaussianBlur(image,(5,5),0) - blobs = GreyBlobDetector.detect_color(image) + gd = GreyBlobDetector() + contours = gd.detect_color(image) - if blobs == None: + if contours == None: return Colors.GREY else: # since the picture with the blobs is black and white, it can act as a mask # we also know that its either red or blue, so we can just check whether its colser to red - # or to blue - pass \ No newline at end of file + # use the area since it would lessen the intergerance of neibouring pixels + mask = np.zeros(image.shape[0], image.shape[1], 1) # mask m x n x 1 + # mask with all the points + cv.drawContours(mask, contours, 0, (255), cv.FILLED) + # applies the mask to image + image[mask != 0] = (0,0,0) + # transform this to hsv + hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV) + # note : 0,0,0 will still be 0,0,0 + colors = hsv[:,:,0] + average = colors.sum() / (colors != 0).sum() + if abs(average - ContourAreaDetector.RED_THRESHOLD) \ + < abs(average - ContourAreaDetector.BLUE_THRESHOLD): + return Colors.RED + else: + return Colors.BLUE diff --git a/robots-at-robots/research/robots_at_robots/armor_color/find_contour/blobEdgeDetector.py b/robots-at-robots/research/robots_at_robots/armor_color/find_contour/blobEdgeDetector.py index 3894a35..afc0cef 100644 --- a/robots-at-robots/research/robots_at_robots/armor_color/find_contour/blobEdgeDetector.py +++ b/robots-at-robots/research/robots_at_robots/armor_color/find_contour/blobEdgeDetector.py @@ -2,9 +2,8 @@ import numpy as np import cv2 as cv import importlib -Colors = importlib.import_module(".greyBlobDetector", "Colors") -GreyBlobDetector = importlib.import_module(".greyBlobDetector", "GreyBlobDetector") - +Colors = importlib.import_module("greyBlobDetector", "Colors").Colors +GreyBlobDetector = importlib.import_module("greyBlobDetector", "GreyBlobDetector").GreyBlobDetector class ContourEdgeDetector: # HSV value for RED @@ -23,7 +22,7 @@ class ContourEdgeDetector: gd = GreyBlobDetector() contours = gd.detect_color(image) - if contours == None: + if len(contours) == 0: return Colors.GREY else: # we know that its either blue or red diff --git a/robots-at-robots/research/robots_at_robots/armor_color/find_contour/greyBlobDetector.py b/robots-at-robots/research/robots_at_robots/armor_color/find_contour/greyBlobDetector.py index 6fd5b46..9d5b282 100644 --- a/robots-at-robots/research/robots_at_robots/armor_color/find_contour/greyBlobDetector.py +++ b/robots-at-robots/research/robots_at_robots/armor_color/find_contour/greyBlobDetector.py @@ -24,7 +24,6 @@ class GreyBlobDetector: INERTIA_THRESHOLD = 0.2 # unless a robot got flipped, the lines should be pretty vertical ANGLE_THRESHOLD = 30 - # these parameters are for the shape of the image, not sure if needed or not WIDTH_RATIO = 0.2 # width LED / width image, TBD HEIGHT_RATIO = 0.67 # height LED / height image, TBD @@ -67,24 +66,22 @@ class GreyBlobDetector: retained_contours = [] for contour in contours: area = cv.contourArea(contour) - circularity = 4*PI*area / (len(contour) * len(contour)) # there gotta be a better way to write this ... + # circularity < threshold : is the contour NOT circular? + circularity = 4*PI*area / (len(contour) * len(contour)) + # convexity > threashold : we want the shape to be convex convexity = area / cv.contourArea(cv.convexHull(contour)) # opencv docs aren't very helpful on this but looks like inertia is aspect ratio (x,y),(width,height),theta = cv.minAreaRect(contour) + # this is basically how e inertia = width / height - + if circularity < GreyBlobDetector.CIRCLE_THRESHOLD and \ convexity > GreyBlobDetector.CONVEX_THRESHOLD and \ inertia < GreyBlobDetector.INERTIA_THRESHOLD and \ theta < GreyBlobDetector.ANGLE_THRESHOLD: retained_contours.append(contour) - if len(retained_contours) == 0: - # no contour should be retained --> all LEDs are off - # the other detectors will return Grey - return None - else: - # these are the contours that supposedly represents the LEDs - return retained_contours + + return retained_contours if __name__ == '__main__': image = cv.imread("test.png") -- GitLab