【发布时间】:2018-02-12 19:20:18
【问题描述】:
我有一个检测图像中形状的函数,它返回形状名称,从中我有一个返回形状的数组,但想知道如何为检测到的每个形状添加计数?
所以它会显示:
rectangle 1
rectangle 2
rectangle 3
rectangle 4
对于检测到的每个矩形,依此类推。 我现在的代码是:
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)
【问题讨论】:
-
看枚举
-
你为什么需要给对象添加一个计数,只需迭代你的数组并用你的数组索引+1打印它的形状
-
我想给每个对象贴上标签,这样它们就可以在图像上被唯一标识
标签: python python-3.x counter