How to find a sub-image within another image and then diff?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find a sub-image within another image and then di

Post by fmw42 »

did you try running it multiple times to see if the latter times speed up?

Hope someone else on Windows can verify times as well?

Otherwise, wait for Magick to comment or submit a bug report about the long times.
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to find a sub-image within another image and then di

Post by minglw »

I tried 3 more times and here are the results:


F:\t\test\q3>timer imcompare -metric rmse -dissimilarity-threshold 1 image1.png image2s.png diff.png

Timer 8.00 : Igor Pavlov : Public domain : 2008-11-25
6724.11 (0.102603) @ 39,91

Kernel Time = 0.171 = 00:00:00.171 = 0%
User Time = 1022.515 = 00:17:02.515 = 192%
Process Time = 1022.687 = 00:17:02.687 = 192%
Global Time = 531.242 = 00:08:51.242 = 100%

F:\t\test\q3>timer imcompare -metric rmse -dissimilarity-threshold 1 image1.png image2s.png diff.png

Timer 8.00 : Igor Pavlov : Public domain : 2008-11-25
6724.11 (0.102603) @ 39,91

Kernel Time = 0.093 = 00:00:00.093 = 0%
User Time = 1060.187 = 00:17:40.187 = 197%
Process Time = 1060.281 = 00:17:40.281 = 197%
Global Time = 536.014 = 00:08:56.014 = 100%

F:\t\test\q3>timer imcompare -metric rmse -dissimilarity-threshold 1 image1.png image2s.png diff.png

Timer 8.00 : Igor Pavlov : Public domain : 2008-11-25
6724.11 (0.102603) @ 39,91

Kernel Time = 0.078 = 00:00:00.078 = 0%
User Time = 1065.578 = 00:17:45.578 = 196%
Process Time = 1065.656 = 00:17:45.656 = 196%
Global Time = 542.556 = 00:09:02.556 = 100%



As you can see, it seems to be a small bit faster, but it still takes 17+ minutes. Seems to be waaaaaaay too slow.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find a sub-image within another image and then di

Post by fmw42 »

Yes, 17 minutes is way too long, unless I don't understand your configuration. You should report this to the bugs forum and see if there is something odd with the windows version of IM as I don't get anywhere near that long on my Mac, just a couple of seconds.

I should point out that your subsection image2s has the same width as image1 and is only cropped by about 30 pixels in height.

Perhaps try making image2s cropped in both directions and see what happens.
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to find a sub-image within another image and then di

Post by minglw »

Thanks Fred!

I will report it as a bug and hope the developers can check it out.


You wrote: I should point out that your subsection image2s has the same width as image1 and is only cropped by about 30 pixels in height.

For your information:

image1.png is 1024x768.

image2s.png is 940x650.

Not like you said, same width or cropped by 30 pixels in height.

Are you sure you're looking at the right images? (I just downloaded the two images from the links I have above and confirmed that the image sizes are what I said above.)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find a sub-image within another image and then di

Post by fmw42 »

image2s.png is 940x650.
This is not what I downloaded and therefore is the root of your problem. In the compare, you need to have the larger image first. Thus

The files I got were image1 512x384 and image2s 512x354

Let me try downloading again.

OK, I just dragged the pictures so only got reduces size versions. I now have the full size ones.

compare -metric rmse image1.png image2s.png image12s_diff.png

time compare -metric rmse image1.png image2s.png image12s_diff.png
6724.11 (0.102603) @ 39,91

Took 11 minutes on my computer. So there is no bug or error. It gets slower the bigger the images are.



real 11m11.454s
user 9m44.523s
sys 0m6.193s
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to find a sub-image within another image and then di

Post by minglw »

Interesting finding!

I thought everyone would just download the image.
Well, blame it on google to show the "thumbnail". :-)

Thank you Fred for verifying that it's really big size images that's causing the much longer time.


Seems like you're the master of combining multiple tasks into a single command.

What's the single command that can do the job I've asked about on this topic?
(basically lineup image2 in image1 and then diff and save the plant field -- as shown in the expected output)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find a sub-image within another image and then di

Post by fmw42 »

what is that you want to do?

first find the matching subsection between image1 and image2s and then get the difference image between the located subsection of image1 and image2s?

If so, let me work on that tomorrow. It is too late this evening for me to think straight. But compare and convert do not combine into one command. So it will take two command lines or more.
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to find a sub-image within another image and then di

Post by minglw »

fmw42 wrote:what is that you want to do?

first find the matching subsection between image1 and image2s and then get the difference image between the located subsection of image1 and image2s?

If so, let me work on that tomorrow. It is too late this evening for me to think straight. But compare and convert do not combine into one command. So it will take two command lines or more.
See.
Yes, you're correct: 1. find the matching subsection between image1 and image2s,
2. then get the difference image between the located subsection of image1 and image2s.

Take your time.

Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find a sub-image within another image and then di

Post by fmw42 »

# get offset coords between the two unequal sized images
coords=`compare -metric rmse image1.png image2s.png null: 2>&1 | cut -d\ -f 4 | tr "," "+"`

# get size of smaller image image2s.png
size=`convert image2s.png -ping -format "%wx%h" info:`

# get subsection of image1.png at same size as image2s.png and at offset and then find difference image
convert image2s.png image1.png[${size}+${coords}] -alpha off +repage \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim \
image12sdiff.png


NOTE: This uses unix commands. If you are on Windows, then some Windows user will need to help you convert to similar operations. Or see
http://www.imagemagick.org/Usage/windows/
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to find a sub-image within another image and then di

Post by minglw »

Thank you very much Fred!

The cmds you gave work great.

I wish the cmd to figure out the coordinates can be faster.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find a sub-image within another image and then di

Post by fmw42 »

There is another way, but it means you have to recompile IM in HDRI. You can use my normcrosscorr script that uses FFT processing. see http://www.fmwconcepts.com/imagemagick/ ... urier.html and particular http://www.fmwconcepts.com/imagemagick/ ... mcrosscorr

I would have to recompile and test your images sometime to see, but I expect it will be much faster.
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to find a sub-image within another image and then di

Post by minglw »

I don't have the proper development environment setup, otherwise, I would try to compile it and then try your scripts and see.

I will wait for you to try it out and see. If it's much faster, I am hoping that a Windows release can be made available for download from the developers.

Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find a sub-image within another image and then di

Post by fmw42 »

Is FFT not available for Windows users? (FFTW delegate in particular -- see http://www.fftw.org/install/windows.html)

Are you converting my commands to Windows or are you running them under Cygwin or the like?

With Cygwin, you ought to be able to run the scripts, but need to compile IM under HDRI.
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to find a sub-image within another image and then di

Post by minglw »

Converting simple shell scripts to run under Windows is easy. However, if Unix specific programs are involved, conversion is not easy.
The easiest method is to run them under Cygwin. I have Cygwin all downloaded -- just need to install it.
So, I think running your scripts will not be a problem.

Is there a page showing how to compile IM with HDRI ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find a sub-image within another image and then di

Post by fmw42 »

Post Reply