码迷,mamicode.com
首页 > 编程语言 > 详细

飘逸的python - 使用图像匹配SIFT算法进行LOGO检测

时间:2014-11-24 19:15:19      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:python   图像匹配   logo识别   

先上效果图.

bubuko.com,布布扣

其中bubuko.com,布布扣是logo标识,

bubuko.com,布布扣是待检测图片.


代码如下.

#coding=utf-8
import cv2
import scipy as sp

img1 = cv2.imread('x1.jpg',0) # queryImage
img2 = cv2.imread('x2.jpg',0) # trainImage

# Initiate SIFT detector
sift = cv2.SIFT()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)   # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)

print 'matches...',len(matches)
# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append(m)
print 'good',len(good)
# #####################################
# visualization
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
view = sp.zeros((max(h1, h2), w1 + w2, 3), sp.uint8)
view[:h1, :w1, 0] = img1
view[:h2, w1:, 0] = img2
view[:, :, 1] = view[:, :, 0]
view[:, :, 2] = view[:, :, 0]

for m in good:
    # draw the keypoints
    # print m.queryIdx, m.trainIdx, m.distance
    color = tuple([sp.random.randint(0, 255) for _ in xrange(3)])
    #print 'kp1,kp2',kp1,kp2
    cv2.line(view, (int(kp1[m.queryIdx].pt[0]), int(kp1[m.queryIdx].pt[1])) , (int(kp2[m.trainIdx].pt[0] + w1), int(kp2[m.trainIdx].pt[1])), color)

cv2.imshow("view", view)
cv2.waitKey()


飘逸的python - 使用图像匹配SIFT算法进行LOGO检测

标签:python   图像匹配   logo识别   

原文地址:http://blog.csdn.net/handsomekang/article/details/41448697

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!