How do I split a color image into its 3 RGB channels?

조회 수: 850 (최근 30일)
hu
hu 2013년 10월 22일
이동: DGM 2023년 2월 11일
Hi,
How can I split a color image into its 3 RGB channels, like in this link:
Thanks
  댓글 수: 3
srikantanss ss
srikantanss ss 2016년 5월 23일
no Walter,the idea is to split only color regions

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2017년 7월 10일
You can do that with this code:
% Read in original RGB image.
rgbImage = imread('flower.png');
% Extract color channels.
redChannel = rgbImage(:,:,1); % Red channel
greenChannel = rgbImage(:,:,2); % Green channel
blueChannel = rgbImage(:,:,3); % Blue channel
% Create an all black channel.
allBlack = zeros(size(rgbImage, 1), size(rgbImage, 2), 'uint8');
% Create color versions of the individual color channels.
just_red = cat(3, redChannel, allBlack, allBlack);
just_green = cat(3, allBlack, greenChannel, allBlack);
just_blue = cat(3, allBlack, allBlack, blueChannel);
% Recombine the individual color channels to create the original RGB image again.
recombinedRGBImage = cat(3, redChannel, greenChannel, blueChannel);
% Display them all.
subplot(3, 3, 2);
imshow(rgbImage);
fontSize = 20;
title('Original RGB Image', 'FontSize', fontSize)
subplot(3, 3, 4);
imshow(just_red);
title('Red Channel in Red', 'FontSize', fontSize)
subplot(3, 3, 5);
imshow(just_green)
title('Green Channel in Green', 'FontSize', fontSize)
subplot(3, 3, 6);
imshow(just_blue);
title('Blue Channel in Blue', 'FontSize', fontSize)
subplot(3, 3, 8);
imshow(recombinedRGBImage);
title('Recombined to Form Original RGB Image Again', 'FontSize', fontSize)
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
My MATLAB code produces this figure:
which you can see is the same as your example figure you gave:
The original flower image is attached. I cropped it out of your figure.
  댓글 수: 2
Mahmood Aldhuhl
Mahmood Aldhuhl 2020년 11월 28일
hello sir
i try typing the code but show this messgae
Error in Q5 (line 7)
redChannel = rgbImage(:,:,1); % Red channel
DGM
DGM 2023년 2월 11일
The only way you'll get an error on that specific line is if rgbImage does not exist. The only way it won't exist is if you didn't create it as the given example does.

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

sixwwwwww
sixwwwwww 2013년 10월 22일
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
Jan
Jan 2018년 3월 5일
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.

댓글을 달려면 로그인하십시오.


David Sanchez
David Sanchez 2013년 10월 22일
my_rgb = my_image;
R = my_rgb(:,:,1);
G = my_rgb(:,:,2);
B = my_rgb(:,:,3);
  댓글 수: 5
aayush chibber
aayush chibber 2020년 11월 28일
How this is particulary working
Image Analyst
Image Analyst 2020년 11월 28일
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);

댓글을 달려면 로그인하십시오.


diya
diya 2016년 5월 3일
how to split rgb image to just take red color and to supress high intensities
  댓글 수: 1
Image Analyst
Image Analyst 2016년 5월 3일
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?

댓글을 달려면 로그인하십시오.


Adam G
Adam G 2017년 7월 12일
편집: Adam G 2017년 7월 15일
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
frishta fro
frishta fro 2020년 6월 21일
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
Image Analyst 2020년 6월 21일
  1. Use imread(), then
  2. Invert the images using img=255-img
  3. if image is grayscale, make red, green, and blue equal to the image, otherwise get the red, green, and blue using imsplit()
  4. Use addition or subtraction to change each color channel
  5. Combine into color image using cat(3, red, green, blue)
  6. Write using imwrite()

댓글을 달려면 로그인하십시오.


yazan abusamra
yazan abusamra 2022년 12월 4일
이동: DGM 2023년 2월 11일
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
Image Analyst
Image Analyst 2022년 12월 5일
이동: DGM 2023년 2월 11일
@yazan abusamra have you tried imadjust?
DGM
DGM 2022년 12월 6일
이동: DGM 2023년 2월 11일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by