Giter VIP home page Giter VIP logo

spot-the-differences's Introduction

Spot-The-Differences

Spot the differences between two images using Python and OpenCV. For a better and visual understanding of this project and it's concepts, watch the video in Youtube or click on the image-link below.

Spot-the-Differences

Main Idea

This program can spot-find the differences between two images. The user loads to the program 2 images that are mostly the same but also have some small differences. By running the program user gets both images side by side with their differences highlighted.

We will check two methods. For the first method we will use images city1.jpg and city2.jpg (Photo by Fede Roveda from Pexels) and for the second we will use images camels1.jpg and camels2.jpg (Photo by Travel Photography from Pexels)

First Method

Using cv2.absdiff. Script img_diff1.py

First we are loading the two images.

img1 = cv2.imread('path_to_image_1')
img2 = cv2.imread('path_to_image_2')

Then we are converting both images to grayscale format.

gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

Now it's time to find the absolute difference between the two images (arrays).

diff = cv2.absdiff(gray1, gray2)
cv2.imshow("diff(img1, img2)", diff)

Apply threshold. Apply both THRESH_BINARY and THRESH_OTSU.

thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv2.imshow("Threshold", thresh)

We are going to use 2 iterations of dilation in order to increase the white region in threshold.

kernel = np.ones((5,5), np.uint8) 
dilate = cv2.dilate(thresh, kernel, iterations=2) 
cv2.imshow("Dilate", dilate)

Finally we are calculating the contours and draw rectangles in both images which are corresponding to the differences between the 2 images.

contours = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)

The results are the following :

Second Method

Using compare_ssim. Script img_diff2.py

Let's load our two images.

We are following the steps of the First Method with some small changes. Instead of cv2.absdiff, now we are computing the full structural similarity (similar) between the two gray images. Also we must convert diff array in range [0, 255].

(simalr, diff) = compare_ssim(gray1, gray2, full=True)
diff = (diff*255).astype("uint8")

After deleting the dilation part from first method we are calculating the contours as before.
The results are the following :

Note : For a better understanding of the two methods you can check the two scripts img_diff1.py and img_diff2.py which are having all the necessary comments for a better explanation of each method's step.

Check also the youtube video : Spot the differences.

Author

  • Konstantinos Thanos

spot-the-differences's People

Contributors

kostasthanos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.