How to separate colors into different images

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?".
loskiroama
Posts: 9
Joined: 2015-07-01T00:07:41-07:00
Authentication code: 6789

How to separate colors into different images

Post by loskiroama »

I'm sure I'm not the first that has this need, but somehow I can't find anybody else that asked this question in the forums so here it goes:
I need to generate different images depending upon an original image's color, pretty much as in this link (http://www.mathworks.com/machine-learni ... ering.html)

Is there any way that I could do so using image magic?

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

Re: How to separate colors into different images

Post by snibgo »

There are many different solutions to the general problem of segmentation of photographs by colour. For the linked histology image, I crop to remove the border and captions, then set the Lightness channel of Lab to 50%. This eliminates lightness from the image.

Windows BAT syntax. For bash, change every ^ to \ and every %% to %.

Code: Select all

convert ^
  ipexhistology_01.png ^
  -crop 302x225+82+30 +repage ^
  -colorspace Lab ^
  -auto-level ^
  -channel R ^
  -evaluate set 50%% ^
  +channel ^
  +dither -colors 3 ^
  -colorspace sRGB ^
  -define histogram:unique-colors=true ^
  -format "%%c" -write histogram:info:h2.lis ^
  i2.png
"-channel R" really means "the first channel". h2.lis contains a list of the three colours.

The result is similar to the linked web page.
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: How to separate colors into different images

Post by fmw42 »

What is your IM version and platform? If on Linux, Mac OSX or Windows with Cygwin, then I have a script, kmeans, that will apply kmeans color reduction. See the link below.
loskiroama
Posts: 9
Joined: 2015-07-01T00:07:41-07:00
Authentication code: 6789

Re: How to separate colors into different images

Post by loskiroama »

Thanks so much for the script snibgo! After trying it there are some things that I still can't understand.

As far as I saw, you separate the 3 color channels (RGB) and then output them into a separate image.
What I'm trying to do is separate all the colors appearing on a single image into different images.
For intance, if an image has yellow,blue,green and purple. I'd like 4 images one containing ony the yellow parts, another the blue parts, and so on and so forth.

Is it possible to do this?
loskiroama
Posts: 9
Joined: 2015-07-01T00:07:41-07:00
Authentication code: 6789

Re: How to separate colors into different images

Post by loskiroama »

fmw42, thanks for the answer.

The thing is that after applying kmenas color reduction I should be able to separate the seed colors into different images.
Do you know any way in which I could do this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to separate colors into different images

Post by snibgo »

I create an image reduced to three colours in Lab space, whatever colours best best suit the image. I also list those colours to a text file.
loskiroama wrote:For intance, if an image has yellow,blue,green and purple. I'd like 4 images one containing ony the yellow parts, another the blue parts, and so on and so forth.
For the image that contain the yellow parts, what would you want the non-yellow pixels to be? Perhaps transparent, so the four images can be layered together to reconstitute the original.

I would do that with masks. For my i2.png and h2.lis result, loop through the colours, making a mask from the colour, and apply that as a mask to make non-yellow (etc) parts of the original image transparent ("-compose CopyOpacity").

That would need a shell script.
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: How to separate colors into different images

Post by fmw42 »

If you use kmeans, you can then get the list of colors using its -v options or subsequently by

Code: Select all

convert kmeans_result.gif -unique-colors txt:
Then you can extract each color to a separate image with the rest transparent (or whatever background color you want), using

Code: Select all

convert kmeans_result.gif +transparent firstcolor firstcolor_image.gif
convert kmeans_result.gif +transparent secondcolor secondcolor_image.gif
... etc ...
convert kmeans_result.gif +transparent lastcolor lastcolor_image.gif
or put it all in one command line as (unix syntax - for windows see http://www.imagemagick.org/Usage/windows/

Code: Select all

convert kmeans_result.gif \
\( -clone 0 +transparent firstcolor -write firstcolor_image.gif \) \
\( -clone 0 +transparent secondcolor -write secondcolor_image.gif \) \
...
\( -clone 0 +transparent lastcolor -write lastcolor_image.gif \) \
null:

You could write a script to call kmeans, then write a loop over each resulting color.

Note that kmeans does not ensure that any individual color is contiguous.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to separate colors into different images

Post by fmw42 »

I have modified my kmeans script to output any one of the following: 1) a flattened image, 2) a multipage/multilayer TIF -- one page/layer per color, or 3) multiple output images for each color.
loskiroama
Posts: 9
Joined: 2015-07-01T00:07:41-07:00
Authentication code: 6789

