How do I split a color image into its 3 RGB channels?
Show older comments
Hi,
How can I split a color image into its 3 RGB channels, like in this link:

Thanks
3 Comments
Walter Roberson
on 2 Sep 2015
vermal, the code is like what was posted below at http://uk.mathworks.com/matlabcentral/answers/91036-split-an-color-image-to-its-3-rgb-channels#answer_100475
srikantanss ss
on 23 May 2016
no Walter,the idea is to split only color regions
Walter Roberson
on 10 Jul 2017
Accepted Answer
More Answers (6)
sixwwwwww
on 22 Oct 2013
If you want to show RGB channels in there original color then you can do like it:
img = imread('filename.png'); % Read image
red = img(:,:,1); % Red channel
green = img(:,:,2); % Green channel
blue = img(:,:,3); % Blue channel
a = zeros(size(img, 1), size(img, 2));
just_red = cat(3, red, a, a);
just_green = cat(3, a, green, a);
just_blue = cat(3, a, a, blue);
back_to_original_img = cat(3, red, green, blue);
figure, imshow(img), title('Original image')
figure, imshow(just_red), title('Red channel')
figure, imshow(just_green), title('Green channel')
figure, imshow(just_blue), title('Blue channel')
figure, imshow(back_to_original_img), title('Back to original image')
I hope it helps. Good luck!
5 Comments
DINESH RAJA P
on 28 Aug 2015
how about simulink block??? how can i split the image ?? and recombine R G B ( separate ) into single image
Walter Roberson
on 16 Oct 2015
Pallawi Pallawi comments
THIS CODE IS AMAZING AND IT REALLY WORKS..
Walter Roberson
on 10 Jul 2017
No, the code makes RGB model; see https://www.mathworks.com/matlabcentral/answers/91036-split-an-color-image-to-its-3-rgb-channels#comment_467946
Jan
on 5 Mar 2018
bassel mar wrote:
Very good and simple code
@bassel mar: Please do not use flags to post a comment, because they are used to inform admins and editors about inappropriate contents like spam or rudeness.
Thamizhthenvalavan Gajendiran
on 20 Feb 2020
simple code
David Sanchez
on 22 Oct 2013
my_rgb = my_image;
R = my_rgb(:,:,1);
G = my_rgb(:,:,2);
B = my_rgb(:,:,3);
5 Comments
Syed Masaab Ahmed
on 20 Nov 2018
Moved: DGM
on 11 Feb 2023
R = my_rgb(:,:,1);
G = my_rgb(:,:,2);
B = my_rgb(:,:,3);
whats are these (:,:,1) doing
Image Analyst
on 20 Nov 2018
Moved: DGM
on 11 Feb 2023
That takes the slice/plane/channel that is a single color. Imagine an RGB image as three separate gray scale images stacked vertically, as slices or planes.

Syed Masaab Ahmed
on 21 Nov 2018
Moved: DGM
on 11 Feb 2023
Thank you fo help
aayush chibber
on 28 Nov 2020
How this is particulary working
Image Analyst
on 28 Nov 2020
An RGB image is like three 2-D monochrome/grayscale images stacked on top of each other in the third dimension. See attached demo for a visualization:

The code basically extracts each 2-D image from the 3-D image into a separate 2-D array. With MATLAB Release 2018b and later, you can use imsplit() instead
[R, G, B] = imsplit(rgbImage);
MathWorks Computer Vision Toolbox Team
on 21 Nov 2024
I=imread("peppers.png");
[r,g,b] = imsplit(I);
diya
on 3 May 2016
0 votes
how to split rgb image to just take red color and to supress high intensities
1 Comment
Image Analyst
on 3 May 2016
You know how to extract the red channel already
redChannel = rgbImage(:, :, 1);
Now explain what "suppress" means to you. Do you want to set high values to zero? Multiply them by 0.5 or some other number? Apply a gamma curve?
I have picture hand.tif.
%the best idea is to make screenshot of this picture

