1

I am looking for an easiest way to compare images in two different directories and identify the best match. After finding the best match, save images into a new directory with the same name of images that are in source directory. For example

/path/to/src/img1, img2, img3.......

/path/to/dest/img11, img22, img33.....

/path/to/target/img1, img2, img3....

I did some research online i found compare command using imagemagick but i am quit new and couldn't figure out how it will work with set of multiple images in two different directories and name the images with respect to names of src directory. Please ask me if i couldn't explain you my problem briefly. Any help will be much appreciated. Thanks in advance.

jHz
  • 79
  • 1
  • 11
  • It depends some extent on *how* you expect the images to differ. An image that is identical to another except flipped about one axis would have an identical min/mean/max and standard deviation but maybe no two pixels the same. Two images shot with a slightly different exposure may be visually almost indistinguishable from each other, yet have no two pixels the same. – Mark Setchell Sep 26 '16 at 12:13
  • In my case I have images taken at same camera angle and their size is same but one with projection of patterns and other without projection. – jHz Sep 26 '16 at 13:27
  • Can you show samples? – Mark Setchell Sep 26 '16 at 13:31
  • For your reference i have uploaded some images and all other are same like these. Please check the link. https://www.mediafire.com/?aizx2z1u76z0db9 – jHz Sep 26 '16 at 13:49
  • @MarkSetchell Hello, actually it didn't work for me. May be I couldn't explain you well about my problem. Here I found an another question same like I asked and answer 2 well suits to my problem. But it is not giving me 100% results. It is eliminating some of pictures in the folder and finding wrong match for 2 or 3 pictures sometimes. [http://stackoverflow.com/questions/12649876/search-for-similar-images-with-different-filenames-in-two-directories?rq=1] (http://stackoverflow.com/questions/12649876/search-for-similar-images-with-different-filenames-in-two-directories?rq=1) – jHz Sep 29 '16 at 02:25

1 Answers1

1

You can perform a normalised cross-correlation between each image in the current directory and each image in the other directory and find which one has the highest correlation like this:

#!/bin/bash

# Work out list of source and destination files
SRC=(*.jpg)
DST=(../wop/*.jpg)

# Ensure output directory exists
TGT=result
[ ! -d "$TGT" ] && mkdir "$TGT" 

for a in ${SRC[@]}; do
   nearest=0
   for b in ${DST[@]}; do
      # Perform normalised cross-correlation with each image in other directory
      result=$(convert "$a" "$b" -metric ncc -compare -format "%[distortion]" info:)
      # Convert result to rounded integer percentage
      percent=$(echo "scale=0;$result*100/1" | bc)
      echo DEBUG compare $a with $b: $percent
      # Update if this one is nearer than previous nearest
      [ $percent -gt $nearest ] && { nearest=$percent; friend=$b; }
   done
   echo cp "$friend" "$TGT/$a" 
done

Output

./go
DEBUG compare 00000000.jpg with ../wop/00000007.jpg: 22
DEBUG compare 00000000.jpg with ../wop/00000014.jpg: 17
DEBUG compare 00000000.jpg with ../wop/00000015.jpg: 77
cp ../wop/00000015.jpg result/00000000.jpg
DEBUG compare 00000001.jpg with ../wop/00000007.jpg: 37
DEBUG compare 00000001.jpg with ../wop/00000014.jpg: 71
DEBUG compare 00000001.jpg with ../wop/00000015.jpg: 32
cp ../wop/00000014.jpg result/00000001.jpg
DEBUG compare 00000005.jpg with ../wop/00000007.jpg: 77
DEBUG compare 00000005.jpg with ../wop/00000014.jpg: 36
DEBUG compare 00000005.jpg with ../wop/00000015.jpg: 31
cp ../wop/00000007.jpg result/00000005.jpg
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • how can we say that these are similar images? I didn't understand from the output. – jHz Sep 26 '16 at 14:24
  • The decimal number is the normalised cross correlation - the nearer it is to 1 the more similar the images, so 000000.jpg is most similar to 000015.jpg, and 00001.jpg is most similar to 000014.jpg. – Mark Setchell Sep 26 '16 at 15:11
  • Ok thank you. I understood the logic but how can I use this info to copy images from src directory to target directory. – jHz Sep 26 '16 at 15:14
  • Thank you sir, your script is working but after so many tries I am still unable to copy images from directory "wop" into another directory with names of same images in "wp" directory. – jHz Sep 27 '16 at 03:08
  • Aha, I think I understand what you want now and have changed the code - please try again. – Mark Setchell Sep 27 '16 at 06:46