Match Then Crop

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?".
Post Reply
afre
Posts: 57
Joined: 2014-01-22T15:02:53-07:00
Authentication code: 6789

Match Then Crop

Post by afre »

Hello All, I used a script to detect and crop a set of objects, but it isn't color managed and resizes the input. I would like to take the output, find their location in the originals and then crop to get a more accurate set. Unsure whether that's the best approach but it's a start.

I've tried using this but it doesn't work. (Maybe my test original is too large.) fmw42 also has a bunch of scripts, DOTPRODUCTCORR NORMCROSSCORR PHASECORR RMSECORR, but I don't know which one is suitable for the task. I am sure that each has it's application.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Match Then Crop

Post by fmw42 »

Please always provide your IM version and platform.

IM has the compare function, which can be very slow on large images. The syntax is:

Code: Select all

compare -metric rmse -subimage-search largeimage smallimage null:
You can choose another metric if you want, but I typically use rmse. The output will provide the match error score (smaller is better) and the top left (offset) coordinates in the large image where to start your crop of the same size as the small image.

But this function is neither scale nor rotationally invariant. There must only be a shift difference between the two (though the brightness/contrast may be somewhat different).

If on unix, you may try my script such as normcrosscorr, but then you must compile IM in HDRI mode and have installed FFTW delegate library.

See the following for examples, but in the first, you will need to add -subimage-search, since that has been added since the example was created.

viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076
http://www.fmwconcepts.com/imagemagick/ ... mcrosscorr
afre
Posts: 57
Joined: 2014-01-22T15:02:53-07:00
Authentication code: 6789

Re: Match Then Crop

Post by afre »

fmw42 wrote:But this function is neither scale nor rotationally invariant.
Are your scripts invariant as well? Maybe I'll try something else (and smaller images). Thanks anyway.
ImageMagick 7.0.7-25 Q16 x64 2018-03-04 · Cipher DPC HDRI Modules OpenMP · Windows 7
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Match Then Crop

Post by fmw42 »

My scripts have the same limitation -- not scale or rotationally invariant. But they run much faster than compare.

Perhaps you should post an example pair of images (not huge ones), so we can see what you are trying to do. You can upload them to someplace such as dropbox.com and put the URLs here.

For rotational invariance, perhaps see viewtopic.php?f=2&t=26530
Post Reply