目录
前言
看到别人的代码中使用aircv, 查看了相关信息,记录一下
1.引入库
import aircv
2.__init__.py文件中代码部分代码
def find_template(im_source, im_search, threshold=0.5, rgb=False, bgremove=False):
'''
@return find location
if not found; return None
'''
result = find_all_template(im_source, im_search, threshold, 1, rgb, bgremove)
return result[0] if result else None
def find_all_template(im_source, im_search, threshold=0.5, maxcnt=0, rgb=False, bgremove=False):
'''
Locate image position with cv2.templateFind
Use pixel match to find pictures.
Args:
im_source(string): 图像、素材
im_search(string): 需要查找的图片
threshold: 阈值,当相识度小于该阈值的时候,就忽略掉
Returns:
A tuple of found [(point, score), ...]
Raises:
IOError: when file read error
'''
# method = cv2.TM_CCORR_NORMED
# method = cv2.TM_SQDIFF_NORMED
method = cv2.TM_CCOEFF_NORMED
if rgb:
s_bgr = cv2.split(im_search) # Blue Green Red
i_bgr = cv2.split(im_source)
weight = (0.3, 0.3, 0.4)
resbgr = [0, 0, 0]
for i in range(3): # bgr
resbgr[i] = cv2.matchTemplate(i_bgr[i], s_bgr[i], method)
res = resbgr[0]*weight[0] + resbgr[1]*weight[1] + resbgr[2]*weight[2]
else:
s_gray = cv2.cvtColor(im_search, cv2.COLOR_BGR2GRAY)
i_gray = cv2.cvtColor(im_source, cv2.COLOR_BGR2GRAY)
# 边界提取(来实现背景去除的功能)
if bgremove:
s_gray = cv2.Canny(s_gray, 100, 200)
i_gray = cv2.Canny(i_gray, 100, 200)
res = cv2.matchTemplate(i_gray, s_gray, method)
w, h = im_search.shape[1], im_search.shape[0]
result = []
while True:
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
if DEBUG:
print('templmatch_value(thresh:%.1f) = %.3f' %(threshold, max_val)) # not show debug
if max_val < threshold:
break
# calculator middle point
middle_point = (top_left[0]+w/2, top_left[1]+h/2)
result.append(dict(
result=middle_point,
rectangle=(top_left, (top_left[0], top_left[1] + h), (top_left[0] + w, top_left[1]), (top_left[0] + w, top_left[1] + h)),
confidence=max_val
))
if maxcnt and len(result) >= maxcnt:
break
# floodfill the already found area
cv2.floodFill(res, None, max_loc, (-1000,), max_val-threshold+0.1, 1, flags=cv2.FLOODFILL_FIXED_RANGE)
return result
ac.find_template(im_source, im_search, threshold=0.5, rgb=False, bgremove=False) im_source:被查找的源图 im_search:要查找的模板图 threshold:值在0到1之间。因为图像匹配并不需要每个像素精确一致,可以模糊匹配,所以这个值设定得越高,找到的区域就越接近模板图,但设得太高就有可能找不到。 rgb: 默认为False. rgb为True,则进行rgb三通道的拆分。 rgb为False, 则进行灰度处理。bgremove:是否去除背景。如果这个值设置为True,那么函数内部会用Canny算子先提取图像轮廓,再做查找 match_result:{ 'result': (x,y), #tuple,表示识别结果的中心点 'rectangle':[ #二位数组,表示识别结果的矩形四个角 [left, top], [left, bottom], [right, top], [right, bottom] ], 'confidence': percentage #识别结果的置信度,在0-1之间,越大越精准 }
3. 示例代码
def pic_similar_judge(original_image, new_image):
original_image = ac.imread(original_image)
new_image = ac.imread(new_image)
similar = ac.find_template(original_image, new_image, threshold=1, rgb=False, bgremove=True) # 老图片跟新图片对比
return similar
if __name__ == '__main__':
original = r"D:\original.png"
new = r"D:\new.png"
print(pic_similar_judge(original, new))
根据__init__.py中的代码,similar值如下:
return result[0] if result else None
图片匹配失败,则返回None.
匹配成功,则返回值如:{‘result’: (108.5, 29.0), ‘rectangle’: ((0, 0), (0, 58), (217, 0), (217, 58)), ‘confidence’: 1.0}