Nam Vu
Nam Vu

Reputation: 5725

Compare two images and get the percent difference

I known some ways to compare 2 images with ImageMagick or OpenCV

But for my case, I also have the same character with different position. Image1:

enter image description here

Image2:

enter image description here

or

enter image description here or

enter image description here

So, what should I do now to find the % difference value between Image1 and Image2?

Upvotes: 2

Views: 4884

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207698

This actually answers your question - which doesn't in fact ask anything about images 3 and 4 - but I fear it will not help you much.

As @GPPK suggests, you need to trim the extraneous material off around your kanji characters, which you can do with the -trim command in ImageMagick. I have added a thin red border so you can see where the edges are:

convert kanji2.png -trim kanji2-t.png

enter image description here

If you want do that to images 1 and 2, and then compare them, you can do that all in one go like this:

convert -metric ae kanji1.png kanji2.png -trim -compare -format "%[distortion]" info:
0

which shows there are zero pixels different in the resulting images if you trim kanji1 and kanji2.

If you compare the trimmed kanji1 and kanji3 like this, you get:

convert -metric AE kanji1.png kanji3.png -trim -compare -format "%[distortion]" info:
893184

which indicates 900,000 pixels of 5,000,000 are different.

Likewise, if you compare kanji1 and kanji4:

convert -metric AE kanji1.png kanji4.png -trim -compare -format "%[distortion]" info:
1.14526e+06

or 1.1 million of 5 million.

But this doesn't help when your images are a different size (scale), or rotated.

You could scale your images to a normalised size before comparing, and I guess that might help you become a bit more "scale invariant":

convert -metric AE kanji1.png kanji4.png -trim -scale 1000x1000! -compare -format "%[distortion]" info:

You could also rotate your images using a little iterative procedure that rotates the images through say +/- 20 degrees and chooses the one with the smallest trimmed bounding box to become a little more "orientation invariant". But then you will still have a problem if the characters are sheared, or fatter, or thinner, or brighter, or darker, or contrastier... I think you need to look into "Template Matching".

Upvotes: 3

Related Questions