OpenCV:查找轮廓匹配的相似对象

时间:2020-03-09 20:38:05

标签: python opencv image-processing

我有一堆带有鸟类的不同图像,但是其中一些包含羽毛,稍微可见的鸟类等。我需要找到鸟类可见度良好的图像,并去除带有羽毛,远距离鸟类的图像等。

我已经尝试过ORB,简单模板匹配,Canny边缘检测。而且我不能使用神经网络。

现在我尝试使用这种算法:

  1. 对模板图像进行二值化处理以获取形状

  2. 在具有滑动窗口的另一个二值化图像上滑动窗口,并在每个窗口中使用模板计算matchShape

  3. 找到最佳匹配项

如您所见,这种方法给我带来奇怪的结果

二进制模板

img

在另一个二进制图像上的形状,例如:

img

我在这张图片的不同部分计算了matchShapes,得到了最好的结果〜0.05我在这一部分得到了

jpg

这显然不同于原始形状。

滑动窗口的代码:

import cv2

OFFSET = 5
SCALE_RATIO = [0.5, 1]

def get_scaled_list(img_path, template):
    matcher_list = []

    img = cv2.imread(img_path)
    #JUST BINARIZATION AND RESIZING
    img = preprocess(resize_image(img))

    height, width = img.shape

    # building size of scale window
    for scaler in SCALE_RATIO:
        x_point = 0
        y_point = 0

        x1_point = int(width * scaler)
        y1_point = x1_point

        if x1_point > height:
            y1_point = height

        while y1_point <= height:
            while x1_point <= width:
                img1 = img[y_point:y1_point, x_point:x1_point]

                #Comparing template and part of image
                diff = cv2.matchShapes(template, img1, cv2.CONTOURS_MATCH_I1, 0)

                data_tuple = (img_path, x_point, y_point, int(width * scaler), diff)
                matcher_list.append(data_tuple)

                x_point += OFFSET
                x1_point += OFFSET

            x_point = 0
            x1_point = int(width * scaler)

            y_point += OFFSET
            y1_point += OFFSET
    return matcher_list

如何进行正确的形状匹配,为什么在这里能获得最佳结果?

1 个答案:

答案 0 :(得分:0)

具有刚性模板的幼稚窗口滑动方法将非常糟糕。尤其是大小不同,无法正确重叠。


您要实现的目标很困难,因为您只有边缘信息,并且边缘很复杂,在几个独立的弧中并带有连接点,因此会断裂。

只有一条闭合曲线(例如,查找“弹性轮廓匹配”)时,您可以找到许多解决方案,但不适用于您的情况。这就是“近似弹性图匹配”的情况。

其他可能的方法是使用特殊的距离功能,例如倒角或Hausdorff距离,但是由于尺寸不匹配,您仍然会被卡住。

相关问题