Page 1 of 2

What happens when a channel gets separated?

Posted: 2015-08-27T09:58:56-07:00
by baxter stockman
Hi,

I am trying to understand what happens when I use this command

Code: Select all

convert rose: -channel R -separate separate_red.gif
found here http://www.imagemagick.org/Usage/color_basics/#channels ?

As far as I understand it separates one channel which means the other two are set to

Code: Select all

0
and then grayscales that.

However, if I separate a channel by setting the two others to 0, a white pixel (255,255,255) doesn't become white, it becomes a perfect Red (255,0,0). When I grayscale it then after using

Code: Select all

R*0.2126 + G*0.7152 + B*0.0722
, it becomes (55,0,0) which isn't white.

The white background in the pictures I use however, always turns out white.

Where is my mistake?

Thanks!

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:12:48-07:00
by snibgo
If an image is RGB, then "-separate" makes one image from each of the selected "-channel" names. Each image will be grayscale, with the same gray value that was in that channel.

So, "-channel R -separate" will make a grayscale image, by setting R=G=B= whatever was in the R channel. White will stay white because the R value was 255, and the new pixel becomes (255,255,255).

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:14:40-07:00
by fmw42
In IM 6, it separates the channel and then duplicates it for the other 3 channels so that all 3 channels are the same and so it looks grayscale. In IM 7, there will be only one single grayscale channel.

EDIT: Sorry snibgo, we apparently were posting at the same time

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:24:59-07:00
by baxter stockman
Ok, so IM6 doesn't use this forumula R*0.2126 + G*0.7152 + B*0.0722 ?


But how does that work with only one single grayscale channel? Will white still be white then?

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:38:07-07:00
by snibgo
"-separate" doesn't use the formula R*0.2126 + G*0.7152 + B*0.0722, or any similar formula. It just copies the value found in the red channel, and uses that as the gray value. As IM6 typically processes three colour channels, it copies this same value into each of the new RGB channels.

In IM7, when there need by only one channel for gray images, the result should be exactly the same.

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:43:24-07:00
by baxter stockman
Is there a difference in the file size then? It should be, the file in IM7 should be smaller, no?

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:45:50-07:00
by fmw42
baxter stockman wrote:Is there a difference in the file size then? It should be, the file in IM7 should be smaller, no?
That is correct. It will be smaller in IM 7.

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:51:18-07:00
by snibgo
Well, maybe, but the big difference will be in memory requirements, and perhaps in processing speed. IM 6 currently recognises when a image is grayscale, and makes the file smaller. But when it reads grayscale images, it creates three colour channels in memory, and processes them all.

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:55:26-07:00
by baxter stockman
Ok, but I still don't understand how IM7 does that. I am trying to build a JS code that grayscales it and then takes one channel of the image just like here:

https://en.wikipedia.org/wiki/Grayscale ... lor_images

the problem that I have is everytime when I do it also turns the white background into gray. So I am wondering what's the formula for that?

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:57:20-07:00
by fmw42
Yes, snibgo is correct. My mistake. When saving to a file, the channels may not be duplicated depending upon whether the file format supports grayscale or not.

Re: What happens when a channel gets separated?

Posted: 2015-08-27T10:58:59-07:00
by fmw42
the problem that I have is everytime when I do it also turns the white background into gray. So I am wondering what's the formula for that?
Perhaps you should post an example image to some place such as dropbox.com and put the URL here, so we can see what you start with and what you end with. Then describe what you think you want it to look like. Perhaps you are not starting with an RGB image. Maybe it is CMYK, then the channel R would really be cyan. However, that would still have white remaining white.

This should work. Note I put +channels at the end to be safe.

Code: Select all

convert rose: -channel R -separate +channel separate_red.gif

Re: What happens when a channel gets separated?

Posted: 2015-08-27T11:09:19-07:00
by fmw42
What version of IM and what platform are you using. If you are using an older version of IM, it might be at a time when IM was making grayscale linear gray and not sRGB gray. That would darken the image, though, still white should remain white, I think.

Re: What happens when a channel gets separated?

Posted: 2015-08-27T11:15:10-07:00
by baxter stockman
Thank you so much for taking the time to explain. It's not so much the problem of IM, I am trying to rebuild it with Javascript but everytime when I do I end up with something that looks like this https://www.dropbox.com/sh/yoqcjwlp6soe ... LpaQa?dl=0

I want it to be like in the wikipedia article. So I use the forumla R*0.2126 + G*0.7152 + B*0.0722 to grayscale it and then I set channel G&B to 0.

but the outcome is not like in IM

Re: What happens when a channel gets separated?

Posted: 2015-08-27T11:20:33-07:00
by fmw42
I want it to be like in the wikipedia article. So I use the forumla R*0.2126 + G*0.7152 + B*0.0722 to grayscale it and then I set channel G&B to 0.
The mistake is setting G and B to 0. Just keep the grayscale result as a single channel in some standard image format such as PNG.

If you need 3 channels, make them all the same as your grayscale result.

Re: What happens when a channel gets separated?

Posted: 2015-08-27T11:26:26-07:00
by baxter stockman
Oh ok, so my function is this

Code: Select all

for (var i = 0; i < data.length; i += 4) {
    var grayscale = data[i]*0.2126 + data[i +1]*0.7152 + data[i +2]*0.0722;
    data[i]     = grayscale; // red
    data[i + 1] = grayscale; // green
    data[i + 2] = grayscale; // blue
}
so when I leave the green and blue out

Code: Select all

for (var i = 0; i < data.length; i += 4) {
    var grayscale = data[i]*0.2126 + data[i +1]*0.7152 + data[i +2]*0.0722;
    data[i]     = grayscale; // red

}
it becomes kind of blueish