I have following codes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
imfinfo('hand.tif');
[L1]= imread('hand.tif');
figure, imshow(L1);
L1r=L1(:, :, 1);
L1g=L1(:, :, 2);
L1b=L1(:, :, 3);
figure;
imshow(cat(3, L1r, ones(size(L1g)), ones(size(L1b))));
figure;
imshow(cat(3, ones(size(L1r)), L1g, ones(size(L1b))));
figure;
imshow(cat(3, ones(size(L1r)), ones(size(L1g)), L1b));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I got following pictures

and okay. I have following code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
imfinfo('hand.tif');
[L1]= imread('hand.tif');
L1=double(L1)/255;
figure, imshow(L1);
L1c=L1(:, :, 1);
L1m=L1(:, :, 2);
L1y=L1(:, :, 3);
figure;
imshow(cat(3, L1c, ones(size(L1m)), ones(size(L1y))));
figure;
imshow(cat(3, ones(size(L1c)), L1m, ones(size(L1y))));
figure;
imshow(cat(3, ones(size(L1c)), ones(size(L1m)), L1y));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I got following pictures

Is it matlab error or what?
So now I check matrix L1r
L1r=
0 0 0 0 0
0 5 5 3 3
0 5 4 3 4
0 3 4 2 3
0 3 4 2 1
I check matrix L1c
L1c=
0 0 0 0 0
0 0,0196078431372549 0,0196078431372549 0,0117647058823529 0,0117647058823529
0 0,0196078431372549 0,0156862745098039 0,0117647058823529 0,0156862745098039
0 0,0117647058823529 0,0156862745098039 0,00784313725490196 0,0117647058823529
0 0,0117647058823529 0,0156862745098039 0,00784313725490196 0,00392156862745098
Strange when I make L1=double(L1)/255
L1c=L1r/255
cyan=red/255
or matlab error and I got wrong colormap?
???????????? so what color is in matrix L1r and L1c?
I check in Paint Program and I got red color =5 (position of pixel 2,2 ) so L1r should be red, but L1c????

6 Comments
Walter Roberson
on 12 Jul 2017
Why are you using ones for the values for the unselected planes? You should be using zeros.
Adam G
on 12 Jul 2017
Yes now (ones->zeros) it works they are the same, thanks.
Image Analyst
on 12 Jul 2017
Shouldn't even be doing that, in my opinion. Should be using grayscale.
By the way, if you're interested in turning an RGB pseudo-colored thermal image into temperature values, I've attached a demo for that.
Adam G
on 15 Jul 2017
I know thermal image should be analysed in grayscale, but it was an example.
frishta fro
on 21 Jun 2020
who can hehp me to solve that pleeeeeeeeeeees
Write a Matlab program to do the following operations in the attached image.
1- Find the negative of the brain image.
2- Reduce the intensity of Red channel by 80.
3- Increase the intensity of Green channel by 50.
4- Save the output image as a name brain2.jpg. 

Image Analyst
on 21 Jun 2020
- Use imread(), then
- Invert the images using img=255-img
- if image is grayscale, make red, green, and blue equal to the image, otherwise get the red, green, and blue using imsplit()
- Use addition or subtraction to change each color channel
- Combine into color image using cat(3, red, green, blue)
- Write using imwrite()
yazan abusamra
on 4 Dec 2022
Moved: DGM
on 11 Feb 2023
0 votes
hello I have an old photo for my grampa it is rgb but it lookes like gray photo the problem is that the rgb chanels have very close values to each other in all pixels how can I solve this problem and restore the photo so I can see the original color
3 Comments
Walter Roberson
on 4 Dec 2022
Moved: DGM
on 11 Feb 2023
You should create your own Question for that. It would be best if you could include one or two of the photos when you do that.
Image Analyst
on 5 Dec 2022
Moved: DGM
on 11 Feb 2023
It sounds like a scan/photo of a black & white print or something. If that's the case, the color content is an artifact of the capture method, not the scene content. That would make this a colorization problem, not an adjustment problem.
If it's a faded/degraded color print, there might be adjustment possibilites, but they can easily be limited by the condition and format of the image.
That said, we'd need more information about the images (examples and perhaps an explanation of why they turned gray). I'd be interested, but I agree with Walter.
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
