user643718
user643718

Reputation: 45

Script is not working. What am I doing wrong?

First off I found this guide, which details exactly what I need.

https://imagemagick.org/script/connected-components.php

enter image description here

For the life of me I cannot get this to work. Anyone have any idea?

I've tried a bunch of variations of the scripts listed in the guide.

Also when I run convert /var/www/mailtovoice/audrey/sean_look_grey.png -define connected-components:verbose=true -connected-components 8 /var/www/mailtovoice/audrey/sean_look4.png

I get 1000s of objects. When I converted it to an image with just 3 objects I get 100s.

Upvotes: 2

Views: 60

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207465

The method suggested by Fred (@fmw42) is far simpler and preferable to that shown in this answer, so all but die-hard enthusiasts should use Fred's answer. Rather than delete mine, I will leave it showing as it could form the basis for other more demanding/involved processing.


This is a rather funny way to do it... find all the blobs. i.e. connected components:

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

which gives you something like this but with 2,000+ lines:

Objects (id: bounding-box centroid area mean-color):
0: 860x482+0+0 431.5,239.7 405738 gray(0)
800: 43x263+252+219 265.9,350.5 2458 gray(255)
2: 21x226+276+0 288.9,111.2 1540 gray(255) 
2216: 5x16+107+445 109.3,452.9 65 gray(255)
910: 7x15+276+228 279.0,234.5 63 gray(255)
491: 7x14+651+150 654.1,156.6 54 gray(255)
1207: 7x9+735+282 737.9,285.8 53 gray(255)
2313: 6x9+147+457 149.6,460.9 48 gray(255)
985: 8x9+754+238 757.3,242.0 48 gray(255)
...
...

Now look for all the ones with a size (second-to-last field) less than 1000 using awk and print the region:

convert spotty.png \
   -define connected-components:verbose=true \
   -connected-components 4 null:             | 
   awk -v thresh=1000 'NR>1 && $(NF-1)<thresh{print " -region " $2 " -colorize 100%"}'

Output

-region 5x16+107+445 -colorize 100%
-region 7x15+276+228 -colorize 100%
-region 7x14+651+150 -colorize 100%
-region 7x9+735+282 -colorize 100%
...
...

Now reload the original image, set the fill colour for colorised regions to red and regenerate the list of regions to be filled exactly as above:

convert spotty.png -fill red $(convert spotty.png -define connected-components:verbose=true  -connected-components 4 null: | awk -v thresh=1000 'NR>1 && $(NF-1)<thresh{print " -region " $2 " -colorize 100%"}' ) result.png

enter image description here


The command generated boils down to:

convert spotty.png -threshold 50% -fill red \
   -region 56x16+107+445 -colorize 100%     \
   -region 70x15+276+228 -colorize 100%     \
   -region ...           -colorize 100%     \
   ...
   ...
   result.png

Upvotes: 1

fmw42
fmw42

Reputation: 53081

Mark has the right idea, but the solution is much simpler than he posted, since ImageMagick -connected-components can do the filtering and output directly.

input: enter image description here

Unix line endings (for windows use ^ rather than \ )

convert image.png \
-define connected-components:area-threshold=100 \
-define connected-components:mean-color=true \
-connected-components 4 \
result.png


enter image description here

Upvotes: 2

Related Questions