Object identification in an 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
lego
Posts: 9
Joined: 2010-04-13T03:43:09-07:00
Authentication code: 8675308

Object identification in an image

Post by lego »

Hi,

I apologize since I have no idea of image processing, but I need to extract information from some images... let's see if someone can help me.

I have thousands of images similar to this one:
Image

This image is scaled down by 25%, the real images have a resolution of 2048x2048 and are stored in TIF format. This is just to show you how they look like.
(in all these images, the moving parts are uniquely the bubbles and the tiny bright particles. The tubes are always in the same place).
The thing is that I need to make a bash script (using convert and other imagemagick or any open source linux tool if necessary) that automatically identifies what is a bubble a what not, count the number of bubbles, and inform about the pixel position of where the bubbles are placed in the image (bubbles inside the tube are not important for me, only the bubbles outside the tube).

--- Another useful information would be the bubble size and shape eccentricity, but this is not so important for now. With all this information, I could make a script to compute the velocity vectors of the bubbles, the tracking of each bubble to reconstruct the trajectory and lots of another useful data. ---

I supose that first I need to remove the "noise" (those tiny particles), and then apply some kind of contrast or alpha channel modification, then mask or something to only keep the bubbles and remove everything else.. but I have no idea on how to start!

Any ideas would be much appreciated!

Thank you,
Lego.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Object identification in an image

Post by snibgo »

Morphology could be useful here, but I'm not an expert on that. See http://www.imagemagick.org/Usage/morphology/

Ignoring the problem of the tubes, which are easy to mask out, the following Windows (adjust as required for Unix) script does a reasonable job of isolating the bubbles, although it misses a couple:

Code: Select all

"%IMG%convert" ^
  mt6nft.jpg ^
  -gaussian-blur 12x4 ^
  ( +clone -blur 6x2 ) ^
  -compose MinusSrc -composite ^
  -auto-level ^
  -level 17x100%% ^
  justBubbles.jpg
Morphology could erode each bubble to a single pixel, and the image can be output in a text format to give the location of each bubble.

Another point: the image has a background variation of dark vertical streaks, which help to obscure the bubbles. If this variation is constant across your images, it would be very useful to create a mask to eliminate this variation.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Object identification in an image

Post by fmw42 »

I would add to what user snibgo said, by simply thresholding his result. In unix syntax:


convert \
1mt6nft.jpg \
-gaussian-blur 12x4 \
\( +clone -blur 6x2 \) \
-compose MinusSrc -composite \
-auto-level \
-level 17x100% -threshold 1% \
justBubbles.gif

You can mask out your pipes since you know where they are located in each image.

Then you can possibly use my script, separate, to get each individual bubble. see link below

Then you can get its area by computing the number of white pixels in each separated image.

area=`convert image -format "%[fx:mean*h*w]" info:`

More statistics are available from the -verbose information, such as min, max, mean, std, skew and kurtosis.

And other feature measures are at http://www.imagemagick.org/script/comma ... p#features

Other typical features are central moments. See
http://en.wikipedia.org/wiki/Image_moments
http://en.wikipedia.org/wiki/Moment_(mathematics)

See also
viewtopic.php?f=1&t=18968#p73803

However, once you have the thresholded image, you may find more direct tools for blob analysis using OpenCV. see
http://en.wikipedia.org/wiki/OpenCV
http://opencv.org/
http://docs.opencv.org/
http://docs.opencv.org/modules/imgproc/ ... 20analysis
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Object identification in an image

Post by snibgo »

The bubbles, all the bubbles, and nothing but the bubbles (Windows script):

Code: Select all

"%IMG%convert" ^
  -size 512x512 xc:None ^
  -fill black ^
  -draw "rectangle 0,356 511,511" ^
  -draw "rectangle 0,285 40,355" ^
  -draw "rectangle 487,286 511,355" ^
  -draw "polygon 0,0 25,0 0,92" ^
  mApp.png

