若要使用OpenCV/cv2比较并标记2张图像(带图片)之间的差异 [英] To use OpenCV/cv2 to compare and mark the difference between 2 images (with pictures)

查看:721
本文介绍了若要使用OpenCV/cv2比较并标记2张图像(带图片)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python和cv2比较2张图片,如下所示.

I want to use Python and cv2 to compare 2 images, like below.

(Python 2.7 + Windows)

(Python 2.7 + Windows)

c:\ Original.jpg

c:\Original.jpg

c:\ Edited.jpg

c:\Edited.jpg

很简单,我可以在下面做,并保存一张显示差异的图片:

Pretty straight forward I can do below and save a picture showing the difference:

import cv2 

Original = cv2.imread("c:\\Original.jpg")
Edited = cv2.imread("c:\\Edited.jpg")

diff = cv2.subtract(Original, Edited)

cv2.imwrite("c:\\diff.jpg", diff)

结果就像:

c:\ diff.jpg

c:\diff.jpg

此外,我希望根据所比较的文件将差异显示在图片中.换句话说,我想在"Edited.jpg"的基础上画一个圆圈或标记出差异.有可能吗?

Further, I want the difference to be shown in a picture, based on the files compared. In another word, I want to have a picture circle or mark the difference, based on "Edited.jpg". is it possible?

(想想其中一种方法是,识别"diff.jpg"中的可见区域,然后为"Edited.jpg"中的区域绘制一个圆圈吗?)

(thinking one of the ways could be, to identify the visible area in the "diff.jpg", then draw a circle for the area in the "Edited.jpg"?)

推荐答案

感谢Micka的帮助.添加以下内容,并且可以使用.

Thanks to Micka's help above. Below is added, and it works.

im = cv2.imread('c:\\diff.jpg')
im1 = cv2.imread('c:\\Edited.jpg')


imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(im1, contours, -1, (0,255,0), 1)
cv2.imwrite("c:\\see_this.jpg", im1)

c:\ see_this.jpg

c:\see_this.jpg

这篇关于若要使用OpenCV/cv2比较并标记2张图像(带图片)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