python3 image_python3 image_detect_03.py --source_image=***.png --target_image=***.png

展开全部

通过opencv,识别出两张2113图片不同部分。俗称大家一5261起来找茬

效果图

resize,m_lfit,w_600,h_800,limit_1

源代码 image_detect_03.pyimport cv2

import numpy as np

from matplotlib import pyplot as plt

import argparse

def matchAB(fileA, fileB):

# 读取图像4102数据

imgA = cv2.imread(fileA)

imgB = cv2.imread(fileB)

# 转换成灰色

grayA = cv2.cvtColor(imgA, cv2.COLOR_BGR2GRAY)

grayB = cv2.cvtColor(imgB, cv2.COLOR_BGR2GRAY)

# 获取图片1653A的大小

height, width = grayA.shape

# 取局部图像,寻找匹配位置

result_window = np.zeros((height, width), dtype=imgA.dtype)

for start_y in range(0, height-100, 10):

for start_x in range(0, width-100, 10):

window = grayA[start_y:start_y+100, start_x:start_x+100]

match = cv2.matchTemplate(grayB, window, cv2.TM_CCOEFF_NORMED)

_, _, _, max_loc = cv2.minMaxLoc(match)

matched_window = grayB[max_loc[1]:max_loc[1]+100, max_loc[0]:max_loc[0]+100]

result = cv2.absdiff(window, matched_window)

result_window[start_y:start_y+100, start_x:start_x+100] = result

# 用四边形圈出不同部分

_, result_window_bin = cv2.threshold(result_window, 30, 255, cv2.THRESH_BINARY)

_, contours, _ = cv2.findContours(result_window_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

imgC = imgA.copy()

for contour in contours:

min = np.nanmin(contour, 0)

max = np.nanmax(contour, 0)

loc1 = (min[0][0], min[0][1])

loc2 = (max[0][0], max[0][1])

cv2.rectangle(imgC, loc1, loc2, 255, 2)

plt.subplot(1, 3, 1), plt.imshow(cv2.cvtColor(imgA, cv2.COLOR_BGR2RGB)), plt.title('A'), plt.xticks([]), plt.yticks([])

plt.subplot(1, 3, 2), plt.imshow(cv2.cvtColor(imgB, cv2.COLOR_BGR2RGB)), plt.title('B'), plt.xticks([]), plt.yticks([])

plt.subplot(1, 3, 3), plt.imshow(cv2.cvtColor(imgC, cv2.COLOR_BGR2RGB)), plt.title('Answer'), plt.xticks([]), plt.yticks([])

plt.show()

if __name__ == '__main__':

parser = argparse.ArgumentParser()

parser.add_argument(

'--source_image',

type=str,

default='img/image01-0.png',

help='source image'

)

parser.add_argument(

'--target_image',

type=str,

default='img/image01-1.png',

help='target image'

)

FLAGS, unparsed = parser.parse_known_args()

matchAB(FLAGS.source_image, FLAGS.target_image)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值