IMAGE PROCESSING LAB
Bachelor of Technology
in
Computer Science Engineering
Name- Ankit Singh
[Link]: 7th SEMESTER
Roll No.: 26782009
Enrolment No: U2251059
Department Of Electronics and Communication
JK Institute of Applied Physics and Technology
University of Allahabad, Prayagraj
DEC,2025
1)Write a MATLAB program to read RGB image and then
i) convert it into grayscale image
% Reading the RGB image
rgb_image = imread('[Link]');
% Converting RGB to grayscale
gray_image = rgb2gray(rgb_image);
% Displaying the original RGB and grayscale images
figure;
subplot(1, 2, 1);
imshow(rgb_image);
title('Original RGB Image');
subplot(1, 2, 2);
imshow(gray_image);
title('Grayscale Image');
(ii) perform image negation on grayscale image obtained in part (i)
negated_image = 255 - gray_image;
% Displaying the negated image
figure;
imshow(negated_image);
title('Negated Grayscale Image');
2) Write a MATLAB program for power law transformation of an image.
% Read
I = imread('[Link]');
I_dbl = im2double(I);
% Parameters
gamma = 0.6;
c = 1;
% Transform
powerLaw = c * (I_dbl .^ gamma);
figure, imshow(powerLaw), title('PowerLaw Transformation');
3) Write a MATLAB program to extract RGB components of an image and also
combine the RGB components to reconstruct the original image.
rgb_image = imread('[Link]');
% RGB components
red_channel = rgb_image(:, :, 1);
green_channel = rgb_image(:, :, 2);
blue_channel = rgb_image(:, :, 3);
% Individual channels
figure;
subplot(2, 2, 1);
imshow(rgb_image);
title('Original RGB Image');
subplot(2, 2, 2);
imshow(red_channel);
title('Red Channel');
subplot(2, 2, 3);
imshow(green_channel);
title('Green Channel');
subplot(2, 2, 4);
imshow(blue_channel);
title('Blue Channel');
% Combined RGB components to reconstruct the image
reconstructed_image = cat(3, red_channel, green_channel, blue_channel);
% Display the reconstructed image
figure;
imshow(reconstructed_image);
title('Reconstructed RGB Image');
4) Write a MATLAB program to plot the histogram of a grayscale image.
% Read
I = rgb2gray(imread('[Link]'));
% Initialize
counts = zeros(1, 256);
[rows, cols] = size(I);
% Loop
for i = 1:rows
for j = 1:cols
val = I(i, j) + 1; % +1 for 1-based indexing
counts(val) = counts(val) + 1;
end
end
% Plot
figure, bar(0:255, counts), title('Histogram');
5) Write a MATLAB program for contrast enhancement of an image using
histogram equalization.
% Read
I = rgb2gray(imread('[Link]'));
% Equalize
eqI = histeq(I);
% Display
figure, imshow(eqI), title('Equalized');
figure, imhist(eqI), title('EqHistogram');
6) Write a MATLAB program for image smoothing using spatial filters.
% Read
I = rgb2gray(imread('[Link]'));
% AverageFilter
h = fspecial('average', [3 3]);
smoothI = imfilter(I, h);
% Display
figure, imshow(smoothI), title('Smoothed');
7) Write a MATLAB program for image sharpening using spatial filters.
% Read
I = rgb2gray(imread('[Link]'));
% LaplacianFilter
h = fspecial('laplacian');
sharpI = imfilter(I, h);
% Enhance
finalI = I - sharpI;
figure, imshow(finalI), title('Sharpened');
8) Write a MATLAB program to detect the edges in the given image using
different operators such as Sobel, Prewitt and Roberts Operators.
% Read
I = rgb2gray(imread('[Link]'));
% Operators
sobel = edge(I, 'sobel');
prewitt = edge(I, 'prewitt');
roberts = edge(I, 'roberts');
% Display
figure;
subplot(1,3,1), imshow(sobel), title('Sobel');
subplot(1,3,2), imshow(prewitt), title('Prewitt');
subplot(1,3,3), imshow(roberts), title('Roberts');
9) Write a MATLAB program to convert grayscale image into binary using
thresholding.
% Read
I = rgb2gray(imread('[Link]'));
% Thresholding
level = graythresh(I);
bw = imbinarize(I, level);
% Display
figure, imshow(bw), title('Binary');
10) Write a MATLAB program to
a) zoom-in a gray scale image by a factor of 2.
b) zoom-out a gray scale image by a factor of 2.
% Read
I = rgb2gray(imread('[Link]'));
% ZoomIn
zoomIn = imresize(I, 2);
% ZoomOut
zoomOut = imresize(I, 0.5);
% Display
figure, imshow(zoomIn), title('ZoomedIn');
figure, imshow(zoomOut), title('ZoomedOut');
11) Write a MATLAB program to flip an image vertically & horizontally.
% Read
I = imread('[Link]');
% FlipVertical
flipV = flipud(I);
% FlipHorizontal
flipH = fliplr(I);
% Display
figure, imshow(flipV), title('Vertical');
figure, imshow(flipH), title('Horizontal');
12) Write a MATLAB program to filter out the image corrupted by salt and
pepper noise using median filter. Compare the performance with averaging
filter.
% Read
I = rgb2gray(imread('[Link]'));
% AddNoise
noisyI = imnoise(I, 'salt & pepper', 0.05);
% MedianFilter
medI = medfilt2(noisyI);
% AverageFilter
h = fspecial('average', [3 3]);
avgI = imfilter(noisyI, h);
% Compare
figure;
subplot(1,3,1), imshow(noisyI), title('Noisy');
subplot(1,3,2), imshow(medI), title('Median');
subplot(1,3,3), imshow(avgI), title('Average');
13) a. Write a MATLAB program for DFT-Domain low pass filtering.
b. Write a MATLAB program for DFT-Domain high pass filtering.
% Read
I = im2double(rgb2gray(imread('[Link]')));
[M, N] = size(I);
% DFT
F = fftshift(fft2(I));
% Distance
[U, V] = meshgrid(-N/2:N/2-1, -M/2:M/2-1);
D = sqrt(U.^2 + V.^2);
D0 = 30; % Cutoff
% LowPass
H_low = double(D <= D0);
G_low = F .* H_low;
I_low = real(ifft2(ifftshift(G_low)));
% HighPass
H_high = double(D > D0);
G_high = F .* H_high;
I_high = real(ifft2(ifftshift(G_high)));
% Display
figure, imshow(I_low), title('LowPass');
figure, imshow(I_high + 0.5), title('HighPass');
14) Write a MATLAB program to find the Discrete Cosine Transform of an
image. Display the low frequency and high frequency components of an
image.
% Read
I = im2double(rgb2gray(imread('[Link]')));
% DCT
J = dct2(I);
% Visualize
% Low frequencies are in the top-left corner
logJ = log(abs(J));
figure, imshow(logJ,[]), colormap(gca, jet), colorbar, title('DCT Spectrum');
% Reconstruction (Optional Check)
K = idct2(J);
figure, imshow(K), title('Reconstructed');