So here is my problem, I need to be able to identify and count shapes for a project and am having one small difficulty. I have just started messing around with OpenCV and have not been able to find a better method on how to identify circles without just using an else command. For example, if there is anything else in the frame that is not one clearly one of the other shapes it will detect it as a circle (and ideally I would like to be able to ignore those objects in the frame.) I am not sure if this affects anything but before this portion of the code, I am using adaptive thresholding and gaussian blur to assist with better processing ability.
TLDR: I am trying to find a way to identify circles without using an else statement.
def detect(self, c):
# initialize the shape name and approximate the contour
shape = "unidentified"
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
# if the shape is a triangle, it will have 3 vertices
if len(approx) == 3:
shape = "triangle"
self.s2 = self.s2 + 1
# if the shape has 4 vertices, it is either a square or
# a rectangle
elif len(approx) == 4:
# compute the bounding box of the contour and use the
# bounding box to compute the aspect ratio
(x, y, w, h) = cv2.boundingRect(approx)
ar = w / float(h)
# a square will have an aspect ratio that is approximately
# equal to one, otherwise, the shape is a rectangle
if 0.75 <= ar <= 1.35:
shape = "square"
self.s3 = self.s3 + 1
else:
shape = "rectangle"
self.s4 = self.s4 + 1
# otherwise, we assume the shape is a circle
else:
shape = "circle"
self.s5 += 1