【问题标题】:Detecting similar objects in an Image检测图像中的相似对象
【发布时间】:2019-06-22 09:44:22
【问题描述】:

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

【问题讨论】:

  • 您是否必须使用破旧的、有损 JPEG 图像,或者您是否可以让自己的生活更简单并使用无损 PNG 文件?

标签: python opencv


【解决方案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

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

【讨论】:

    猜你喜欢
    • 2017-10-09
    • 2016-08-10
    • 2020-05-04
    • 2014-10-03
    • 2011-04-16
    • 2015-03-20
    • 2012-06-05
    • 2019-07-13
    相关资源
    最近更新 更多