Using connected components tool

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?".
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Using connected components tool

Post by tmelorc »

Version: ImageMagick 7.0.3-1 Q16 x86_64 2016-11-28
Hello. This is my first message here.

I'm trying to learn about the connected components feature. I created using GIMP a PNG image with transparent background and I draw 4 disjoint full black rectangles. I converted the image to 1 bit colour pallet.

Here is the original image: https://dl.dropboxusercontent.com/u/42709342/molde.png

Here is the output of histogram:

Code: Select all

convert molde.png -format %c histogram:info:histo
   8379293: (  0,  0,  0,  0) #00000000 none
    320547: (  0,  0,  0,255) #000000FF black
Then I tried to follow the example from http://www.imagemagick.org/script/conne ... onents.php to compute the components. I supposed that the result should be 4 components.

But after

Code: Select all

convert molde.png -connected-components 4 -auto-level -depth 8 -define connected-components:verbose=true objects.png
I just got a full black image and no info on components from the verbose option.

So, how to use this feature? Is not possible to use it with binary images?

Thanks in advance and regards.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using connected components tool

Post by fmw42 »

You have 4 black objects in a transparent background. Transparent has a black background under the transparency. I am not sure CCL can process transparency. Try with black on white background. What is it you want removed. You have to process once to see the list of objects. Then process again to remove objects smaller than some area. See the example page.

If you tell me what you want to remove, then perhaps I can test it and show you.

What is your platform?
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Using connected components tool

Post by tmelorc »

Thanks so much for information. I didn't know on Transparent has a black background under the transparency. Nice!

Well, in fact, that transparent image was only to study the tools. My images are JPG images fully coloured.

You can get it here: https://dl.dropboxusercontent.com/u/427 ... iginal.jpg

I want to split only the leaves in files. But other images could contain more than 3.

So I tried:

Code: Select all

convert f13original.jpg -threshold 70% BP.png
convert BP.png -morphology Close Octagon BP.png
Output: https://dl.dropboxusercontent.com/u/427 ... iginal.png

Then, I tried to deal with CCL.

Code: Select all

convert BP.png  -negate  -define connected-components:area-threshold=40000  -define connected-components:verbose=true  -connected-components 8   tmp.png >  data.cc
Output: https://dl.dropboxusercontent.com/u/42709342/tmp fully black image with verbose:

Code: Select all

  0: 1613x2340+0+0 819.8,1076.4 2952642 gray(0)
  77: 1073x500+60+1262 674.7,1522.5 338184 gray(255)
  4: 136x2340+1564+0 1648.8,1195.1 236678 gray(255)
  81: 835x428+355+1780 843.4,2005.6 235000 gray(255)
  72: 817x406+186+747 660.1,951.4 215496 gray(255)
After, I use a loop over the id's to split the image (for example, for 72 the code would be):

Code: Select all

convert tmp.png -define connected-components:verbose=false -define connected-components:keep=72 -connected-components 4   layer72.png
(Rem.) Only for id 0 and 4 the result was as expected.

- Id 0 produced the complement of leaves and the right vertical strip.
- Id 4 contains the bottom leaf.

For 72, 77, 81, the image looks like empty even the area is big enough! For example, for id 72, here is the output: https://dl.dropboxusercontent.com/u/427 ... %3Atmp.png

Thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using connected components tool

Post by fmw42 »

try this. You have to pick which objects are the correct ones by looking at their bounding boxes visually and deciding. If you did not have the broad stripe on the right, you could have just used area thresholding to remove all parts smaller than the 3 leaves.

Unix syntax. If on Window replace \ with ^

# same as yours but I used smooth in place of close to try to remove small spots of either black or white

Code: Select all

convert f13original.jpg -threshold 70% -morphology smooth Octagon -negate BP.png
# I used a smaller area threshold to keep the higher area ones to be sure I got your right ids
# I did not negate the image, since I wanted a final mask that was white on black background
# I sent the output to null: (meaning no output)

Code: Select all

convert BP.png \
-define connected-components:verbose=true  \
-define connected-components:area-threshold=1000 \
-connected-components 8 null:
# here ids 68,60,65 are the leaves and 9 is the vertical strip on the right side of the image

