1.思路
原先图片匹配一般都是缺口匹配全图
优化点:
1.缺口图片匹配缺口所在图片那一行图片可以提高他识别率
2.移动后再进行2次匹配计算距离
2.代码
def get_image_deviation():
block = cv.imread("img.png", -1)
backimg = cv.imread("bg_img.png")
gray_backimg = cv.cvtColor(backimg, cv.COLOR_RGB2GRAY)
blockWidth, blockHeight = block.shape[1], block.shape[0]
left = blockWidth
right = 0
top = blockHeight
bottom = 0
for i in range(0, blockHeight):
for j in range(0, blockWidth):
if block[i, j, 3] > 0:
if j <= left:
left = j
if j >= right:
right = j
if i <= top:
top = i
if i >= bottom:
bottom = i
blockBox = block[top:bottom, left:right]
blockBox_width, blockBox_height = blockBox.shape[1], blockBox.shape[0]
print(blockBox_width)
blockMask = np.zeros([blockBox_height, blockBox_width], np.uint8)
for i in range(0, blockBox_height):
for j in range(0, blockBox_width):
if blockBox[i, j, 3] > 0:
blockMask[i, j] = 255
blockBox = cv.cvtColor(blockBox, cv.COLOR_RGBA2GRAY)
kernel = np.ones((3, 3), np.uint8)
blockMask = cv.erode(blockMask, kernel, iterations=1).astype(np.float32)
backgroundROI = gray_backimg[top:bottom, :]
blockBox = (blockBox * 1.0).astype(np.float32)
backgroundROI = (backgroundROI * 1.0).astype(np.float32)
res = cv.matchTemplate(backgroundROI, blockBox, cv.TM_CCORR_NORMED, mask=blockMask)
loc = cv.minMaxLoc(res)
print("loc==", loc[3][0])
locs = (loc[3][0])
return locs