How do I use OpenCV MatchTemplate?

前端 未结 2 1526
野性不改
野性不改 2020-12-06 01:55

I\'m attempting to find an image in another.

im = cv.LoadImage(\'1.png\', cv.CV_LOAD_IMAGE_UNCHANGED)
    tmp = cv.LoadImage(\'e1.png\', cv.CV_LOAD_IMAGE_UN         


        
2条回答
  •  囚心锁ツ
    2020-12-06 02:19

    This might work for you! :)

    def FindSubImage(im1, im2):
        needle = cv2.imread(im1)
        haystack = cv2.imread(im2)
    
        result = cv2.matchTemplate(needle,haystack,cv2.TM_CCOEFF_NORMED)
        y,x = np.unravel_index(result.argmax(), result.shape)
        return x,y
    

    CCOEFF_NORMED is just one of many comparison methoeds. See: http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html for full list.

    Not sure if this is the best method, but is fast, and works just fine for me! :)

提交回复
热议问题