检测图像中的类似对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测图像中的类似对象相关的知识,希望对你有一定的参考价值。

我必须在项目的图像中检测具有相同区域的相同大小和相同颜色的矩形。这是一个例子image。我不知道该怎么做。我正在使用我不熟悉的OpenCV和python。

我尝试了SIFT和SURF特征描述符来获得类似的功能。我也尝试过模板匹配,但因为trainImage可能会改变,所以不可行。但主要的想法是从提供的图像中获取那些类似的矩形。我正在使用python3和openCV3。我从opencv教程网站上获取了这段代码。

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('template.jpg',0)          # queryImage
img2 = cv2.imread('input.jpg',0) # trainImage

sift=cv2.xfeatures2d.SIFT_create()

kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])

# cv2.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)

项目reslut的图像结果

答案

这是一个简单的方法。

generate a list of the unique colours in the image
for each unique colour
    make everything that colour in the image white and everything else black
    run findContours() and compare shapes and sizes
end for

为了增加乐趣,请在单独的线程中执行每种颜色:-)

以上是关于检测图像中的类似对象的主要内容,如果未能解决你的问题,请参考以下文章

检测图像 Cloud Vision 中的对象数量

如何检测图像中的形状?

✠OpenGL-2-图像管线

在 Python 多处理进程中运行较慢的 OpenCV 代码片段

如何使用模块化代码片段中的LeakCanary检测内存泄漏?

用C#检测摄像机图像中的对象[关闭]