segment image according to colors in image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

segment image according to colors in image

Post by sanjeevk »

Hi,
All

Can we segment an image in multiple images according to used color in that image.

I want to following things to do with an image.

1. Pick the used color in image using any tool like color piker.
2. supply all picked color to that command which segment the image.

OUTPUT
get segmented images according to color.


e.g.

Suppose we have an image with 3 color
1. red.
2. black.
3.blue.

then after segmentation of this image we need 3 new images. Each contain only one color rest part of this image must be transparent.

Any one can suggest on the same.

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

Re: segment image according to colors in image

Post by fmw42 »

convert image -channel rgba -alpha on -fuzz XX% -fill none +opaque red image_red.png

This will extract all colors within XX% of red (you could use XX=0 for an exact match) and make all the rest transparent.

Repeat for each of the other color.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: segment image according to colors in image

Post by anthony »

convert xc:red xc:black xc:blue color_map.png
convert image.png +dither -map color_map.png image_segments.png

As you are using primary colors like red and blue, you could use -separate to get the
red and blue segment masks.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: segment image according to colors in image

Post by fmw42 »

convert xc:red xc:black xc:blue color_map.png
convert image.png +dither -map color_map.png image_segments.png
Does not work!

1) convert xc:red xc:black xc:blue color_map.png produces 3 images; color_map-0.png, etc
2) you only reference color_map.png which does not exist.

Not sure if you want to append the red,black,blue images or you want to reference the three color_map images

3) Also -map does not show a file option, do you mean -remap?

Anthony, please clarify.
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: segment image according to colors in image

Post by sanjeevk »

Hi fmw42,
I have use

" convert image -channel rgba -alpha on -fuzz XX% -fill none +opaque red image_red.png"

command for this it's working. You have any other idea for the same in which we does not use -fuzz factor.

Or we can get same result without using -fuzzXX%.

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

Re: segment image according to colors in image

Post by fmw42 »

you can leave -fuzz XX% off or set to XX=0 and you get an exact match to make all colors but exactly red to transparent.

convert image -channel rgba -alpha on -fill none +opaque red image_red.png

This will change all colors but exactly red to transparent. If you don't have exactly red, then it will not work.
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: segment image according to colors in image

Post by sanjeevk »

Thanks,

I am working on the same.
loskiroama
Posts: 9
Joined: 2015-07-01T00:07:41-07:00
Authentication code: 6789

Re: segment image according to colors in image

Post by loskiroama »

Very helpful fmw42!
Now, what if I don't know the colors present in the image beforehand?
How can I do the same in this situation?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: segment image according to colors in image

Post by fmw42 »

You would have to use the Colormap or Histogram in the meta data to find all the colors and select the color you want to use. This could be scripted, but there is no single IM command to do this. You can find the colormap or histogram via

identify -verbose yourimage

The histogram also can be retrieved by

convert image -format "%c" histogram:info:

It may be necessary to reduce the number of colors in your image first (via -colors)
Post Reply