annotate white region of image

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
pcecil

annotate white region of image

Post by pcecil »

i have to identify the white region of a black and white image and annotate it.. for this, first i have to identify a white region of given minimum area in the image.. how should this be done/ what api should be used? your help is appreciated!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: annotate white region of image

Post by fmw42 »

build a white image of the minimum size you want. then use the compare function to do a similarity match against the black and white image. specify a similarity resulting image which will have two components (-0 and -1). the second component (-1) will be the similarity score image. the brightest pixel in that image will be the upper left corner of black/white image where the white region best matches. every place that is fully white in the similarity image will correspond to the upper left corner of a perfectly white area of the minimum size.

see the compare example with mandril and its small eye image at viewtopic.php?f=1&t=14613&hilit=compare+rmse

replace the eye image with the minimum size white image and the mandril with the black and white image.


see http://www.imagemagick.org/script/compare.php

see also http://www.imagemagick.org/script/comma ... -threshold as you may need to increase the default dissimilarity-threshold as you are comparing white to black areas which will be very dissimilar. If you need to use this you will get a message saying the images are too dissimilar.

This seems to work:

Image

#create 100x100 pixel white image for comparison in the compare command

compare -metric rmse logo2.png -size 100x100 xc:white logo2_white_similarity.png
0 (0) @ 0,102

This says the first perfect match (rmse value =0) is at pixel 0,102

Here is the similarity image: logo2_white_similarity-1.png
Image

The fully white area in the lower left corner corresponds to an area in the logo2.png image that is larger than 100x100 and completely white. So there is some room in the lower left corner of the logo2.png image to shift a 100x100 window around and still have it contain only white pixels.

If we threshold this image, then we see all the possible match coordinates for the 100x100 white area.

convert logo2_white_similarity-1.png -negate -threshold 0 -negate logo2_white_similarity-1_threshold.png
Image

The coordinate of every white pixel here will correspond to the upper left corner of the 100x100 white image placement in the logo2.png image where it could be placed and match perfectly. That is a 100x100 window placed with its upper left corner at each of these coordinates in the logo2.png image would contain only white pixels.

The image below shows a black square of size 100x100 drawn starting with its upper left corner at the (first) match point identified above at 0,102

convert logo2.png -fill none -stroke black -draw "rectangle 0,102 99,201" logo2_square.png
Image

So you can see there is some room to move the black outline around and still contain only white pixels. This is why the lower left area of the similarity score image is fully white.

With regard to APIs, I cannot really advise much. I do not know if this new similarity feature of compare is built into any of them. The API developers would have to comment.

However, you can do this either manually with the command line as above (or with a script) or putting the command lines in the exec function of PHP. For more details about using exec in PHP with IM, see http://www.rubblewebs.co.uk/imagemagick/
Last edited by fmw42 on 2009-09-16T08:22:35-07:00, edited 1 time in total.
pcecil

Re: annotate white region of image

Post by pcecil »

thank you very much for your insightful reply.. will dig into the code and report on the progress!
pcecil

Re: annotate white region of image

Post by pcecil »

Sadly, the compare tool is taking far too much time and resources on my machine to compare against a 100x100 sub-image than I anticipated ( more than 5 mins ) on a 200 kb tiff image, as anthony had suggested in the other referenced similar post. :)

Is this normal/expected? My machine spec is 1.73 Ghz, 2 GB RAM.

Do you have any idea when the FFT additions by Fred Weinhaus is getting released?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: annotate white region of image

Post by fmw42 »

Do you have any idea when the FFT additions by Fred Weinhaus is getting released?
The FFT code in IM has been supported now since IM 6.5.4.7 (but best after 6.5.4.9). See all my demonstrations and examples and tutorial at http://www.fmwconcepts.com/imagemagick/ ... urier.html and in particular normcrosscorr script for doing something similar to compare. You will need to install the FFTW delegate library to use the IM FFT functions.

see http://www.imagemagick.org/script/comma ... ns.php#fft and http://www.imagemagick.org/script/comma ... ns.php#ift

But to do normcrosscorr (image matching) you will need to be in IM HDRI (recommend Q16) compilation.

P.S. What are the pixel dimensions of your tiff image? Can you post a link to an example?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: annotate white region of image

Post by fmw42 »

Here is another idea to speed things up. Do it in a hierarchical fashion. First scale your image down to 10%. Then run the compare with a 10x10 white image. Once you find the match point. Crop a liberal subsection from your original image at the corresponding coordinates. Then run the compare at full 100x100 white image against the subsection.

For example using the logo2.png image (320x240) the following run time is achieved:

time compare -metric rmse logo2.png -size 100x100 xc:white logo2_white_similarity.png
0 (0) @ 0,102

real 0m30.106s
user 0m24.727s
sys 0m0.196s

This takes about 30s.

Now if I scale the logo2.png image to 10% of its original size
convert logo2.png -scale 10% logo2_10pct.png

Then do a compare with a 10x10 image,
time compare -metric rmse logo2_10pct.png -size 10x10 xc:white logo2_10pct_white_similarity.png
0 (0) @ 0,11

real 0m0.101s
user 0m0.051s
sys 0m0.027s


This takes only 0.1s

So taking the results of the match coord of 0,11 and multiplying by 10 we get 0,110 vs the original 0,102. But if you subsection (crop) the original image liberally around 0,110 and do the compare at full resolution only on this subsection it won't take so long.
pcecil

Re: annotate white region of image

Post by pcecil »

you are very innovative..this works! thank you very much!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: annotate white region of image

Post by fmw42 »

many years of doing image processing and a topic of interest to me. glad it helped
Post Reply