图像处理如何检测图像中的特定自定义形状

时间:2020-03-14 02:16:08

标签: image opencv image-processing computer-vision object-detection

我想检测一种特定的形状,该形状是五角形和正方形两种形状的组合。但是,正方形的边应为五边形的3倍左右,以匹配图中所示的有效形状。

enter image description here

我正在根据自己的需求学习不同的图像处理技术。

  1. 轮廓检测:问题是仅仅知道7个角点是不够的,因为x:需要验证3个。

  2. Haar功能:问题是图像由背景组成,并且这些对象中包含文本。因此,我认为haar并不是最佳方法。

我的想法

我想可能是我可以检测到线角并使用边长可以做一些数学运算并识别图像。是否有任何使用数学距离,比例尺和位置来识别物体的图像处理技术?

原始图片

匹配

enter image description here

不匹配

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一种简单的方法:

  1. 获取二进制图像。加载图像,先转换为灰度,先Gaussian blur,然后再转换为Otsu's threshold

  2. 过滤所需轮廓。然后,我们find contours使用contour approximation + contour area的组合进行过滤。观察到形状具有较大的面积差异,我们可以使用预定的阈值区域在两者之间进行识别。


输入->二进制图像->检测到的形状

结果

Match

输入->二进制图像->检测到的形状

结果

No Match

由于您没有指定语言,所以我用Python实现了

import cv2
import numpy as np

# Load image, grayscale, Gaussian blur, Otsus threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.04 * peri, True)
    area = cv2.contourArea(c)
    # Filter using contour approximation and area filtering (Remove small noise)
    if len(approx) > 4 and len(approx) < 8 and area > 200:
        # This is the larger version
        if area >= 5500:
            print('Match')
        # Smaller version
        elif area < 5500:
            print('No Match')
        cv2.drawContours(image, [c], -1, (255,0,12), 3)

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()