gpt4 book ai didi

python - 使用 opencv python 进行 SURF Logo 检测

转载 作者:行者123 更新时间:2023-12-02 17:14:25 36 4
gpt4 key购买 nike

我正在尝试检测图片中已知软件相关事物的一些 Logo 。
我将 Opencv 2.4.5 与 python 2.7 一起使用。
我想使用在 opencv 中实现的 SURF 检测器,但问题是我没有获得好的结果。有很多假阴性和假阳性。
我的代码是:

import cv2
import numpy as np

def detectLogo(template, img):

templateg = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
imgg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# SURF extraction
hessian_threshold = 30
surf = cv2.SURF(hessian_threshold)
kp, desc = surf.detect(imgg, None, useProvidedKeypoints = False)

# KNN
samples = np.array(desc)
responses = np.arange(len(kp), dtype = np.float32)
knn = cv2.KNearest()
knn.train(samples, responses)

# Loading template and searching for similar kp
kp2, desc2 = surf.detect(templateg, None, useProvidedKeypoints = False)

matched = 0
total = 0
for h,des in enumerate(desc2):
des = np.array(des,np.float32).reshape((1,128))
retval, results, neigh_resp, dists = knn.find_nearest(des,1)
res,dist = int(results[0][0]),dists[0][0]
total += 1
if dist<0.1: # draw matched keypoints in red color
color = (0,0,255)
matched += 1
else:
color = (255,0,0)
#Draw matched key points on original image
x,y = kp[res].pt
center = (int(x),int(y))
cv2.circle(img,center,2,color,-1)

#Draw matched key points on template image
x,y = kp2[h].pt
center = (int(x),int(y))
cv2.circle(template,center,2,color,-1)

cv2.imwrite("../resources/template.jpg", template)
cv2.imwrite("../resources/image.jpg", img)
return matched / float(total)

template = cv2.imread("../resources/pictures/appleLogo.jpg")
img = cv2.imread("../resources/pictures/pic2.jpg")
print detectLogo(template, img)
以下是结果。

(来源: hostingpics.net)
Template url .

(来源: hostingpics.net)
Image url .
匹配点与 loho 完全不对应,对于两个完全不同的图像,我得到了相同的结果。
我认为这是执行此任务的唯一解决方案,但是此检测的问题在哪里?先感谢您。
亚历山大

最佳答案

沿边缘的兴趣点被抑制,因为它们在尺度空间中不是不变的。由于您的 Logo 完全由边缘定义,因此像 SURF(或 SIFT)一样工作的测试点检测器不会很好地工作。可能训练一个 Haar 分类器,比如 OpenCV 中的 Viola-Jones 分类器,效果会更好。

关于python - 使用 opencv python 进行 SURF Logo 检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17105513/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com