What happens when a channel gets separated?

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
baxter stockman
Posts: 16
Joined: 2015-07-31T12:35:27-07:00
Authentication code: 1151

What happens when a channel gets separated?

Post by baxter stockman » 2015-08-27T09:58:56-07:00

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!

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

Re: What happens when a channel gets separated?

Post by snibgo » 2015-08-27T10:12:48-07:00

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).
snibgo's IM pages: im.snibgo.com

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

Re: What happens when a channel gets separated?

Post by fmw42 » 2015-08-27T10:14:40-07:00

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

baxter stockman
Posts: 16
Joined: 2015-07-31T12:35:27-07:00
Authentication code: 1151

Re: What happens when a channel gets separated?

Post by baxter stockman » 2015-08-27T10:24:59-07:00

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?

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

Re: What happens when a channel gets separated?

Post by snibgo » 2015-08-27T10:38:07-07:00

"-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.
snibgo's IM pages: im.snibgo.com

baxter stockman
Posts: 16
Joined: 2015-07-31T12:35:27-07:00
Authentication code: 1151

Re: What happens when a channel gets separated?

Post by baxter stockman » 2015-08-27T10:43:24-07:00

Is there a difference in the file size then? It should be, the file in IM7 should be smaller, no?

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

Re: What happens when a channel gets separated?

Post by fmw42 » 2015-08-27T10:45:50-07:00

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.

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

Re: What happens when a channel gets separated?

Post by snibgo » 2015-08-27T10:51:18-07:00

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.
snibgo's IM pages: im.snibgo.com

baxter stockman
Posts: 16
Joined: 2015-07-31T12:35:27-07:00
Authentication code: 1151

Re: What happens when a channel gets separated?

Post by baxter stockman » 2015-08-27T10:55:26-07:00

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?

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

Re: What happens when a channel gets separated?

Post by fmw42 » 2015-08-27T10:57:20-07:00

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.

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

Re: What happens when a channel gets separated?

Post by fmw42 » 2015-08-27T10:58:59-07:00

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

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

Re: What happens when a channel gets separated?

Post by fmw42 » 2015-08-27T11:09:19-07:00

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.

baxter stockman
Posts: 16
Joined: 2015-07-31T12:35:27-07:00
Authentication code: 1151

Re: What happens when a channel gets separated?

Post by baxter stockman » 2015-08-27T11:15:10-07:00

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

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

Re: What happens when a channel gets separated?

Post by fmw42 » 2015-08-27T11:20:33-07:00

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.

baxter stockman
Posts: 16
Joined: 2015-07-31T12:35:27-07:00
Authentication code: 1151

Re: What happens when a channel gets separated?

Post by baxter stockman » 2015-08-27T11:26:26-07:00

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

Post Reply