1
头图

When I analyzed the aircv source code before, there were still many dissatisfactions, and it was not updated and maintained, and no one was bothered to submit PR, so I just made a new project. I have submitted the PyPi library, which can be installed with the following name:

pip install findimage

findimage is not an image search, you can't find the similar one from a bunch of pictures, but locate the position of the small image of the given template from a large image.

Features

Compared with the original aircv, findimage has the following improvements:

  • Supports direct incoming grayscale images (although when calling opencv in the function, it is done using grayscale images, but the original aircv project requires that the incoming image must contain three channels of bgr, otherwise an error will be reported)
  • Support images with transparent background
  • Optimized the performance of the find_all_template method, replacing the floodFill method with numpy's slice assignment to avoid overlapping, which will probably shorten the overall search time by 1/4

Example

For example, we take a screenshot of the menu of the "Si No" course as follows:
思否课程菜单-标准

We want to find the position of # from it, we can provide a small template map:
思否课程菜单-标准

Then call the find_template method:

from cv2 import cv2
from findimage import find_template

image_origin = cv2.imread('seg_course_whole_page.png')
image_template = cv2.imread('seg_sharp.png')

match_result = find_template(image_origin, image_template)

The obtained match_result identifies the position of the center point of the first # in the source image, the coordinates of the four corners of the rectangular area and the degree of matching.

{
    "result": (x,y),        #tuple,表示识别结果的中心点
    "rectangle":[            #二位数组,表示识别结果的矩形四个角
        [left, top],
        [left, bottom],
        [right, top],
        [right, bottom]
    ],
    "confidence": percentage   #识别结果的匹配度,在-1~1之间,越大匹配度越高, 如果为1,表示按像素严格匹配
}

We can use this result to identify matching locations on the source image:

img_result = image_origin.copy()
rect = match_result['rectangle']
cv2.rectangle(img_result, (rect[0][0], rect[0][1]), (rect[3][0], rect[3][1]), (0, 0, 220), 2)
cv2.imwrite('result.png', img_result)

The result is shown below:
find_template匹配结果


songofhawk
303 声望24 粉丝