之前剖析aircv源码的时候,还是有诸多不满,而且它也不更新保护了,提交PR也没人理,罗唆做个新我的项目,目前曾经提交PyPi库,能够用上面的命名装置:
<code class="console">pip install findimage
findimage不是图像搜寻,不能从一堆图片中找到类似的那张,而是从一张大图中定位给定模板小图的地位。
性能
和原aircv相比,findimage有以下这些改良:
- 反对间接传入灰度图(尽管函数内调用opencv的时候,都是应用灰度图实现的,原aircv我的项目却要求传入的图片必须蕴含bgr三个通道,不然会报错)
- 反对背景通明的图片
- 优化了find_all_template办法的性能,用numpy的切片赋值代替floodFill办法来防止重叠,大略会缩短1/4的总体查找时间
示例
比方咱们对“思否”课程菜单截图如下:
咱们想从中找到#的地位,能够提供一张小模板图:
而后调用find_template办法:
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)
失去的match_result,标识了第一个#在源图中的中心点地位,矩形区域四角坐标 和 匹配度。
<code class="json">{ "result": (x,y), #tuple,示意辨认后果的中心点 "rectangle":[ #二位数组,示意辨认后果的矩形四个角 [left, top], [left, bottom], [right, top], [right, bottom] ], "confidence": percentage #辨认后果的匹配度,在-1~1之间,越大匹配度越高, 如果为1,示意按像素严格匹配 }
咱们能够用这个后果,在源图上标识出匹配的地位:
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)
后果如下图所示: