Opencv2.4 Python SURF 匹配

來源:互聯網
上載者:User
opencv_haystack =cv2.imread('woman2.bmp')opencv_needle =cv2.imread('face.bmp')ngrey = cv2.cvtColor(opencv_needle, cv2.COLOR_BGR2GRAY)hgrey = cv2.cvtColor(opencv_haystack, cv2.COLOR_BGR2GRAY)# build feature detector and descriptor extractorhessian_threshold = 85detector = cv2.SURF(hessian_threshold)(hkeypoints, hdescriptors) = detector.detect(hgrey, None, useProvidedKeypoints = False)(nkeypoints, ndescriptors) = detector.detect(ngrey, None, useProvidedKeypoints = False)# extract vectors of size 64 from raw descriptors numpy arraysrowsize = len(hdescriptors) / len(hkeypoints)if rowsize > 1:    hrows = numpy.array(hdescriptors, dtype = numpy.float32).reshape((-1, rowsize))    nrows = numpy.array(ndescriptors, dtype = numpy.float32).reshape((-1, rowsize))    #print hrows.shape, nrows.shapeelse:    hrows = numpy.array(hdescriptors, dtype = numpy.float32)    nrows = numpy.array(ndescriptors, dtype = numpy.float32)    rowsize = len(hrows[0])# kNN training - learn mapping from hrow to hkeypoints indexsamples = hrowsresponses = numpy.arange(len(hkeypoints), dtype = numpy.float32)#print len(samples), len(responses)knn = cv2.KNearest()knn.train(samples,responses)# retrieve index and value through enumerationcount = 1for i, descriptor in enumerate(nrows):    descriptor = numpy.array(descriptor, dtype = numpy.float32).reshape((1, rowsize))    #print i, descriptor.shape, samples[0].shape    retval, results, neigh_resp, dists = knn.find_nearest(descriptor, 1)    res, dist =  int(results[0][0]), dists[0][0]    #print res, dist    if dist < 0.1:        count = count+1        # draw matched keypoints in red color        color = (0, 0, 255)#    else:#        # draw unmatched in blue color#        color = (255, 0, 0)    # draw matched key points on haystack image        x,y = hkeypoints[res].pt        center = (int(x),int(y))        cv2.circle(opencv_haystack,center,2,color,-1)        # draw matched key points on needle image        x,y = nkeypoints[i].pt        center = (int(x),int(y))        cv2.circle(opencv_needle,center,2,color,-1)cv.ShowImage("Input Image", opencv_haystack)cv.waitKey(0)cv.ShowImage("The match Result", opencv_needle)cv.waitKey(0)print countif count>40:    print "Yes Success!"else:    print "False Face!"#cv2.waitKey(0)#cv2.destroyAllWindows()

編譯環境Opencv2.4 Python2.7

這個大家注意就好了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.