I want to use Python and cv2 to compare 2 images, like below.
(Python 2.7 + Windows)
c:\Original.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)
the result is like:
c:\diff.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?
(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"?)
The difference between two images is calculated by finding the difference between each pixel in each image, and generating an image based on the result.
cv2 (old interface in old OpenCV versions was named as cv ) is the name that OpenCV developers chose when they created the binding generators. This is kept as the import name to be consistent with different kind of tutorials around the internet.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With