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

New to MATLAB?

combining RGB to get a full image

Asked by prem preet

prem preet (view profile)

on 23 Oct 2011

image_RGB = cat(2,final_red,final_green,final_blue)

imshow(image_RGB);

can anyone suggest me that this code is correct for combining RGB components for getting full image

as soon as possible

0 Comments

prem preet

prem preet (view profile)

2 Answers

Answer by Wayne King

Wayne King (view profile)

on 23 Oct 2011

Hi, You don't want to concatenate along the column dimension. An RGB image has 3 dimensions. You want to concatenate along the 3rd dimension.

      imdata = imread('ngc6543a.jpg');
      RC = imdata(:,:,1);
      GC = imdata(:,:,2);
      BC = imdata(:,:,3);
      im = cat(3,RC,GC,BC);
      isequal(imdata,im)
      imshow(im)            

5 Comments

Wayne King

Wayne King (view profile)

on 23 Oct 2011

please post the exact error message

prem preet

prem preet (view profile)

on 23 Oct 2011

??? Undefined function or variable 'redchannel'.

prem preet

prem preet (view profile)

on 23 Oct 2011

this error occured for all 3 above functions ....
imshow(redchannel);.....

plss rply fast..

Wayne King

Wayne King (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 23 Oct 2011

IMPORTANT NOTE: MATLAB is case sensitive. redchannel is different than redChannel.

It looks like you've taken code I've posted many times and tried to adapt it:

% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Recombine separate color channels into an RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);

I call them "channels" because that is the terminology Photoshop uses. You have to change the name of the arrays from redChannel to what yours are called. After you split apart your color channels and reprocessed them you have final_red. This, NOT redChannel, is what you need to use. You also need to not change from dimension 3 (which I had) to dimension 2. So the final code would be

    % Recombine separate color channels into an RGB image.
    rgbImage = cat(3, final_red,final_green,final_blue);

3 Comments

prem preet

prem preet (view profile)

on 23 Oct 2011

thanks sir ,
i gonna try this..

Shaila parvin

Shaila parvin (view profile)

on 24 Jun 2013

rgbImage = cat(3, final_red,final_green,final_blue);

What will happen if I write 2/1/4/something else in place of 3. I mean:

rgbImage = cat(1, final_red,final_green,final_blue);

Titus Edelhofer

Titus Edelhofer (view profile)

on 24 Jun 2013

It will most likely fail: the 3 is denoting the 3rd dimension. For RGB images: 1. and 2. dimension give the size in pixels, the 3. dimension is the color.

Image Analyst

Image Analyst (view profile)

Contact us