/image_comparison_opencv

python project with opencv to compare image similarity

Primary LanguagePython

Image Comparison - Alternative Approach with OpenCV

this is another alternative approach to solve the image comparison task, which might not contains as much as detailed documentation as the pixel comparison approach.

Table of contents

About the Program

A program written in Python that aim to help an internal user to automate this process of comparing two images.

With a given input cvs file and an output result csv file, the program reduce the tedious manual work by comparing images pixel by pixel.

The goal of this project is to reduce automate process and boost efficiency by eliminating toil and making work interesting.

Getting Started - Steps by Steps

Environments

The program could be run in both Windows and MacOS system, either in Python IDE or Terminal (Commend Prompt in Windows)

If you are a Mac user, you can either download PyCharm or running in your terminal directly, followed by the below instructions.
If you are using Windows, you could do:

  • Download PyCharm - develop, test and run this program.
  • Download Git, and follow the Mac Instructions.
  • Running in Command Prompt directly, however you need to run the Windows version of the commands as this docs mainly cover the Linux/MacOS instruction.
  • You could also setup your virtual environment, you can find details from here.

Install python

To run this program, python 3 is required. If you do not have python installed, or wish to updated existing from python 2, you can do it from download python.

Install Other Libraries

The program requires OpenCV package in python. You can install it by running following command:

pip3 install opencv-python

If your default Python distribution does not come with pre-installed scikit-image, we need to install scikit-image inside of already configured default Python environment. This will allow us to use compare_ssim, which computes the mean structural similarity index between two images.

pip3 install scikit-image

This is the same command for Windows, MacOS and Linux Installation.

How to Use the Program

Clone This Repo

To clone this repo, you can use various source code management tools such as Source Tree and Github Desktop or simply run

git clone https://github.com/discoliver/image_comparsion_opencv.git

Running the Program

To run this program, simply run the compare.py script, following a cvs file path which contains pairs of images.

python3 compare.py filename.csv

or

python3 compare.py absolute/path/to/yout/filename.csv

This repo has also provided an example of the input cvs file images.csv , which indicates the format that you should follow for this program to run. To run this test csv file, type the command below:

python3 compare.py images.csv

You can also run them in PyCharm as well.

Result and Test

After successfully run the program (See FAQ for error you might get), you should expect a result.csv file in your repo, which contains the information of 2 images, a similarity score converted from Structural Similarity Index (SSIM), and an elapsed time.

Re-run the program will automatically delete the file and regenerate the new result file.

However well this program perform? The images.csv contains a simple test set of images to demonstrate how this program handle each scenario.

Compared to the test set result from individual pixel comparison, I have included another test analysis based on new test set images. Compared with the actual similarity score and estimated Bjorn Score, the SSIM approach preforms better as it can estimate the similarity score based on the structural difference.


Test with pure color, with accurate result

slight modification

Test with pure color and character, with poor prediction as only focus on color scheme.


luminance

Test with pure color picture rotation.

Color and mint

Test with contrast adjustment and photoshopped picture

Contrast and Photoshop

Solution Approach

Background

As time permits, I have implemented this alternative approach to solve the image comparison task, leveraging OpenCV and SSIM method, which might not contains as much as detailed documentation as the pixel comparison approach.

Thoughts Gathering

Using the Structural Similarity Index (SSIM), we evaluate the group of pixels so we can easily determine differences due to slight image manipulations, tampering, adjustment. This approach is better than comparing individual pixel which could be affected by noise and image manipulations. SSIM could help us perceive the change in structural information of the image, such as adjusting the contrast or photoshopped overlay.

slight modification

With packages cv2 for OpenCV bindings and scikit-imag for pre-definited Structural Similarity Index, this approach is easy to implement and maintain as well.

Initial Approach

The original workflow of this approach demonstrate as below:

  1. Import the Image files into image_list from cvs.
  2. For every row of data in cvs (every element in image_list), create image i1 and i2 using cv2.imread.
imageA = cv2.imread(image_row[0])
imageB = cv2.imread(image_row[1])
  1. We convert them to grayscale (focus luminance and reduce the complexity of code) and calculate SSIM score
gray_imageA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
ssim_score = compare_ssim(gray_imageA, gray_imageB)
  1. We record the time elapsed_time after complete calculation, and convert SSIM range [-1, 1] to [0, 1].
score = ssim_score * 0.5 + 0.5
  1. Import the image filename, similarity and elapsed_timeinto new cvs output file: result.cvs.

Improvement to be done

There are some work left to improve this work including:

  1. Add resize function to enable this program to compare different size of image.
  2. We can also compute SSIM for Red, Green, and Blue components and average them together, without greyscale, which lower the performance but provide better accuracy.

Maintain the Program

To Maintain this application, please go through this README carefully and contact meif you have any question. Additional knowledge transfer session will be hold.

Ensure you have understood the logic of the program is fundamental to maintain this project. There are couple of tips can help as well.

  1. Read error log - details catch error and debug output is written in the script.
  2. Check your environment - although this program does not require many dependencies, you still have to check you have correct version of python, and other libraries installed.
  3. Always test it when you contribute this program; Work in specific topic branches and keep the commit history clean, with proper tag if necessary.
  4. Refer to FAQ session for common question.

Contribute and Update

Your contribution are warmly welcome to make this project better. Please fork the repository and create pull request if you want. Appreciate anyone to jump in and help out.

This project does not include much dependency but a few things below could help you maintain the latest version of the application.

  • Contact the author to add the user to the Github Notification. When new merge, push or other activities happens you will receive the updates.
  • Ensure you have the proper version of cv2 and skimage, update themn to latest if necessary.
  • Ensure your Python is up-to-date

Acknowledgments and License

There are a few helpful examples and research help to shape this project, and they have demonstrated the different perspectives and methods to accomplish the test. Here are some of good references.

Zenva, Implementing Structural Similarity for Images。
scikit-image, image processing in python.
Adrian Rosebrock, Image Difference with OpenCV and Python.

This project is licensed under the MIT License.

FAQ

Frequently asked questions about this program can be found below.

How to ensure this program works?

There are some test files in the image folders, which covers different case from modification, contrast adjustment and image rotation. Feel free to contribute more test case or contact the author if you needed.

What if I have received error?

Check the console output to quickly categorize the error either in:

  • Your system environment, either python or related libraries is setup incorrectly.
  • Your input csv contains wrong format.
  • Your image mode is not RGB or the format we do not support.

Can I input cvs files with different format?

Unfortunately, you have to follow the format of the provided csv example.

Does this program restrict to MacOS?

This program can be running on MacOS, Windows and Linux, as long as you have configured correctly. (See getting started) The easiest way to run on Windows is to get Git Bash and follow the MacOS instruction, but you can always run in Windows Command Prompt or Pycharm IDE.