Color Imaging - RGB Channels

3 views (last 30 days)
Utkarsh Srivastava
Utkarsh Srivastava on 28 Oct 2020
Commented: DGM on 1 Jan 2024
%Read the image
img = imread('image.jpg');
%Get the size (rows and columns) of the image
[r,c] = size(img);
rr=r/3;
%Wrire code to split the image into three equal parts and store them in B, G, R channels
B=imcrop(img,[1,1,c,rr]);
G=imcrop(img,[1,1*rr,c,rr]);
R=imcrop(img,[1,2*rr,c,rr]);
%concatenate R,G,B channels and assign the RGB image to ColorImg variable
ColorImg(:,:,1) = R;
ColorImg(:,:,2) = G;
ColorImg(:,:,3) = B;
imshow(ColorImg)

Answers (2)

ANTO OVID G S
ANTO OVID G S on 1 Apr 2021
%Read the image
img = imread('image.jpg');
%Get the size (rows and columns) of the image
[r,c] = size(img);
rr=r/3;
%Wrire code to split the image into three equal parts and store them in B, G, R channels
B = img(1 : r / 3, :, :);
G = img(r / 3 + 1 : 2 * r / 3, :, :);
R = img(2 * r / 3 + 1 : r, :, :);
% Concatenate R,G,B channels and assign the RGB image to ColorImg variable
ColorImg(:,:,1) = R;
ColorImg(:,:,2) = G;
ColorImg(:,:,3) = B;
imshow(ColorImg)
  2 Comments
Zakariye Mohamud
Zakariye Mohamud on 12 Nov 2022
thanks it worked me...
Much Thankful for your Work .. Anto Ovid
DGM
DGM on 12 Nov 2022
Edited: DGM on 12 Nov 2022
Note that this will only work if the copy of the source image is 2D. Some copies of this image are not. Just because an image looks gray does not mean it's not an RGB image.
  • misuse of scalar output syntax for size() will break if array is not 2D
  • presumptive output assignment will break if array is not 2D
  • rr is calculated but never used
  • array indexing blindly uses fractional subscripts
  • output image is constructed by array growing

Sign in to comment.


DGM
DGM on 12 Nov 2022
I know this is homework and that actually trying to make the result not look like garbage is well beyond the scope of the assignment. That said, it's not my homework, so I'm not limited by those expectations.
I'm going to just blindly throw this to the Image Registration App and see what it can do. I don't have CVT, so I'm stuck trying to do this in-browser here. I'm sure it can be registered better, but the results are fair for a remarkably lazy attempt.
% read the image
inpict = imread('image.jpeg'); % a gray or RGB image
inpict = im2gray(inpict); % force the image to be gray
inpict = imcrop(inpict,[21 22 365 991]); % crop off excess
% detile the image
[h,w,~] = size(inpict);
h = 3*floor(h/3);
inpict = reshape(inpict(1:h,:).',w,[],3);
inpict = permute(inpict,[2 1 3]);
[B G R] = imsplit(inpict);
% show naive concatenation of R G B channels
imshow(cat(3,R,G,B))
% apply estimated registration for R and B channels
% these come from the Image Registration Estimator app that comes with IPT
% but maddeningly enough, it actually requires CVT to use the files it creates
Sbg = registerImages_BG(B,G);
Srg = registerImages_RG(R,G);
% show attempted automated registration
outpict = cat(3,Srg.RegisteredImage,G,Sbg.RegisteredImage);
imshow(outpict)

Categories

Find more on Image Processing Toolbox 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!