Finding a subimage inside a Numpy image

后端 未结 5 1130
太阳男子
太阳男子 2020-12-08 03:24

I have two Numpy arrays (3-dimensional uint8) converted from PIL images.

I want to find if the first image contains the second image, and if so, find out the coordin

5条回答
  •  离开以前
    2020-12-08 03:35

    import cv2
    import numpy as np
    
    img = cv2.imread("brows.PNG")              #main image
    gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    template = cv2.imread("websearch.PNG", cv2.IMREAD_GRAYSCALE)      #subimage
    w,h = template.shape[::-1]
    
    result = cv2.matchTemplate(gray_img,template, cv2.TM_CCOEFF_NORMED)
    loc = np.where(result >= 0.9)
    
    for pt in zip(*loc[::-1]):
        cv2.rectangle(img, pt,(pt[0] + w,pt[1] +h), (0,255,0),3)
    
    cv2.imshow("img",img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

提交回复
热议问题