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?".
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

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

Postby minglw » 2010-02-23T21:35:37-07:00

This is similar to the objective I've asked about on another topic (How to save the DIFFERENCES between two images ?):
viewtopic.php?f=1&t=15584

Except this time that the two images are of different size.
Here are the details...

I have two images:

image1.png -- this is the background image
link to image: https://docs.google.com/leaf?id=0B-jgOG ... ZTc4&hl=en

imge2s.png -- this is the background image with some stuff added to it. However, this image is only a sub-section of image1.png.
this image is smaller than image1.png.
link to image: https://docs.google.com/leaf?id=0B-jgOG ... NmRi&hl=en

the ultimate end goal is still the same as what I've asked in the other topic:
save the differences between the two images.
In the end, I should see the same "plants" as the diff as in the other topic.

Here is the expected output:
https://docs.google.com/leaf?id=0B-jgOG ... MDU2&hl=en


It seems to me that in order to accomplish that, I have to first tell IM to local the image sub-image first and then only do a diff on the matching sub-image section.

Question is, is it possible to do that? If so, how?

Thanks!

User avatar
fmw42
Posts: 15150
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA
Contact:

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

Postby fmw42 » 2010-02-23T21:52:03-07:00

Yes, you can do it. You use the compare function with two image that are not the same size and specify an output image. The output image will have two frames. The second will be the similarity image and the best match will also be reported to the terminal. see viewtopic.php?f=1&t=14613&p=51076&hilit=compare+metric#p51076 for an example

However, I am not sure that is needed, since if you leave off the +repage at the end and then look at the verbose information for the page geometry it will tell you the offset. For example in my first example

convert cyclops_question.png cyclops.png \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim \
cyclops_sep.png

identify -verbose cyclops_sep.png
...
Geometry: 20x20+0+0 <--- tells you the actual resulting image size as 20x20
...
Page geometry: 100x100+40+40 <--- tells you the offset of the resulting image relative to the original image (so it is at 40,40 rel to 100x100 original)
Origin geometry: +40+40 <--- tells you just the offset

In fact, you can flatten them together without specify the location and the virtual canvas (page geometry) will know exactly where to put it so that it goes back where it came from in the original image.

convert cyclops.png cyclops_sep.png -flatten cyclops_cyclops_sep_flatten.png

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

Postby minglw » 2010-02-23T22:51:42-07:00

I guess I missed your point. In your example that creates the "cyclops_sep.png", the two cyclops images are the same size.
When I try the same thing on my two images (image1.png and image2s.png) the resulting image is the same size as image2s.png (it actually looks the same as image2s.png other than the file size).

I tried following your link to the other post with the compare command.
The compare command seems to hang and didn't return, not sure why.
The compare command actually worked the first time, but when I try it the second time it hangs.

This is the compare command I've tried: compare -metric rmse image1.png image2s.png out.png

You mentioned "The output image will have two frames", how do you see the two frames ? I loaded the image in IMDisplay and I only see one image that looks like image1.png, but only "brighter" (or whiter).

User avatar
fmw42
Posts: 15150
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA
Contact:

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

Postby fmw42 » 2010-02-24T10:37:49-07:00

compare -metric rmse largeimage smallimage resultimages

if the two images are different sizes, then resultimages is actually two images, resultimges-0 and resultimages-1. The second is the similarity image.

if this does not work with two different size images, then type

compare -metric rmse -dissimilarity-threshold 1 largeimage smallimage resultimages

This works just fine for me:

compare -metric rmse mandril3.png mandril3_156_22.png mandril3_similarity.png
0 (0) @ 156,22

The 156,22 are the coordinates where the top left of the eye image best matches in the mandril image. The 0 (0) means it is a perfect match.

If the two images are the same size, then you only get one images which shows where things are different (like resultimages-0).

My point about the page geometry in the cyclops images was to show that if you extract the difference image, its verbose information shows you where the difference image is located relative to the original image. So that if you get the difference image, you don't need to do compare to find where it is located. The compare process on two different size images would be used if you had two new images that you wanted to find where one best matched in the other. It is not needed if you extract the difference by the process you have used to get your flowerbed.

By the way, the flower bed difference is the same size (I believe) as the original, so there is no point in trying compare on it as it will match at 0,0. You can see this in the flower bed difference image as the page geometry has +0+0 for the offset.

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