Re: How to separate colors into different images

Post by loskiroama »

Thank you very much for your advice fmw42!
I've been trying to run your k means script (on cygwin) but I'm having the following errors:

$ ./kmeans hl.jpg out.jpg

convert.exe: unable to open image `/tmp/KMEANS.6672/tmpI.miff': No such file or directory @ error/blob.c/OpenBlob/2674.
identify.exe: unable to open image `/tmp/KMEANS.6672/tmpI.miff': No such file or directory @ error/blob.c/OpenBlob/2674.
identify.exe: unable to open image `/tmp/KMEANS.6672/tmpI.miff': No such file or directory @ error/blob.c/OpenBlob/2674.
convert.exe: unable to open image `/tmp/KMEANS.6672/tmpI.miff': No such file or directory @ error/blob.c/OpenBlob/2674.
convert.exe: no images defined `txt:-' @ error/convert.c/ConvertImageCommand/3212.
convert.exe: unable to open image `/tmp/KMEANS.6672/tmpW.miff': No such file or directory @ error/blob.c/OpenBlob/2674.
convert.exe: unable to open image `/tmp/KMEANS.6672/tmpW.miff': No such file or directory @ error/blob.c/OpenBlob/2674.
convert.exe: no images defined `/tmp/KMEANS.6672/tmpD.miff' @ error/convert.c/ConvertImageCommand/3212.
convert.exe: divide by zero `0/0' @ error/fx.c/FxEvaluateSubexpression/2167.
convert.exe: no images defined `out.jpg' @ error/convert.c/ConvertImageCommand/3212.

Do you know what could be the cause of the error?
Do you think is related to cigwyin write permissions?

Thanks you once again
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to separate colors into different images

Post by snibgo »

Fred doesn't use Cygwin. I do, so I will comment.

Fred's kmeans script contains the line:

Code: Select all

tmpdir="/tmp"
On my http://im.snibgo.com/cygwin.htm page, I point out that this causes problems as "/tmp" is seen by Unix tools as one location, but by Windows tools as a different location. One solution, among others, is to change this to:

Code: Select all

tmpdir="."
After making this change, Fred's kmeans script works for me without problems.
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: How to separate colors into different images

Post by fmw42 »

Thanks Snibgo for the comment. I was not aware of that. I have just fixed that and uploaded a new version.

Fred
loskiroama
Posts: 9
Joined: 2015-07-01T00:07:41-07:00
Authentication code: 6789

Re: How to separate colors into different images

Post by loskiroama »

fmw42,snibgo,

Let me thank you once again for your incredible help guys.
I've made some progress but there are still some things i cannot figure out.

For instance passing different -n parameters i end up obtaining the same result in terms of color quantity:

$./kmeans hl.jpg .jpg (default n should be 5)
$ identify -format "%k" out.jpg
6777

$./kmeans -n 2 hl.jpg .jpg
./kmeans: line 195: bc: command not found
./kmeans: line 196: [: -eq: unary operator expected
$ identify -format "%k" out.jpg
6777

I have a couple of question here

1)Why if I use the default (n = 5) after I run identify -format "%k" out.jpg I get 6777 as color quantity? (shound't be 5)
2)Is the "command not found/eq: unary operator expected" error when I use n = 2 is affecting the result in a way that makes the script ignore the parameter that I pass and use the default n? (This seems logic as I get the same color quantity using n=2 and the default n=5)
3)Is identify -format "%k" the best way to count different colors on an image?

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

Re: How to separate colors into different images

Post by snibgo »

"bc" is a common calculator program for unix. It is available in the Cygwin package. I suggest you install it (or change the lines in the script to use IM to do the calculations).
snibgo's IM pages: im.snibgo.com
loskiroama
Posts: 9
Joined: 2015-07-01T00:07:41-07:00
Authentication code: 6789

Re: How to separate colors into different images

Post by loskiroama »

Thanks for your suggestion snibgo, but it makes no difference.
May I know what is bq supposed to help me with?

fmw42, Can I askyou how to call your modified script to get one image per color as you said?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to separate colors into different images

Post by snibgo »

You reported:

Code: Select all

./kmeans: line 195: bc: command not found
This tells us the script is trying to call the program "bc" (not "bq").
snibgo's IM pages: im.snibgo.com
Post Reply