I have an function to detect shapes in an image, this returns the shape name, from that I have an array of the returned shapes but was wondering how I could add a count to each shape detected?
So it would show:
rectangle 1
rectangle 2
rectangle 3
rectangle 4
and so on for each rectangle detected. The code I have at the moment is:
def detect(c):
# initialize the shape name and approximate the contour
shape = ""
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
# if the shape has 4 vertices, it is a rectangle
if 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)
#the shape is a rectangle
shape = "rectangle"
# otherwise the shape is a circle
else:
shape = "circle"
# return the name of the shape
return shape
# detect the shape
shape = detect(c)
#array of rectangles
rectangles = []
#add each rectangle found to the array 'rectangles'
if shape == 'rectangle':
rectangles.append(shape)