Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
The mixing formula of RGB

Subject: The mixing formula of RGB

From: Wenlong

Date: 14 Jan, 2013 12:29:06

Message: 1 of 7

Hi, all

I am trying to interpret RGB image format. Currently, I read the .bmp image with Matlab, and I can see it three layers of red, green, and blue. But when I try to composite the layers to recover the original image, the result is significantly different from the original one.

What I do is simply add the pixel values of red, green and blue, and divide it by 3. But seems it is not the correct approach.

May I know what is the mixing formula?

Thank you very much for your kindly help.

Best regards
Wenlong

Subject: The mixing formula of RGB

From: Steven_Lord

Date: 14 Jan, 2013 15:09:14

Message: 2 of 7



"Wenlong " <scc.wwl@gmail.com> wrote in message
news:kd0tmi$3m6$1@newscl01ah.mathworks.com...
> Hi, all
>
> I am trying to interpret RGB image format. Currently, I read the .bmp
> image with Matlab, and I can see it three layers of red, green, and blue.
> But when I try to composite the layers to recover the original image, the
> result is significantly different from the original one.
>
> What I do is simply add the pixel values of red, green and blue, and
> divide it by 3. But seems it is not the correct approach.
> May I know what is the mixing formula?

If your image is an RGB image, use CAT to concatenate the color pages in the
3rd dimension.

If it is an indexed image, use IND2RGB with the indices and color map data.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: The mixing formula of RGB

From: Eugenijus Januškevičius

Date: 15 Jan, 2013 22:02:24

Message: 3 of 7

2013.01.14 14:29, Wenlong rašė:
> Hi, all
>
> I am trying to interpret RGB image format. Currently, I read the .bmp
> image with Matlab, and I can see it three layers of red, green, and
> blue. But when I try to composite the layers to recover the original
> image, the result is significantly different from the original one.
>
> What I do is simply add the pixel values of red, green and blue, and
> divide it by 3. But seems it is not the correct approach.
> May I know what is the mixing formula?

Can you be more specific?
Reading BMP give you values of red, green and blue channel as 3D matrix,
so there is no need to reinvent the wheel, as i can understand.

If you need a greyscale image, use rgb2gray()

--
regards, ejs

Subject: The mixing formula of RGB

From: Wenlong

Date: 16 Jan, 2013 10:30:09

Message: 4 of 7

Hi, Eugenijus

Thank you for your reply.

I know little about computer graphic. My problem is that I need the pixel value of pixels in a colorized image (.BMP format), and use the value for some data analysis. I have the grayscale version of the same image, and I see it is a 2D matrix. So I can specify the coordinates of a pixel and get its pixel value.

But when I come to colorized version of the image, it is a 3D matrix that, as you said, combined by red, green and blue. I think there could be a way to extract the pixel value, not in three individual channels but a single metric?

Thank you very much for your help.

Best regards
Wenlong

Subject: The mixing formula of RGB

From: Steven_Lord

Date: 16 Jan, 2013 14:41:33

Message: 5 of 7



"Wenlong " <scc.wwl@gmail.com> wrote in message
news:kd5vfh$gcp$1@newscl01ah.mathworks.com...
> Hi, Eugenijus
>
> Thank you for your reply.
>
> I know little about computer graphic. My problem is that I need the pixel
> value of pixels in a colorized image (.BMP format), and use the value for
> some data analysis. I have the grayscale version of the same image, and I
> see it is a 2D matrix. So I can specify the coordinates of a pixel and get
> its pixel value.
>
> But when I come to colorized version of the image, it is a 3D matrix

The convention in MATLAB is that matrices are 2-D. Variables with arbitrary
numbers of dimensions are arrays. [All matrices are also arrays, but only
some arrays are matrices.] Certain operations are defined only on matrices
and will not work on N-D (N > 2) arrays. [Matrix multiplication is one
example.]

> that, as you said, combined by red, green and blue. I think there could be
> a way to extract the pixel value, not in three individual channels but a
> single metric?

img = rand(5, 4, 3);
pixel23 = img(2, 3, :)

The first element of pixel23 is the R component of the pixel at location (2,
3) in img. The second is the G component, the third if B.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: The mixing formula of RGB

From: Jurgen

Date: 17 Jan, 2013 16:51:12

Message: 6 of 7

"Wenlong" wrote in message <kd0tmi$3m6$1@newscl01ah.mathworks.com>...
> Hi, all
>
> I am trying to interpret RGB image format. Currently, I read the .bmp image with Matlab, and I can see it three layers of red, green, and blue. But when I try to composite the layers to recover the original image, the result is significantly different from the original one.
>
> What I do is simply add the pixel values of red, green and blue, and divide it by 3. But seems it is not the correct approach.
>
> May I know what is the mixing formula?
>
> Thank you very much for your kindly help.
>
> Best regards
> Wenlong

If your image is uint8 then there are 2^(3*8) about 16.7 million unique colors.

If you have a pixel with 3 uint8 values ([R G B]), then: sub2ind([256 256 256],[R G B]) will give a unique number for that color.

If your RGB image is truecolor (like 0-1 double values) then you need to quantize (approximate) to your desired level of accuracy. e.g. you could linearly & uniformly quantize each color into 16bits.This will mean 2^(16*3) unique colors.

Subject: The mixing formula of RGB

From: Wenlong

Date: 18 Jan, 2013 09:34:09

Message: 7 of 7

Hi, Jurgen

Thank you very much for your reply. It is exactly what I need. It works perfectly in my application.

Many thanks for your help.

Best wishes
Wenlong

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us