Use CV find_template to search for small graphs in large graphs

import aircv as ac
from PIL import Image


class CompareImage() :
    Confidencevalue can be used to adjust the threshold of similarity. If the threshold is smaller than the threshold, the similarity is not similar
    def matchImg(self, imgsrc, imgobj, phone_x, phone_y, confidencevalue=0) :  # imgsrc= original image, imgobj= Image to be found
        imsrc = ac.imread(imgsrc)
        imobj = ac.imread(imgobj)
        match_result = ac.find_template(imsrc, imobj, confidencevalue)
        print(match_result)
        if match_result is not None:
            match_result['shape'] = (imsrc.shape[1], imsrc.shape[0])  # 0 is high, 1 is wide
            x, y = match_result['result']  # Position x,y in standard map
            shape_x, shape_y = tuple(map(int, match_result['shape']))  # X,y in standard graph
            position_x, position_y = int(phone_x * (x / shape_x)), int(phone_y * (y / shape_y))
        else:
            return None.None.None.None
        # print(match_result)
        # return match_result
        return position_x, position_y, str(match_result['confidence'[]) :4], match_result

    def fixed_size(self, width, height, infile, outfile) :
        "" fixed size image processing ""
        im = Image.open(infile)
        out = im.resize((width, height), Image.ANTIALIAS)
        out.save(outfile)

    def get_picture_size(self, imgsrc) :
        Gets the length and width of the image.
        imsrc = ac.imread(imgsrc)
        y, x, z = imsrc.shape
        return x, y

result = CompareImage().matchImg("333333.jpg"."222222.png".10.10)
zuobiao = result[3] ["rectangle"]
xmin = zuobiao[0] [0]
ymin = zuobiao[0] [1]
xmax = zuobiao[2] [0]
ymax = zuobiao[3] [1]

# Draw similar areas on the original image
import cv2
image = cv2.imread('333333.jpg')
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0.0.255), 2)
cv2.imwrite('2.jpg', image)
Copy the code

Pictures of the tests used are as follows:

  • Images to be tested

  • The original image

The results of the search are shown below