"%IMG%convert" ^
  mt6nft.jpg ^
  -adaptive-blur 12x4 ^
  -write m1.jpg ^
  ( +clone -blur 6x2 ^
    -write m2.jpg ^
  ) ^
  -compose MinusSrc -composite ^
  -write m3.jpg ^
  -auto-level ^
  -write m4.jpg ^
  -level 12.5x100%% ^
  -write m5.jpg ^
  -threshold 1%% ^
  -write m6.jpg ^
  mApp.png ^
  -compose Over -composite ^
  justBubbles.jpg
mApp.png is a mask for the apparatus.

The "-write m?.png" lines allow you to see the intermediate images so you can tune the parameters for a range of files; remove them before processing the entire batch. If this doesn't work for all the images, a photograph of the apparatus with no bubbles would allow the background to be removed, which might help.

Push the results through Fred's "separate" script, and away you go.
snibgo's IM pages: im.snibgo.com
lego
Posts: 9
Joined: 2010-04-13T03:43:09-07:00
Authentication code: 8675308

Re: Object identification in an image

Post by lego »

THANK YOU so much to all of you!!

This script works very good for the image I posted... and the procedure it uses would have never occured to me (maybe because I don't know anything about image processing) however, if I have a slightly different image, it doesn't seem to work: I attach an example of an image in which the script doesn't work: Image

I have tried to change all the values of the blurs, level, threshold... it is impossible for me to get the bubbles in this new image! I also tried the -despeckle option (which seems to help a lot), and many other options, but it is impossible for me to get it work!!

It is surprising for me that you responded so quick to my first post, with excelent results for the first image I posted, but why is so difficult for me to get it work in the second image??

EDIT: it seems that tinypic doesn't work, and the image in the first post is not available. Anybody can recomend me some procedure to upload images to this forum?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Object identification in an image

Post by fmw42 »

EDIT: it seems that tinypic doesn't work, and the image in the first post is not available. Anybody can recomend me some procedure to upload images to this forum?
You have to post your image to some free image hosting web site and then put a link here to that image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Object identification in an image

Post by snibgo »

Compared to the OP image, tubes have changed position (trivial to correct) and the lighting has changed, so I would expect the script not to work entirely. But it isn't too bad. If you visually compare m4.jpg with the recently posted XKcOC.png, m4.jpg has all the bubbles and has eliminated most of the small particles. m5.jpg has then eliminated all the particles, but created one small problem: it has split one of the bubbles into two.

So, visually, m5.jpg is almost correct. But m6.jpg has "restored" many small particles. From the script, you can see that the only operation between the two images is "-threshold 1%". So the problem is that this threshold is too low: these particles are visually black but must be above the 1% threshold.

Using Gimp to examine m5.jpg, I see the darkest bubble is 24%. I could spend more time to find the lightest particle, but let's just try a threshold of 20%.

Code: Select all

"%IMG%convert" ^
  XJcOC.png ^
  -adaptive-blur 12x4 ^
  -write m1.jpg ^
  ( +clone -blur 6x2 ^
    -write m2.jpg ^
  ) ^
  -compose MinusSrc -composite ^
  -write m3.jpg ^
  -auto-level ^
  -write m4.jpg ^
  -level 12.5x100%% ^
  -write m5.jpg ^
  -threshold 20%% ^
  -write m6.jpg ^
  mApp.png ^
  -compose Over -composite ^
  justBubbles2.jpg
Yeah, that's excellent apart from false positives at the top of the image (which might justifiably be masked out) and the split bubble. Unfortunately, using this larger threshold has shrunk the apparent size of the bubbles in justBubbles2.jpg which means the results can be used to get the positions of the bubbles but not their size or shape.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Object identification in an image

Post by snibgo »

Rather than "-threshold", it's better to modify "-level". This one is rather sensitive: too low and we mistake particles for bubbles; too high and at best we shrink the bubbles, at worst we eliminate them altogether. For any image there is one optimal value. The two supplied images need different values.

So we automatically find a lower bound for the level parameter by finding the lightest pixel ("MAXPIX") within an area we are certain contains no bubbles ("NULL_AREA"). It is a lower bound because there is a good chance a brighter particle exists outside the NULL_AREA. We should find MAXPIX only once per series of images. If we calculate it for every image, an unusually high value will cause "-level" to rise, which will shrink bubbles.

The final "-morphology" removes any particles that were brighter than any in NULL-AREA. Sadly, it also slightly changes the shape of bubbles. If you are very fussy about the bubble shapes, you might dilate the bubbles in the outputs and use them as a mask for the original image, chopped up by Fred's script, then manipulate these small images.

Windows script:

Code: Select all

"%IMG%convert" ^
  -size 512x512 xc:None ^
  -fill black ^
  -draw "rectangle 0,356 511,511" ^
  -draw "rectangle 0,283 40,355" ^
  -draw "rectangle 487,285 511,355" ^
  -draw "polygon 0,0 25,0 0,92" ^
  mApp1.png

"%IMG%convert" ^
  -size 512x512 xc:None ^
  -fill black ^
  -draw "rectangle 0,359 511,511" ^
  -draw "rectangle 0,288 226,358" ^
  -draw "rectangle 487,288 511,358" ^
  -draw "polygon 0,0 29,0 0,118" ^
  mApp2.png

call :CalcPic mt6nft.jpg 200x300+150+50  mApp1.png justBubbles1.jpg
call :CalcPic XJcOC.png  170x170+270+200 mApp2.png justBubbles2.jpg

goto :EOF


:CalcPic

rem Call with:
rem   %1 input file
rem   %2 null_area
rem   %3 apparatus mask file
rem   %4 output file

"%IMG%convert" ^
  %1 ^
  -adaptive-blur 12x4 ^
  -write m1.jpg ^
  ( +clone -blur 6x2 ^
    -write m2.jpg ^
  ) ^
  -compose MinusSrc -composite ^
  -write m3.jpg ^
  -auto-level ^
  m4.png

rem What's the maximum pixel in m4, in an area that cannot contain bubbles?
rem Give the answer as a percentage.

for /F %%i in ('%IM%convert ^
  m4.png ^
  -crop %2 ^
  -format "%%[fx:maxima*100]" ^
  info:-' ) do set MAXPIX=%%i

"%IMG%convert" ^
  m4.png ^
  -level %MAXPIX%x100%% ^
  -write m5.jpg ^
  -threshold 1%% ^
  -write m6.jpg ^
  -morphology Open:2 Diamond ^
  +repage ^
  -write m7.jpg ^
  %3 ^
  -compose Over -composite ^
  %4

goto :EOF
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Object identification in an image

Post by fmw42 »

This may help as a preprocessing step to do background subtraction and noise cleaning.

The background is determined by averaging the image down to one row (since the variation seems to be mostly horizontal) and then scaling up to original size. Then subtract from the image.

The noise cleaning seems to work reasonsbly well by using a sufficient number of repeated despeckles. Note I have a script, noisecleaner, which will iterate for a given number of iterations or stop at some minumum difference between successive image means. Use method=smooth for -despeckle and method=denoise for -enhance. In this kind of image, method=denoise does not work well. But method=smooth using -despeckle works rather well.

Note that this script is a unix shell script so on Windows would require Cygwin. But someone expert in Windows could probably do the looping and comparing in Windows directly or with a BAT file.

First image earlier above:

convert mt6nft.jpg \
\( -clone 0 -scale 512x1! -blur 0x1 -scale 512x512! \) \
+swap -compose minus -composite -auto-level -despeckle -despeckle show:


Second image provided above:

convert XJcOC.png \
\( -clone 0 -scale 512x1! -blur 0x1 -scale 512x512! \) \
+swap -compose minus -composite -auto-level \
-despeckle -despeckle -despeckle -despeckle -despeckle -despeckle -despeckle -despeckle show:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Object identification in an image

Post by fmw42 »

Sorry this is unix and uses my fuzzythreshold script. You can see the intermediate images via the -write show: commands

Using the last image provided above. I did not mask out the tubes.

Image

convert XJcOC.png \
\( -clone 0 -scale 512x1! -blur 0x1 -scale 512x512! \) \
+swap -compose minus -composite -auto-level \
-despeckle -despeckle -despeckle -despeckle -despeckle -despeckle -despeckle -despeckle -write show: tmp1.png
fuzzythresh -r 10 tmp1.png tmp2.png
convert tmp2.png -write show: -statistic median 3x3 -statistic median 3x3 -write show: \
-morphology close octagon:3 XJcOC_proc.png


Image
Post Reply