Professional Documents
Culture Documents
AIM: Write a program to read an RGB image and perform the following operations on the
image:
a) Extract red, green and blue components of the image and then combine them to get the
original image.
b) Convert the RGB image into grayscale image and resize the image to 512x512 pixels.
c) Add and subtract another RGB image into the original image.
RGB Image
In RGB or color, images, the information for each pixel requires a tuple of numbers to
represent. So we need a three-dimensional matrix to represent an image. Almost all colors in
nature can be composed of three colors: red (R), green (G), and blue (B). So each pixel can be
represented by a red/green/blue tuple in an RGB image.
Grayscale Image
The grayscale image adds a color depth between black and white in the binary image to form a
grayscale image. Such images are usually displayed as grayscales from the darkest black to the
brightest white, and each color depth is called a grayscale, usually denoted by L. In grayscale
images, pixels can take integer values between 0 and L-1.
COMMANDS:
imread(filename,fmt): reads a greyscale or color image from the file specified by the
string filename, where the string fmt specifies the format of the file. If the file is not in
the current directory or in a directory in the MATLAB path.
cat(dim,A,B) concatenates B to the end of A along dimension dim when A and B have
compatible sizes.
imresize(A,scale): returns image B that is scale times the size of A. The input
image A can be a grayscale, RGB, or binary image.
CODE:
img=imread("build.jpg");
R=img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);
a = zeros(size(img, 1), size(img, 2));
just_red = cat(3, R, a, a);
subplot(2,3,1), imshow(just_red);
title('Red Image');
just_green = cat(3, a, G, a);
subplot(2,3,2), imshow(just_green);
title('Green Image');
just_blue = cat(3, a, a, B);
subplot(2,3,3), imshow(just_blue);
title('Blue Image');
x1=rgb2gray(img);
subplot(2,3,4), imshow(x1)
title('Greyscale Image');
x2=cat(3,R,G,B);
subplot(2,3,5), imshow(x2)
title('RGB Image');
r_vec = [512, 512];
resized_img = imresize(x1,r_vec);
subplot(2,3,6);
imshow(resized_img);
title('Resized Image');
OUTPUT:
RESULT:
Different operations on image has been performed and results have been verified.