Using connectec components

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
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Using connectec components

Post by eleison »

I found out the new function connected components for imagemagick
http://www.imagemagick.org/script/conne ... onents.php

I tried using it on this image:
Image

I want to extract all the "blobs" from the image into separate images.

I tried this:
convert test.jpg -define connected-components:verbose=true -connected-components 4 objects.png

But doesn't seem to work... Any suggestions? :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using connectec components

Post by snibgo »

This command thresholds to black/white, then writes a text list of the components.

Code: Select all

convert testblob.jpg -threshold 50% -define connected-components:verbose=true -connected-components 4 -auto-level null:
The text list is:

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 628x618+0+0 319.1,308.4 350121 srgb(255,255,255)
  3: 354x210+65+220 240.2,321.9 24497 srgb(0,0,0)
  1: 158x56+147+0 211.6,20.5 5629 srgb(0,0,0)
  5: 88x75+429+512 473.7,549.6 4435 srgb(0,0,0)
  4: 38x46+0+362 16.8,384.0 1544 srgb(0,0,0)
  6: 43x37+372+539 392.9,556.4 1299 srgb(0,0,0)
  2: 44x19+354+0 375.7,7.2 579 srgb(0,0,0)
To get one of these components as an isolated image:

Code: Select all

convert testblob.jpg -threshold 50% -crop 354x210+65+220 +repage out1.png
You could repeat this in a script to get them all.
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: Using connectec components

Post by fmw42 »

Your image is not binary even though it looks like it. Try adding -threshold 50% to make it binary. The -gamma will make the graylevels more visible.

Code: Select all

convert test.jpg -threshold 50% -connected-components 4 -gamma 1.5 -auto-level -depth 8 objects.png
That should show 6 objects with different graylevels and the background.

You can also do

Code: Select all

convert test.jpg -threshold 50% -define connected-components:verbose=true -connected-components 4 object2.png
and get the list of objects as

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 628x618+0+0 319.1,308.4 350121 srgb(255,255,255)
  3: 354x210+65+220 240.2,321.9 24497 srgb(0,0,0)
  1: 158x56+147+0 211.6,20.5 5629 srgb(0,0,0)
  5: 88x75+429+512 473.7,549.6 4435 srgb(0,0,0)
  4: 38x46+0+362 16.8,384.0 1544 srgb(0,0,0)
  6: 43x37+372+539 392.9,556.4 1299 srgb(0,0,0)
  2: 44x19+354+0 375.7,7.2 579 srgb(0,0,0)
You can use the object id to extract each one separately. Compute each id into a graylevel as percent. For example

Code: Select all

gray=3
pct=`convert xc: -format "%[fx:100*$gray/65535]" info:`
convert object2.png -depth 16 -precision 15 -fill black +opaque "gray($pct%)" -fill white +opaque black object3.png
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Re: Using connectec components

Post by eleison »

Thanks, I tried using -monochrome. But -threshold 50% works much better, monochrome made some noise... Thanks for great support :)
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Re: Using connectec components

Post by eleison »

If I want to only get the larger blobs in this image, how do I use this:

Code: Select all

convert objects.gif -define connected-components:area-threshold=410 -connected-components 4 \
  -auto-level objects.jpg
? Can't get it to work on my test image...
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Re: Using connectec components

Post by eleison »

Ok, got it to work.

Code: Select all

convert test.jpg -threshold 50% -define connected-components:area-threshold=5510 -define connected-components:verbose=true  -connected-components 4 -auto-level testout.jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using connectec components

Post by fmw42 »

See my modified message above.
PetRoo
Posts: 5
Joined: 2010-12-15T09:07:17-07:00
Authentication code: 8675308

Re: Using connectec components

Post by PetRoo »

snibgo wrote:This command thresholds to black/white, then writes a text list of the components.

Code: Select all

convert testblob.jpg -threshold 50% -define connected-components:verbose=true -connected-components 4 -auto-level null:
...
Hmm - I am after a similar functionality and the new connected-components functionality is really great, but using

Code: Select all

