Creating Mask files from connected-compoents

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
UHFT
Posts: 1
Joined: 2019-05-21T19:55:42-07:00
Authentication code: 1152

Creating Mask files from connected-compoents

Post by UHFT » 2019-05-21T20:12:30-07:00

Given a BW image with several white color regions, it seems connected-components can label regions separately.

How can I create individual region in different mask files for each connected-component regions ?

Thanks for your help.

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

Re: Creating Mask files from connected-compoents

Post by fmw42 » 2019-05-21T23:05:15-07:00

Use both -define connected-components:mean-color=true and -define connected-components:keep=list-of-ids. That will export one file for each region that you list. You have to run it once to find the regions you want and then run it again with these commands.

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

Please always provide your IM version and platform when asking questions, since syntax may differ.

snibgo
Posts: 12029
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Creating Mask files from connected-compoents

Post by snibgo » 2019-05-22T03:44:45-07:00

If you want one image per component, you will need a shell loop after first listing the components. Within that loop, you could re-run the connected components for a single id-number. Alternatively, use the first run to create unique colours, one per component. Then loop through those colours, isolating the colour to make the mask.

The code for this will depend on your shell language, of course.
snibgo's IM pages: im.snibgo.com

Post Reply