Compare images and calculate distance of objects

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
delooxon
Posts: 2
Joined: 2014-03-02T21:29:52-07:00
Authentication code: 6789

Compare images and calculate distance of objects

Post by delooxon »

Hello, I recently installed imagick for php, I'm trying to figure out how to, after obtaining the difference image from compare(), calculate the distance an object moved.
Ej: I have to images, it's a table with a cup, in picture A the cup is in the bottom right corner, in picture B it's in the top left corner, I've been trying to go through the documentation or find some guides online but the documentation is not helpful at all
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare images and calculate distance of objects

Post by fmw42 »

The coordinates returned tell you how much the one image has shifted relative to another larger image, when you use -subimage-search. If the two images are the same size, there are no shift coordinates returned and you only see the difference in intensity as measured by the metric specified.

See
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/#statistics

An old example can be found at the following, but that syntax now needs -subimage-search to be added.
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076

If you use PHP exec(), you could use some of my correlation scripts at the link below under FFT processing. The one that applies best to same size images is phasecorr. See the very first example on that page. But you need to install the FFTW delegate.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare images and calculate distance of objects

Post by snibgo »

If the camera was on a tripod, the problem is fairly simple. Find the difference between the photos, with a suitable threshold to eliminate small differences. The result is white in the two positions of the cup, and black elsewhere.

Then you can find the coords of the first (top-left) white pixel in each area. To be more sophisticated, morphology distance can find the centre of each area.

If the camera wasn't on a tripod, the problem is much harder.
snibgo's IM pages: im.snibgo.com
delooxon
Posts: 2
Joined: 2014-03-02T21:29:52-07:00
Authentication code: 6789

Re: Compare images and calculate distance of objects

Post by delooxon »

Thank you people, I really appreciate the input, I'll give it a shot and get back to the thread, I was ideally looking for a way to do it all programatically, without having to use exec() because it's for my grade paper.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare images and calculate distance of objects

Post by fmw42 »

If you use snibgo's solution or just compare with -subimage-search for two image that are not the same size, then Imagick should have compatible commands.
Post Reply