检测图像中的类似对象

问题描述 投票:2回答:1

我必须在项目的图像中检测具有相同区域的相同大小和相同颜色的矩形。这是一个例子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的图像结果

python opencv
1个回答
1
投票

这是一个简单的方法。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.