Postby minglw » 2010-02-24T13:53:54-07:00

I am still having problems with the compare program.

I am running this version of IM: http://www.imagemagick.org/download/bin ... ws-dll.exe

To verify that I am running the proper "compare", I renamed "compare.exe" to "imcompare.exe".

I tried it on the your cyclops images and it works:

F:\t\test\q3>imcompare -metric rmse cyclops.png cyclops_sep.png output.png
4480.5 (0.068368) @ 66,44


However, I tried it on my two images and it hangs: (it never returns back to the prompt)

F:\t\test\q3>imcompare -metric rmse image1.png image2s.png diff.png
^C
F:\t\test\q3>


In the end, I CTRL+C out of it.

Fred, do you have the same problem with your version of IM compare using the two images (image1.png and image2s.png) I have above ?

User avatar
fmw42
Posts: 15150
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA
Contact:

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

Postby fmw42 » 2010-02-24T16:26:13-07:00

No, it returns very quickly with -dissimilarity-threshold 1 added.

time compare -metric rmse -dissimilarity-threshold 1 image1.png image2s.png image12_comp.png
12643.8 (0.192932) @ 0,30

real 0m1.566s
user 0m1.253s
sys 0m0.062s


And likewise without it.

time compare -metric rmse image1.png image2s.png image12_comp.png
12643.8 (0.192932) @ 0,30

real 0m1.665s
user 0m1.254s
sys 0m0.064s


The two different size image compare method was added to compare in a relatively recent release (6.5.0-9), but may not have stabalized for a few releases after that. Perhaps your IM is too old. What version do you have?

I am using IM 6.5.9-10 Q16 Mac OSX Tiger

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

Postby minglw » 2010-02-24T17:32:30-07:00

Thanks for the info Fred!

I thought when I pasted the link above to the download, one would notice the package I am using.

I am using this package: ImageMagick-6.5.9-10-Q16-windows-dll.exe

It is the latest I can download from this site for Windows.

Perhaps this is a bug in the Windows code ?

Can anyone else running on Windows verify if this a problem ?

User avatar
fmw42
Posts: 15150
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA
Contact:

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

Postby fmw42 » 2010-02-24T19:30:53-07:00

very surprising that it would work for one set of images and not for another. have you rebooted and tried again?

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

Postby minglw » 2010-02-24T21:25:31-07:00

Yes, I've tried after rebooting (twice already) and still the exact same symptom -- it works on your cyclops images but not mine.

User avatar
fmw42
Posts: 15150
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA
Contact:

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

Postby fmw42 » 2010-02-24T21:40:29-07:00

what was your exact command with your images? (note the smaller image must be second)

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

Postby minglw » 2010-02-25T10:13:33-07:00

The cmd is in my post above:

imcompare -metric rmse image1.png image2s.png diff.png

I renamed "compare" to "imcompare" and as such, you see "imcompare" being used instead of "compare".

User avatar
fmw42
Posts: 15150
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA
Contact:

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

Postby fmw42 » 2010-02-25T10:32:45-07:00

try

imcompare -metric rmse -dissimilarity-threshold 1 image1.png image2s.png diff.png

compare.exe -metric rmse -dissimilarity-threshold 1 image1.png image2s.png diff.png


Also try taking a subsection of some other image or a smaller subsection of your current image1 and see if that works. Sometimes compare can take a long time if the two images are both large.

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

Postby minglw » 2010-02-25T11:20:19-07:00

I think you're right Fred. It just took a long time and I didn't wait long enough.

I let it ran and it finally returned.


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 = 1.843 = 00:00:01.843 = 0%
User Time = 1115.531 = 00:18:35.531 = 194%
Process Time = 1117.375 = 00:18:37.375 = 194%
Global Time = 573.957 = 00:09:33.957 = 100%

The timing differences is very different from what you've shown.

User avatar
fmw42
Posts: 15150
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA
Contact:

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

Postby fmw42 » 2010-02-25T11:36:35-07:00

run it two or three times in a row. sometimes the first time it is run, it runs slowly, then afterwards is much faster.

How much memory do you have? Perhaps it is thrashing to disk.

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

Postby minglw » 2010-02-25T11:58:33-07:00

I don't see memory as the issue.
I have 2GB of physical RAM, and 4GB HD set aside for paging. I am using WinXP with service packs 2.

I don't notice any problem with any other program in the system.


Return to “Users”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests