How to separate colors into different images
-
- Posts: 9
- Joined: 2015-07-01T00:07:41-07:00
- Authentication code: 6789
How to separate colors into different images
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
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to separate colors into different images
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 %.
"-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.
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
The result is similar to the linked web page.
snibgo's IM pages: im.snibgo.com
- 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
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.
-
- Posts: 9
- Joined: 2015-07-01T00:07:41-07:00
- Authentication code: 6789
Re: How to separate colors into different images
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?
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?
-
- Posts: 9
- Joined: 2015-07-01T00:07:41-07:00
- Authentication code: 6789
Re: How to separate colors into different images
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?
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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to separate colors into different images
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.
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.
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.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.
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
- 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
If you use kmeans, you can then get the list of colors using its -v options or subsequently by
Then you can extract each color to a separate image with the rest transparent (or whatever background color you want), using
or put it all in one command line as (unix syntax - for windows see http://www.imagemagick.org/Usage/windows/
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.
Code: Select all
convert kmeans_result.gif -unique-colors txt:
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
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.
- 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
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.
-
- Posts: 9
- Joined: 2015-07-01T00:07:41-07:00
- Authentication code: 6789
Re: How to separate colors into different images
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
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to separate colors into different images
Fred doesn't use Cygwin. I do, so I will comment.
Fred's kmeans script contains the line:
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:
After making this change, Fred's kmeans script works for me without problems.
Fred's kmeans script contains the line:
Code: Select all
tmpdir="/tmp"
Code: Select all
tmpdir="."
snibgo's IM pages: im.snibgo.com
- 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
Thanks Snibgo for the comment. I was not aware of that. I have just fixed that and uploaded a new version.
Fred
Fred
-
- Posts: 9
- Joined: 2015-07-01T00:07:41-07:00
- Authentication code: 6789
Re: How to separate colors into different images
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!
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!
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to separate colors into different images
"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
-
- Posts: 9
- Joined: 2015-07-01T00:07:41-07:00
- Authentication code: 6789
Re: How to separate colors into different images
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?
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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to separate colors into different images
You reported:
This tells us the script is trying to call the program "bc" (not "bq").
Code: Select all
./kmeans: line 195: bc: command not found
snibgo's IM pages: im.snibgo.com