Page 1 of 2
How to separate colors into different images
Posted: 2015-07-01T00:13:33-07:00
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
Re: How to separate colors into different images
Posted: 2015-07-01T08:17:55-07:00
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.
Re: How to separate colors into different images
Posted: 2015-07-01T09:00:52-07:00
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.
Re: How to separate colors into different images
Posted: 2015-07-05T11:05:55-07:00
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?
Re: How to separate colors into different images
Posted: 2015-07-05T11:11:48-07:00
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?
Re: How to separate colors into different images
Posted: 2015-07-05T13:53:01-07:00
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.
Re: How to separate colors into different images
Posted: 2015-07-05T15:47:04-07:00
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.
Re: How to separate colors into different images
Posted: 2015-07-06T09:11:35-07:00
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.
Re: How to separate colors into different images
Posted: 2015-07-07T06:16:20-07:00
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
Re: How to separate colors into different images
Posted: 2015-07-07T07:12:39-07:00
by snibgo
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.
Re: How to separate colors into different images
Posted: 2015-07-07T10:03:43-07:00
by fmw42
Thanks Snibgo for the comment. I was not aware of that. I have just fixed that and uploaded a new version.
Fred
Re: How to separate colors into different images
Posted: 2015-07-08T03:31:05-07:00
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!
Re: How to separate colors into different images
Posted: 2015-07-08T05:29:26-07:00
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).
Re: How to separate colors into different images
Posted: 2015-07-08T12:43:45-07:00
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?
Re: How to separate colors into different images
Posted: 2015-07-08T13:02:27-07:00
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").