Need help with creating or modifying an RGB image.

Capture.PNG
% read in a jpg image
%
z=imread('Paraglider.jpg');
size(z)
%
% separate out the red, blue, and green components
%
r=z(:,:,1); % r=z(rows, columns, first page)
b=z(:,:,2);
g=z(:,:,3);
%
% look at the components
%
figure(6),clf
subplot(2,2,1),imshow(z),title('composite')
subplot(2,2,2),imshow(r),title('red')
%
% recombine components
%
figure(7),clf
subplot(2,2,1),imshow(cat(3,r,b,g)),title('r,b,g')
%
% write an image to a jpg file
%
outimage=cat(3,g,r,b);
imwrite(outimage,'ParaGRB.jpg')

 Accepted Answer

I'm not sure how to separate out the red, blue, and green components of an image like the Paraglider.jpg that I'm using .
As you already showing the same in the code
%Read the image
image1=imread('image file name.format');
r=image1(:,:,1); % This line gives you red component of the image
b=image1(:,:,2); % This line gives you blue component of the image
g=image1(:,:,3); % This line gives you green component of the image
Please response in the comment section only.

5 Comments

JC
JC on 27 Apr 2019
Edited: JC on 27 Apr 2019
But these codes don't give me separate images as subplots in red, blue, and green of the Paraglider.jpg. They all show up as 4 gray images when I use the following codes below.
% look at the components
%
figure(6),clf
subplot(4,2,1),imshow(z),title('composite')
subplot(4,2,2),imshow(r),title('red')
subplot(4,2,3),imshow(b),title('blue')
subplot(4,2,4),imshow(g),title('green')
Are you loking for this one? Change according for composite cat(..)
im=imread('test.jpg');
%..........^^^^^^^...Change the image file name
subplot(221);imshow(im), title('RGB Image');
%% Red Component
red_com=im;
red_com(:,:,2:3)=0;
subplot(222); imshow(red_com); title('Red');
%% Green Componnet
green_com=im;
green_com(:,:,1)=0;
green_com(:,:,3)=0;
subplot(223);imshow(green_com); title('Green');
%% Blue Component
blue_com=im;
blue_com(:,:,1:2)=0;
subplot(224);imshow(blue_com); title('Blue');
Right that's what I wanted to do for my image. Thank you.
Would you also happened to know how to recombine the component and then write an image to a JPEG file?
% recombine components
%
figure(7),clf
subplot(2,2,1),imshow(cat(3,r,b,g)),title('r,b,g')
%
% write an image to a jpg file
%
outimage=cat(3,g,r,b);
imwrite(outimage,'ParaGRB.jpg')
Those all red_com, blue_com and green_com are RGB images shown in respective components.
Red color shows means in RGB, in the following you extract the r,g, b components (Gray Representation)
im=imread('test.jpg');
%subplot(221);imshow(im), title('RGB Image');
%% Gray-Red Component
red_com=im(:,:,1);
subplot(221); imshow(red_com); title('Red Component');
%% Gray-Green Componnet
green_com=im(:,:,2);
subplot(222);imshow(green_com); title('Green Component');
%% Blue Component
blue_com=im(:,:,3);
subplot(223);imshow(blue_com); title('Blue Component');
%% Blue Component
combine=cat(3,red_com,green_com,blue_com);
subplot(224);image(uint8(combine)); title('Composite');

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

JC
on 27 Apr 2019

Edited:

on 27 Apr 2019

Community Treasure Hunt

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

Start Hunting!