convert DSCN2258.JPG -fuzz 5% -threshold 76% -define connected-components:verbose=true -define connected-components:area-threshold=35271 -connected-components 4 null:
for a real-life (non-binary) image I get back the following lines:

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 2592x1944+0+0 1107.7,1124.7 3668601 srgb(21,21,21)
  13: 2542x1440+49+0 1718.5,387.7 987843 srgb(247,247,247)
  73: 594x858+1998+0 2416.6,213.4 156252 srgb(27,27,27)
  31734: 579x286+1876+1658 2171.9,1823.2 127362 srgb(243,243,243)
  20271: 492x559+653+1016 953.6,1277.9 58337 srgb(240,240,240)
  19897: 393x444+1272+987 1400.2,1137.2 40345 srgb(252,252,252)
  18496: 15x8+2553+864 2560.8,866.9 49 srgb(177,177,177)
  33938: 8x6+1890+1879 1894.1,1880.7 10 srgb(255,255,255)
  707: 4x3+2023+11 2024.3,12.3 9 srgb(170,170,170)
  19197: 9x2+2532+925 2536.8,925.8 6 srgb(43,43,43)
  28223: 2x4+2421+1382 2421.5,1383.5 6 srgb(255,255,255)
  27638: 1x6+2394+1356 2394.0,1358.8 5 srgb(255,255,255)
  18477: 6x1+2560+865 2562.0,865.0 4 srgb(0,0,0)
  19483: 4x1+2545+950 2546.5,950.0 4 srgb(255,255,255)
  19793: 2x3+2548+975 2548.2,976.2 4 srgb(0,0,0)
  25168: 3x2+2497+1233 2498.2,1233.8 4 srgb(0,0,0)
  20319: 2x1+699+1018 699.5,1018.0 2 srgb(255,255,255)
  25971: 2x1+2462+1275 2462.5,1275.0 2 srgb(128,128,128)
  12: 1x1+92+0 92.0,0.0 1 srgb(255,255,255)
  60: 1x1+2052+0 2052.0,0.0 1 srgb(0,0,0)
  27242: 1x1+2441+1336 2441.0,1336.0 1 srgb(255,255,255)
What I don't understand:

# Why are areas reported that are definitely smaller than the value given by my "define connected-components:area-threshold=35271"?
# Why do a get back srgb[,,] values that are definitely not "white" or "black", even though I define a certain threshold value to induce binarization?

I'm using the freshly downloaded IM 6.9.0-5.

Cheers,
Petra
Last edited by PetRoo on 2015-02-12T13:28:58-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using connectec components

Post by fmw42 »

Please provide your color image so we can test your command. The srgb colors are all gray. Try taking out the -fuzz 5%. It is not usually used with -threshold if you want a binary image.
PetRoo
Posts: 5
Joined: 2010-12-15T09:07:17-07:00
Authentication code: 8675308

Re: Using connectec components

Post by PetRoo »

fmw42 wrote:Please provide your color image so we can test your command.
The original image is now included in my former post and here as a link.
The srgb colors are all gray. Try taking out the -fuzz 5%. It is not usually used with -threshold if you want a binary image.
Well, three of them went right to srgb(255,255,255), so I consider them as white. But the rest is varying shades of grey, indeed. :D Taking out the -fuzz 5% did not change a thing in the resultant output, though.

Cheers,
Petra
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using connectec components

Post by fmw42 »

try this and look at the images

convert DSCN2258.JPG -threshold 50% tmp1.png

convert tmp1.png -define connected-components:verbose=true -connected-components 4 null:

convert tmp1.png -define connected-components:area-threshold=115648 -connected-components 4 -auto-level result.png


The issue here is that either the white pretzels are being merged into the large black background or the black regions inside the white pretzels are being merged into the white pretzels.


If you want to extract the pretzels, then you need to either crop the ones you want, since they are top 6 after the background white and background black. Or convert the id to percent grayscale and use

convert CCLimage -fill black +opaque "gray(XX%)" -fill white +opaque black result
Post Reply