如何比较目标检测的2个列表

2024-09-30 16:41:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用opencv matchTemplate创建了一个函数,用于检测屏幕上的对象,并将其中心位置返回为(x,y)。 我想比较在两个不同对象上运行相同函数的结果,以检测一个对象相对于另一个对象的位置。在我的例子中,这两个对象是一个玩家化身,我想知道一些灌木丛是否是当前站在灌木丛附近的玩家。屏幕上只有一个用户化身,因此它只返回单个(x,y)值,但有多个灌木丛,因此有多个(x,y)值。我想要一种比较这两个矩阵的方法。对于代码格式,很抱歉

def center(base_img, needle_img, th, color):
result = cv.matchTemplate(BASE_IMG, needle_img, cv.TM_CCOEFF_NORMED)
threshold = th
yloc, xloc = np.where(result >= threshold)
w = needle_img.shape[1]
h = needle_img.shape[0]
rectangles = []
for (x, y) in zip(xloc, yloc):
    rectangles.append([int(x), int(y), int(w), int(h)])
    rectangles.append([int(x), int(y), int(w), int(h)])
rectangles, weights = cv.groupRectangles(rectangles, 1, 0.4)
points = []
for (x, y, w, h) in rectangles:
    certre_x = x + int(w / 2)
    certre_y = y + int(h / 2)
    cv.drawMarker(base_img, (certre_x, certre_y), color, cv.MARKER_DIAMOND)
    points.append((certre_x, certre_y))
    # cv.imshow("result", base_img)
    # print(points)
    # return points

Tags: 对象函数imgbase屏幕resultcvpoints
1条回答
网友
1楼 · 发布于 2024-09-30 16:41:46

您可以循环穿过灌木的中心,获得到每个灌木的距离sqrt((x2-x1)**2+(y2-y1)**2),也可以使用最近邻和numpy

如果代码片段有效,请发布它

相关问题 更多 >