如何匹配图像中检测到的两个矩形

如何匹配图像中检测到的两个矩形

本文介绍了如何匹配图像中检测到的两个矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于深度学习,由两个检测器计算从许多图像中检测到的矩形的比率.例如,如下图所示,首先,第一个检测器检测到大圆圈(c1和c2),第二个检测器检测到小圆圈(d1和d2).现在,第一个检测器返回c1和c2的坐标,第二个检测器还返回d1和d2的坐标.

I want to calculate the ratio of detected rectangle from many images by two detector based on deep learning. for example with below picture, firstly, the first detector detects big circles (c1 and c2), and the second detector detects small circles (d1 and d2).Now, first detector returns the coordinates of c1 and c2, second detector also those of d1 and d2.

所以我想制作可以自动计算d1/c1和d2/c2比率的函数和代码.因此,我的想法是,如果在相应的大对象(例如c1,c2)中包含小对象(例如d1,d2)的坐标,则它们会匹配以进行比率的计算.

So I want to make the function and code that can calculate the ratio d1/ c1 and d2 / c2 automatically. so, my idea is that if coordinate of small one(e.g. d1, d2) is included in corresponding big one (e.g. c1, c2), they are matched for calculation of ratio.

c1坐标(xmin,ymin,xmax,ymax):(10,10,30,30)d1 coorinate:(13,15,20,23)

c1 coordinate (xmin, ymin, xmax, ymax) : (10, 10, 30, 30)d1 coorinate : (13, 15, 20, 23)

c2坐标:(20,20,40,40)d2坐标:(25,24,32,33)

c2 coordinate : (20, 20, 40, 40)d2 coordinate : (25, 24, 32, 33)

但是,我不知道OpenCV和python的许多功能.您能否编写一些代码,函数或推荐库?

But, I don't know many function of OpenCV and python.Could you please make some code, function or recommend library?

谢谢

推荐答案

通常用于此任务的度量标准是联合上的交集".即借条.

The metric which is generally used for this task is "Intersection over Union" i.e. IOU.

使用格式为 [50、60、200、150] 的包围盒(矩形)输入,您可以像这样为它编写自定义函数-

With inputs for bounding box (rectangle) in this format [50, 60, 200, 150], you can write a custom function for it like this -

def intersection_over_union(box1, box2):
    # Get coordinates of the intersection
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    x2 = min(box1[2], box2[2])
    y2 = min(box1[3], box2[3])

    # Get the area of intersection rectangle
    intersection = max(0, x2 - x1 + 1) * max(0, y2 - y1 + 1)

    # Get the area of both rectangles
    box1Area = (box1[2] - box1[0] + 1) * (box1[3] - box1[1] + 1)
    box2Area = (box2[2] - box2[0] + 1) * (box2[3] - box2[1] + 1)

    iou = intersection / float(box1Area + box2Area - intersection)

    return iou

请注意,这需要在以下条件正确的情况下以标准格式返回边界框:

Note that this needs the bounding boxes are returned in the standard format where following conditions are correct:

assert box1['x1'] <= box1['x2']
assert box1['y1'] <= box1['y2']
assert box2['x1'] <= box2['x2']
assert box2['y1'] <= box2['y2']

answer中的更多详细信息.

如果边界框对不满足该条件,则最好使用sklearn中的jaccard_score(IOU的另一个名称)

If bounding box pair does not hold that condition, its best to use jaccard_score (another name for IOU) from sklearn

from sklearn.metrics import jaccard_score
import numpy as np

box1 = [180, 400, 450, 450]
box2 = [200, 450, 425, 425]

img = np.zeros((800, 800, 3), np.uint8)  # use your image shape here or directly below

img1 = cv2.rectangle(np.zeros(img.shape), (box1[0], box1[1]), (box1[2], box1[3]), (1, 1, 1), -1)
img2 = cv2.rectangle(np.zeros(img.shape), (box2[0], box2[1]), (box2[2], box2[3]), (1, 1, 1), -1)

jaccard_score(img1.ravel(),img2.ravel())

这篇关于如何匹配图像中检测到的两个矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:40