gpt4 book ai didi

python - 如何给数组中的每一项编号?

转载 作者:行者123 更新时间:2023-11-28 22:20:44 25 4
gpt4 key购买 nike

<分区>

我有一个检测图像中形状的函数,它返回形状名称,从中我有一个返回形状的数组,但想知道如何为检测到的每个形状添加一个计数?

所以它会显示:

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)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com