-2

I need to write tests (in Java) for an image capturing application. To be more precise:

  1. Some image is captured from a scanner.
  2. The application returns a JPEG of this image.
  3. The test shall compare the scanned image with a reference image. This reference image has been captured by the identical application and has been verified visually that it contains the same content.

My first idea was comparing the images pixel by pixel but as the application applies a JPG compression the result would never by "100 % identical". Besides the fact that the scanner would capture the image with slight differences each time.

The goal is not to compare two "random images" for similarity like "Do both images show a car?" but rather "How similar is the captured car to the reference image of this car?".

What other possibilities do you see?

Robert Strauch
  • 12,055
  • 24
  • 120
  • 192
  • 3
    This is not as easy as it sounds. What about an image that has been shifted by 4 pixels? It would look nearly identical, but if all that was done was basic pixel analysis, it would be marked very different. I am betting that whole fields of study are based on this very issue. – Hovercraft Full Of Eels Mar 26 '15 at 20:01
  • 1
    Look into machine learning. That is the sort of thing that you will need in order to get this working. However, there are plenty of Ph.D computer scientists working on almost this exact problem. They've been at it for literally decades. While they've had some success, there are still very large problems with all current implementations. Don't expect this to be a simple problem. – Davis Broda Mar 26 '15 at 20:07
  • 1
    This SO posting seems to answer your question in detail and provides you with a few different approaches: http://stackoverflow.com/questions/843972/image-comparison-fast-algorithm – Marc Mar 26 '15 at 21:00

1 Answers1

1

Let me say first, that image processing is not my best field, nor am I an expert in any way. However, here is my suggestion: take the absolute value of each pixel in the pictures, in row-major order, respective to each other. Then, average out the differences, and see if the average is within a certain threshold: if so, the images are similar in nature. Another way, would be to go once more pixel by pixel, but instead count the number of pixels that differ by at least x, where x is a number chosen to represent how close the matching needs to be. Then, just see if the different pixels comprise 5% of all pixels, 10%, and so on. The more pixels, the more different the image.
I hope this helps you, and best of luck.
P.S. to judge how far two pixels are apart, it's going to be like finding the distance between two points (i.e. sqrt[(r2-r1)^2+(g2-g1)^2+(b2-b1)^2]).
P.P.S. This all of course assumes that you listened to the Hovercraft Full of Eels, and have somehow made sure that the images match up in size and position.

Zesty Memes
  • 442
  • 2
  • 11