Program 1: Image representation, gray level basics
MATLAB CODE:
clear all
close all
A = imread('sunset.jpg');
figure(1)
imshow(A)
B = rgb2gray(A);
figure(2)
imshow(B)
C = im2bw(B);
figure(3)
imshow(C)
figure(4)
subplot(1,3,1),imshow(A),title('Color Image')
subplot(1,3,2),imshow(B),title('Gray Image')
subplot(1,3,3),imshow(C),title('Black and White Image')
figure(5)
subplot(1,2,1),imshow(B),title('Gray Image')
subplot(1,2,2),imhist(B),title('Histogram')
img1_r = A(:, :, 1);
img1_g = A(:, :, 2);
img1_b = A(:, :, 3);
figure(6)
subplot(2, 2, 1), imshow(A), title('Original')
subplot(2, 2, 2), imshow(img1_r), title('Red'), colorbar
subplot(2, 2, 3), imshow(img1_g), title('Green'), colorbar
subplot(2, 2, 4), imshow(img1_b), title('Blue'), colorbar
figure(7)
subplot(3, 2, 1), imshow(img1_r), title('Red')
subplot(3, 2, 2), imhist(img1_r), title('Red_Histogram')
subplot(3, 2, 3), imshow(img1_g), title('Green')
subplot(3, 2, 4), imhist(img1_g), title('Green_Histogram')
subplot(3, 2, 5), imshow(img1_b), title('Blue')
subplot(3, 2, 6), imhist(img1_b), title('Blue_Histogram')
img2 = zeros(size(A));
img2 = uint8(img2);
img2(:, :, 1) = img1_r;
img2(:, :, 2) = img1_g;
img2(:, :, 3) = img1_b;
figure(8)
imshow(img2)
Purpose of the Code:
• Demonstrates image processing techniques:
• Color to grayscale conversion.
• Grayscale to binary conversion.
• RGB channel extraction.
• Histogram plotting.
• Image reconstruction from RGB components
Output:
Code Explanation:
*Clearing workspace and closing figures
clear all: Removes all variables from the workspace.
close all: Closes all open figure windows.
*Reading and displaying the original image – Figure 1
A = imread('Sunset.jpg');
figure(1)
imshow(A)
imread('Sunset.jpg'): Reads the image file named Sunset.jpg into the variable A.
imshow(A): Displays the color image in a figure.
*Converting the image to grayscale – Figure 2
B = rgb2gray(A);
figure(2)
imshow(B)
rgb2gray(A): Converts the RGB image A into a grayscale image B.
imshow(B): Displays the grayscale image.
*Converting the grayscale image to black-and-white (binary) – Figure 3
C = im2bw(B);
figure(3)
imshow(C)
im2bw(B): Converts the grayscale image B into a binary (black-and-white) image C. Pixels are
either 0 (black) or 1 (white) based on a threshold.
imshow(C): Displays the binary image.
*Displaying all versions side-by-side – Figure 4
figure(4)
subplot(1,3,1),imshow(A),title('Color Image')
subplot(1,3,2),imshow(B),title('Gray Image')
subplot(1,3,3),imshow(C),title('Black and White Image')
subplot(1,3,...): Creates a grid of 1 row and 3 columns to display:
The original color image.
The grayscale image.
The binary image.
*Displaying the grayscale image and its histogram – Figure 5
figure(5)
subplot(1,2,1),imshow(B),title('Gray Image')
subplot(1,2,2),imhist(B),title('Histogram')
imhist(B): Plots the histogram of the grayscale image B, showing pixel intensity distribution.
*Extracting RGB components
img1_r = A(:, :, 1);
img1_g = A(:, :, 2);
img1_b = A(:, :, 3);
Splits the original image A into its Red (img1_r), Green (img1_g), and Blue (img1_b)
components.
*Displaying the original image and RGB channels – Figure 6
figure(6)
subplot(2, 2, 1), imshow(A), title('Original')
subplot(2, 2, 2), imshow(img1_r), title('Red'), colorbar
subplot(2, 2, 3), imshow(img1_g), title('Green'), colorbar
subplot(2, 2, 4), imshow(img1_b), title('Blue'), colorbar
Displays:
1. The original image.
2. The Red channel.
3. The Green channel.
4. The Blue channel.
colorbar: Adds a color bar to indicate pixel intensity values.
*Displaying RGB channels and their histograms – Figure 7
figure(7)
subplot(3, 2, 1), imshow(img1_r), title('Red')
subplot(3, 2, 2), imhist(img1_r), title('Red_Histogram')
subplot(3, 2, 3), imshow(img1_g), title('Green')
subplot(3, 2, 4), imhist(img1_g), title('Green_Histogram')
subplot(3, 2, 5), imshow(img1_b), title('Blue')
subplot(3, 2, 6), imhist(img1_b), title('Blue_Histogram')
Displays:
RGB channels (Red, Green, Blue).
Histograms for each channel showing the intensity distribution of pixels.
Reconstructing the image from RGB components - Figure 8
img2 = zeros(size(A));
img2 = uint8(img2);
img2(:, :, 1) = img1_r;
img2(:, :, 2) = img1_g;
img2(:, :, 3) = img1_b;
figure(8)
imshow(img2)
zeros(size(A)): Creates a zero matrix of the same size as A.
uint8: Converts the zero matrix to an unsigned 8-bit integer type for compatibility with image
data.
Reconstructs the RGB image by assigning:
img1_r to the red channel.
img1_g to the green channel.
img1_b to the blue channel.
Displays the reconstructed image to confirm it matches the original.
Program 2: Program to demonstrate Point Processing, Contrast Stretching,
and Grey level slicing.
MATLAB CODE:
clear all;
close all;
clc;
a = imread('cameraman.png');
a = double(a);
s = size(a);
p1 = [0, 0];
p2 = [150, 20];
p3 = [200, 200];
p4 = [255, 255];
m1 = (p1(2) - p2(2)) / (p1(1) - p2(1));
m2 = (p2(2) - p3(2)) / (p2(1) - p3(1));
m3 = (p3(2) - p4(2)) / (p3(1) - p4(1));
c1 = p1(2) - m1 * p1(1);
c2 = p2(2) - m2 * p2(1);
c3 = p3(2) - m3 * p3(1);
t = [];
for x = 0:255
if (x <= p2(1))
t = [t (m1 * x + c1)];
end
if (x > p2(1) && x <= p3(1))
t = [t (m2 * x + c2)];
end
if (x > p3(1) && x <= p4(1))
t = [t (m3 * x + c3)];
end
end
for n = 1:s(1)
for m = 1:s(2)
ot(n, m) = t(a(n, m) + 1);
end
end
plot(t)
grid on;
xlabel('Intensity in input image');
ylabel('Intensity in output image');
figure();
subplot(1,2,1), imshow(a/255), title('Original image');
subplot(1,2,2), imshow(ot./255), title('Contrast stretching')
x=imread('cameraman.png'); y=x;
[w h]=size(x); for i=1:w
for j=1:h
if x(i,j)>=100 && x(i,j)<=200 y(i,j)=255;
else y(i,j)=x(i,j);
end
end
end
figure, imshow(x); figure, imshow(y);
Output:
Program 3: Program to demonstrate Point Processing, Contrast Stretching,
and Grey level slicing.
MATLAB CODE:
clear all; close all; clc;
a = imread('cameraman.tif');
b1 = []; b2 = []; b3 = []; b4 = [];
b5 = []; b6 = []; b7 = []; b8 = [];
for m = 1:256
for n = 1:256
t = de2bi(a(m, n), 8, 'left-msb');
b1(m, n) = t(1, 1);
b2(m, n) = t(1, 2);
b3(m, n) = t(1, 3);
b4(m, n) = t(1, 4);
b5(m, n) = t(1, 5);
b6(m, n) = t(1, 6);
b7(m, n) = t(1, 7);
b8(m, n) = t(1, 8);
end
end
subplot(3, 3, 1); imshow(a);
title('image of cameramen', 'color', 'r');
subplot(3, 3, 2); imshow(b8); title('image of bit-1', 'color', 'r');
subplot(3, 3, 3); imshow(b7); title('image of bit-2', 'color', 'r');
subplot(3, 3, 4); imshow(b6); title('image of bit-3', 'color', 'r');
subplot(3, 3, 5); imshow(b5); title('image of bit-4', 'color', 'r');
subplot(3, 3, 6); imshow(b4); title('image of bit-5', 'color', 'r');
subplot(3, 3, 7); imshow(b3); title('image of bit-6', 'color', 'r');
subplot(3, 3, 8); imshow(b2); title('image of bit-7', 'color', 'r');
subplot(3, 3, 9); imshow(b1); title('image of bit-8', 'color', 'r');
Output:
Program 4: Program to demonstrate Histogram Processing.
MATLAB CODE:
A = imread('image.jpg'); % Read an image
B = rgb2gray(A); % Convert it to grayscale
subplot(1,2,1),imshow(B),title('Original image'); % Display original grayscale image
subplot(1,2,2),imhist(B),title('Histogram'); % Display its histogram
bin = 255; % Define intensity range [0,255]
Val = reshape(B,[],1); % Convert image matrix into a single
column vector
Val = double(Val); % Convert values to double precision
I = hist(Val,0:bin); % Compute histogram
Output = I/numel(B); % Normalize the histogram (PDF)
CSum = cumsum(Output); % Compute cumulative sum (CDF)
HIm = CSum(B+1); % Map input image pixels using CDF
HIm = uint8(HIm*bin); % Convert to uint8 format (0-255 range)
figure
subplot(1,2,1),imshow(HIm),title('Output image'); % Display equalized image
subplot(1,2,2),imhist(HIm),title('Histogram'); % Display its new histogram
Output:
Program 5: Program to Image Smoothing, Noise removal in spatial domain.
MATLAB CODE:
img = imread('cameraman.tif'); % Read grayscale image
imgd = im2double(img); % Convert to double (range [0,1])
f = ones(3,3)/9; % Define a 3x3 averaging filter (mean filter)
img1 = filter2(f, imgd); % Apply filter
subplot(121);imshow(img); % Display original image
subplot(122);imshow(img1); % Display filtered image
img = imread('cameraman.tif');
imgd = im2double(img); % Convert to double precision
imgd = imnoise(imgd,'salt & pepper',0.02); % Add 2% salt & pepper noise
f = ones(3,3)/9; % Define a 3x3 averaging filter
img1 = filter2(f, imgd); % Apply filter
figure
subplot(121);imshow(imgd); % Display noisy image
subplot(122);imshow(img1); % Display filtered image
I = imread('cameraman.tif');
J = imnoise(I,'salt & pepper',0.02); % Add salt & pepper noise
K = medfilt2(J); % Apply median filtering
figure
subplot(121);imshow(J); % Display noisy image
subplot(122);imshow(K); % Display filtered image
I = imread('cameraman.tif');
radius = 1;
J1 = fspecial('disk', radius); % Create a disk filter with radius 1
K1 = imfilter(I,J1,'replicate'); % Apply disk filter
radius = 10;
J10 = fspecial('disk', radius); % Create a disk filter with radius 10
K10 = imfilter(I,J10,'replicate'); % Apply disk filter
figure
subplot(131);imshow(I);title('original');
subplot(132);imshow(K1);title('disk: radius=1');
subplot(133);imshow(K10);title('disk: radius=10');
I = imread('hawk.jpg');
J = imnoise(I,'salt & pepper',0.2); % Add 20% salt & pepper noise
% Filter each channel separately
r = medfilt2(J(:, :, 1), [3 3]); % Apply median filter to red channel
g = medfilt2(J(:, :, 2), [3 3]); % Apply median filter to green channel
b = medfilt2(J(:, :, 3), [3 3]); % Apply median filter to blue channel
% Reconstruct the image from r, g, b channels
K = cat(3, r, g, b);
figure
subplot(121);imshow(J); % Display noisy image
subplot(122);imshow(K); % Display filtered image
Output:
Program 6: Program to demonstrate Edge detection techniques.
MATLAB CODE:
I = imread('coins.jpg'); % Read input image
I = rgb2gray(I); % Convert to grayscale
figure, imshow(I)
title ('figure 1 original image');
% Define a 5×5 averaging filter
h = ones(5,5)/25;
(mean filter)
b = imfilter(I, h); % Apply the filter using convolution
figure, imshow(b)
title ('figure 2 filtered image');
c = edge(b, 'sobel'); % Apply Sobel operator
figure, imshow(c)
title ('figure 3 edge detected output by sobel operator');
d = edge(b, 'prewitt'); % Apply Prewitt operator
figure, imshow(d)
title ('figure 4 edge detected output by prewitt operator');
e = edge(b, 'roberts'); % Apply Roberts operator
figure, imshow(e)
title ('figure 5 edge detected output by robert operator');
f = edge(b, 'canny'); % Apply Canny operator
figure, imshow(f)
title ('figure 6 edge detected output by canny operator');
Output:
Program 7: Program to demonstrate Morphological operations.
MATLAB CODE:
clc;
clear all;
close all;
a = imread ('letter.jpg');
b = im2bw(a,0.4);
subplot(2,3,1);
imshow(b);
title('original binary image');
c = bwmorph(b,'remove');
subplot(2,3,2);
imshow(c);
title('outline of original image');
d = bwmorph(b,'skel',Inf);
subplot(2,3,3);
imshow(d);
title('skeleton of original image');
se = strel('line',11,90);
e = imdilate(b,se);
subplot(2,3,4);
imshow(e),
title('dilation of original image');
f = imerode(b,se);
subplot(2,3,5);
imshow(f),
title('erosion of original image');
g = bwmorph(b,'bothat');
subplot(2,3,6);
imshow(g);
title('bottom hat operation on original image');
Output:
Program 8: Program to demonstrate Frequency domain filters for
smoothing, high pass of images.
Highpass filter:
% Reading input image : input_image
in = imread('letter.jpg');
input_image=rgb2gray(in);
% Saving the size of the input_image in pixels-
% M : no of rows (height of the image)
% N : no of columns (width of the image)
[M, N] = size(input_image);
% Getting Fourier Transform of the input_image
% using MATLAB library function fft2 (2D fast fourier transform)
FT_img = fft2(double(input_image));
% Assign Cut-off Frequency
D0 = 10; % one can change this value accordingly
% Designing filter
u = 0:(M-1);
idx = find(u>M/2);
u(idx) = u(idx)-M;
v = 0:(N-1);
idy = find(v>N/2);
v(idy) = v(idy)-N;
% MATLAB library function meshgrid(v, u) returns 2D grid
% which contains the coordinates of vectors v and u.
% Matrix V with each row is a copy of v, and matrix U
% with each column is a copy of u
[V, U] = meshgrid(v, u);
% Calculating Euclidean Distance
D = sqrt(U.^2+V.^2);
% Comparing with the cut-off frequency and
% determining the filtering mask
H = double(D > D0);
% Convolution between the Fourier Transformed image and the mask
G = H.*FT_img;
% Getting the resultant image by Inverse Fourier Transform
% of the convoluted image using MATLAB library function
% ifft2 (2D inverse fast fourier transform)
output_image = real(ifft2(double(G)));
% Displaying Input Image and Output Image
subplot(2, 1, 1), imshow(input_image),
subplot(2, 1, 2), imshow(output_image, [ ]);
Lowpass filter:
% Reading input image : input_image
in = imread('letter.jpg');
input_image=rgb2gray(in);
% Saving the size of the input_image in pixels-
% M : no of rows (height of the image)
% N : no of columns (width of the image)
[M, N] = size(input_image);
% Getting Fourier Transform of the input_image
% using MATLAB library function fft2 (2D fast fourier transform)
FT_img = fft2(double(input_image));
% Assign Cut-off Frequency
D0 = 30; % one can change this value accordingly
% Designing filter
u = 0:(M-1);
idx = find(u>M/2);
u(idx) = u(idx)-M;
v = 0:(N-1);
idy = find(v>N/2);
v(idy) = v(idy)-N;
% MATLAB library function meshgrid(v, u) returns
% 2D grid which contains the coordinates of vectors
% v and u. Matrix V with each row is a copy
% of v, and matrix U with each column is a copy of u
[V, U] = meshgrid(v, u);
% Calculating Euclidean Distance
D = sqrt(U.^2+V.^2);
% Comparing with the cut-off frequency and
% determining the filtering mask
H = double(D <= D0);
% Convolution between the Fourier Transformed
% image and the mask
G = H.*FT_img;
% Getting the resultant image by Inverse Fourier Transform
% of the convoluted image using MATLAB library function
% ifft2 (2D inverse fast fourier transform)
output_image = real(ifft2(double(G)));
% Displaying Input Image and Output Image
subplot(2, 1, 1), imshow(input_image),
subplot(2, 1, 2), imshow(output_image, [ ]);