Code: Select all

  0: 1612x2340+0+0 815.3,1074.3 2902323 gray(0)
  68: 1074x500+60+1262 674.9,1522.4 338407 gray(255)
  9: 138x2340+1562+0 1648.6,1194.5 237477 gray(255)
  69: 837x429+353+1779 843.6,2005.4 235336 gray(255)
  65: 825x406+181+747 660.3,951.3 215784 gray(255)
  32: 118x1534+1256+456 1309.8,1166.7 39232 gray(255)
  67: 9x1162+0+1178 2.1,1851.0 5619 gray(255)
  12: 247x61+90+335 222.9,358.2 1867 gray(255)
  64: 245x35+59+644 180.5,662.2 1534 gray(255)
  61: 33x30+140+598 155.4,612.0 421 gray(0)
# here I had to keep the area threshold to get the same ids
# also had to specify mean-color=true so that result was not coded with the id as graylevel
# note that mean-color=true makes all other areas transparent, so I had to make transparent black

Code: Select all

convert BP\.png \
-define connected-components:area-threshold=1000 \
-define connected-components:keep="0,68,69,65" \
-define connected-components:mean-color=true \
-connected-components 8 \
-background black -alpha background -alpha off \
mask.png
# here I put the mask image into the alpha channel of your original image so that you have the original colors

Code: Select all

convert f13original.jpg mask.png -alpha off -compose copy_opacity -composite result.png
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Using connected components tool

Post by tmelorc »

Thanks so much for all explanations and effort. Let me study what you wrote. Regards.

ps: do you think that IM version or some other programs installed on my computer could change the results comparing with yours? I didn't get the layer

Code: Select all

  61: 33x30+140+598 155.4,612.0 421 gray(0)
I'm using

Code: Select all

Linux tmelorc 3.13.0-101-generic #148-Ubuntu SMP Thu Oct 20 22:08:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Version: ImageMagick 7.0.3-1 Q16 x86_64 2016-11-28
java version "1.7.0_121"
OpenJDK Runtime Environment (IcedTea 2.6.8) (7u121-2.6.8-1ubuntu0.14.04.1)
OpenJDK 64-Bit Server VM (build 24.121-b00, mixed mode)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using connected components tool

Post by fmw42 »

What do yo mean you did not get the layer? Are you saying that the mask.png file did not get created or that it did not include id=61. If the latter, did you include that in the list of ids? If so, not that it is black. So you would not see it, because all other ids were make transparent and then I converted them to black.
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Using connected components tool

Post by tmelorc »

Your output showed 61: 33x30+140+598 155.4,612.0 421 gray(0) but not mine. I did the same commands as you did, but no id 61 in the verbose output.

Since I'll try to run the script on thousands of images, I'm trying to find a way to avoid manual adjustments to choose the id to keep.

I think that some use of bounding boxes could help.

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

Re: Using connected components tool

Post by fmw42 »

You have several choices.

1) choose ids
2) use area threshold to remove everything below some size -- that is the usual way. If you photographed without the side bar, then that would work. So if you always have a sidebar, then crop the images first to remove the side bar.
3) filter on the bounding boxes or centers, but you will have to write a script to do that. There is no internal filter for that at this time.
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Using connected components tool

Post by tmelorc »

Thanks for your help. You are confirming the ideas I have in mind. I'm working on some scripts. After some area threshold, I am able to reduce to 5 or 10 ids. Now I have to decide how to filter more. The centers is a good option since the leaves usually have centers almost in the center of image.

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

Re: Using connected components tool

Post by fmw42 »

They are centroids, not the same as centers of the bounding box. But you could try either.

see https://en.wikipedia.org/wiki/Centroid
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Using connected components tool

Post by tmelorc »

Ow, nice. Thanks.

What is strange is that I'm using 40000 for the area feature but after running the script on 100 images I got a lot of times some components with very smal area. For example,

Code: Select all

  23: 32x30+80+1189 95.4,1203.8 420 gray(255)
Using the draw rectangle options I could see that those small components come from the text labels. For example, interior of O letter.

Do you know if there is some problem within the connected tool regarding small areas?

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

Re: Using connected components tool

Post by fmw42 »

Post an example image and your exact command, so we can test it out to see what is happening.
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Using connected components tool

Post by tmelorc »

Here is the original image: https://www.dropbox.com/s/frmfjv3wawxp4 ... 4.jpg?dl=0

The commands:

Code: Select all

convert f3_f4.jpg -threshold 70% out.png
convert out.png -morphology smooth Octagon out.png 
convert out.png -shave 10x10 -bordercolor white -border 10x10 out.png 
convert out.png -gravity east -chop 130x0 -background white -gravity east -splice 130x0 out.png 
convert out.png -define connected-components:verbose=true -define connected-components:area-threshold=40000 -define connected-components:keep=0   -connected-components 4  0_out.png 
Output:

