Display the red, green and blue color planes of a color image in MATLAB
Last Updated :
28 Mar, 2019
Prerequisite – RGB image representation
A colored image can be represented as a 3 order matrix. The first order is for the rows, the second order is for the columns and the third order is for specifying the color of the corresponding pixel. Here we use the RGB color format, so the third order will take 3 values of Red, Green ans Blue respectively. The values of the rows and columns depends on the size of the image.
Approach:
- Load the image into a variable J by using imread().
- Store the number of rows and columns of the image in variables, r and c.
- Create 3 zero matrices R, G and B (one for each of the 3 colors) of size rXc.
- Store the corresponding color plane of the image in the corresponding zero matrix.
1: Red
2: Green
3: Blue
- Display the images by using imshow(), but typecast them into uint8 first.
Implementation:
I = imread( 'lenna.png' );
r = size(I, 1);
c = size(I, 2);
R = zeros(r, c, 3);
G = zeros(r, c, 3);
B = zeros(r, c, 3);
R(:, :, 1) = I(:, :, 1);
G(:, :, 2) = I(:, :, 2);
B(:, :, 3) = I(:, :, 3);
figure, imshow(uint8(R));
figure, imshow(uint8(G));
figure, imshow(uint8(B));
|
Input :
Output :
Share your thoughts in the comments
Please Login to comment...