Code: Select all

  0: 1700x2340+0+0 834.7,1175.6 3133346 gray(255)
  15: 657x410+271+490 613.9,689.7 174785 gray(0)
  12: 548x378+826+307 1124.7,476.3 142044 gray(0)
  66: 509x382+351+1668 609.7,1858.3 135930 gray(0)
  19: 551x350+860+736 1159.6,923.1 135762 gray(0)
  71: 437x331+775+1871 1011.5,2043.3 108776 gray(0)
  65: 467x317+830+1466 1076.7,1630.1 105779 gray(0)
  9: 1479x138+51+128 794.2,176.7 41162 gray(0)
  31: 33x29+65+1194 80.9,1207.8 416 gray(255)
Output image with component 0: https://www.dropbox.com/s/26hsf14kalyoq ... t.png?dl=0

Observe the 31: with area 416. I used that white borders to make sure that 0: component is the complement of leaves and others, I mean, to avoid 0: containing some dust from the top left corner.

Also I observed that those small area component is gray(255) (the same for 0:).

Thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using connected components tool

Post by fmw42 »

You are using the commands slightly wrong.

Your first commands are correct:

Code: Select all

convert f3_f4.jpg -threshold 70% out.png
convert out.png -morphology smooth Octagon out.png 
convert out.png -shave 10x10 -bordercolor white -border 10x10 out.png 
convert out.png -gravity east -chop 130x0 -background white -gravity east -splice 130x0 out.png 
But you should combine them into one long command for efficiency. If you kept them separate to review each step it would have been better to name them out1, out2 ...

One Command Equivalent:

Code: Select all

convert f3_f4.jpg -threshold 70% \
-morphology smooth Octagon \
-shave 10x10 -bordercolor white -border 10x10 \
-gravity east -chop 130x0 -background white -gravity east -splice 130x0 out.png 
Next you should have negated the image so it was white on black background, so that it creates a proper polarity mask.

Code: Select all

convert out.png -negate out.png 
Then you need to use a slightly different -connected-components command to create a mask. Note I have added
-define connected-components:mean-color=true, so that it makes the out colors the same as out.png and not colored with graylevels equal to the ids. You do not want to use -define connected-components:keep=0; otherwise, you would only have id 0 and the rest of the image would be transparent. You want the image to be white and black only for a mask

Code: Select all

convert out.png \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=40000 \
-define connected-components:verbose=true \
-connected-components 4 mask.png


The report strangely shows a small area (416) id (31), that should not be listed. Seems like a bug to me.

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 1700x2340+0+0 834.7,1175.6 3133346 gray(0)
  15: 657x410+271+490 613.9,689.7 174785 gray(255)
  12: 548x378+826+307 1124.7,476.3 142044 gray(255)
  66: 509x382+351+1668 609.7,1858.3 135930 gray(255)
  19: 551x350+860+736 1159.6,923.1 135762 gray(255)
  71: 437x331+775+1871 1011.5,2043.3 108776 gray(255)
  65: 467x317+830+1466 1076.7,1630.1 105779 gray(255)
  9: 1479x138+51+128 794.2,176.7 41162 gray(255)
  31: 33x29+65+1194 80.9,1207.8 416 gray(0)
However it is really gone and you can see that from a check of the mask areas:

Code: Select all

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

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 1700x2340+0+0 834.6,1175.6 3133762 gray(0)
  3: 657x410+271+490 613.9,689.7 174785 gray(255)
  2: 548x378+826+307 1124.7,476.3 142044 gray(255)
  6: 509x382+351+1668 609.7,1858.3 135930 gray(255)
  4: 551x350+860+736 1159.6,923.1 135762 gray(255)
  7: 437x331+775+1871 1011.5,2043.3 108776 gray(255)
  5: 467x317+830+1466 1076.7,1630.1 105779 gray(255)
  1: 1479x138+51+128 794.2,176.7 41162 gray(255)
No area 416 in the above.

Next you use the mask to get your final image.

Code: Select all

convert f3_f4.jpg mask.png -alpha off -compose copy_opacity -composite result.png
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Using connected components tool

Post by tmelorc »

Dear fmw42, thanks a lot for all your effort. I will study it. But let me tell you that my final goal is to create one file for each leaf, black with white background. So I don't need to use the mask tool to keep the color. Maybe I didn't explained well.

I'll study the contour of leaves.

So, your suggestion is to take the 0: layer and apply again the connected tool to check if the small areas had gone?

Since each leaf should be in different component, I'm trying to make a script to decide what components should be left via keep only command.

Regards.
Post Reply