Karnatak Arts, Science and Commerce College Bidar: Digital Image Processing Laboratory Manual - SCP 4.1
Karnatak Arts, Science and Commerce College Bidar: Digital Image Processing Laboratory Manual - SCP 4.1
1: DIP Programs
KRE Society’s
Karnatak Arts, Science and Commerce
College Bidar
Department of PG Studies and Research in ComputerScience
M.Sc. IV Semester
1|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
2|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
PROGRAMS INDEX: -
1. Program for opening and closing of the image.
2. Program to fill the region of interest for the image.
3. Program for edge detection algorithm.
4. Program of sharpen image using gradient mask.
5. Program for Display of an Image, Negative of an Image(Binary & Gray
Scale)
6. Program for Implementation of Relationships between Pixels
7. Program for Implementation of Transformations of an Image
8. Program for Contrast stretching of a low contrast image, Histogram, and
Histogram Equalization
9. Program for Display of bit planes of an Image
10.Program for Display of FFT(1-D & 2-D) of an image
11.Program for Computation of Mean, Standard Deviation, Correlation
coefficient of the given Image.
12.Program for Implementation of Image Smoothening Filters (Mean and
Median filtering of an Image.
13.Program for Implementation of image sharpening filters and Edge
Detection using Gradient Filters.
14.Program for Image Compression by DCT, DPCM, HUFFMAN coding.
15. Program for Implementation of image restoring techniques.
16.Program for Implementation of Image Intensity slicing technique for
image enhancement.
17.Program for Canny edge detection Algorithm.
18.Program to obtain histogram equalization concept.
19.Program To Implement smoothing or averaging filter in spatial domain.
20.Program for morphological operation: erosion and dilation.
21.Program for DCT/IDCT computation.
22.Program to compute the edges in the image using Roberts, Sobel, Canny,
Log, Prewitt edge detection methods.
23. Program to perform watershed transformation on an image.
24. Program for the dilation and erosion process with a character image
example.
25. Program to separate the RGB plane from the color image.
26.Program to demonstrate run length encoding and decoding.
27.Program to read a document image and perform morphological whole
filling operation.
28.Program to demonstrate geometric transformations like scale, rotate,
affine.
3|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
subplot(2, 3, 4);
imshow (output_image);
title('Opened Image');
output_image=custom_closing (input_image,se);
subplot(2, 3, 6);
imshow(output_image);
title('Closed Image');
4|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
Output:-
5|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
2. Program to fill the region of interest for the image.
Matlab code:-
clc
close all
image = imread('region.png');
image=rgb2gray(image);
subplot(1, 2, 1);
imshow(image);
title('Original Image');
subplot(1, 2, 2);
imshow(outputImage);
title('Image with Filled Region');
seedX = seedPoint(1);
seedY = seedPoint(2);
seedValue = image(seedY, seedX);
if seedValue == fillColor || seedX < 1 || seedX > cols || seedY < 1 || seedY > rows
return;
end
while ~isempty(stack)
currentPoint = stack(end, :);
stack(end, :) = [];
currentX = currentPoint(1);
currentY = currentPoint(2);
if currentX >= 1 && currentX <= cols && currentY >= 1 && currentY <= rows &&
outputImage(currentY, currentX) == seedValue
outputImage(currentY, currentX) = fillColor;
for i = 1:size(neighbors, 1)
neighborX = currentX + neighbors(i, 1);
neighborY = currentY + neighbors(i, 2);
stack = [stack; neighborX, neighborY];
end
end
end
end
6|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
Output:-
7|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
3. Program for edge detection algorithm.
Matlab code:-
clc
close all
originalImage = imread('mixedfruit.bmp');
grayImage = rgb2gray(originalImage);
for i = 2:rows-1
for j = 2:cols-1
gx = sum(sum(double(grayImage(i-1:i+1, j-1:j+1)) .* sobelHorizontal));
gy = sum(sum(double(grayImage(i-1:i+1, j-1:j+1)) .* sobelVertical));
subplot(3, 1, 1);
imshow(originalImage);
title('Original Image');
subplot(3, 1, 2);
imshow(grayImage);
title('Original Grayscale Image');
subplot(3, 1, 3);
imshow(gradientMagnitude);
title('Edge Detected Image');
Output:-
8|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
4. Program for sharpen image using gradient mask.
Matlab code:-
//for color image
clc
close all
originalImage = imread('Images/apple3.bmp');
originalImage = im2double(originalImage);
subplot(1, 2, 1);
imshow(originalImage);
title('Original Image');
subplot(1, 2, 2);
imshow(sharpenedImage);
title('Sharpened Image');
Output:-
9|Pa ge
M.Sc. Computer Science SCT 4.1: DIP Programs
//converting gray scale image
clc
close all
input_image = imread('Images/key.jpg');
originalImage = im2double(input_image);
gray_image=rgb2gray(originalImage);
sharpenedImage=conv2(gray_image,laplacianKernel,'same');
sharpenedImage = gray_image-sharpenedImage;
subplot(3, 1, 1);
imshow(input_image);
title('Original Image');
subplot(3, 1, 2);
imshow(gray_image);
title('Gray Scale Image');
subplot(3, 1, 3);
imshow(sharpenedImage);
title('Sharpened Image using Gradient Mask');
10 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
5. Program for display of an image, negative of an image (Binary & Gray Scale).
Matlab code:-
clc
close all
input_image=imread('lotus.jpg');
subplot(1, 2, 1);
imshow(input_image);
title('Original Image');
negative_image=255-input_image;
subplot(1, 2, 2);
imshow(negative_image);
title('Negative of Original Image');
Output:-
11 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
6. Program for implementation of Transformation of an image.
Matlab code:-
clc
close all
original_image=imread('myna.jpg');
input_image=rgb2gray(original_image);
subplot(2, 3, 2);
imshow(input_image);
title('Original Image');
%Logarithmic Transform
L=255;
c=L/log10(1+L);
log_transform=c*log10(1+double(input_image));
subplot(2, 3, 5);
imshow(uint8(log_transform));
title('Log Transformation');
Output:-
12 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
7. Program for contrast stretching of a low contrast image, Histogram, and histogram
Equalization.
Matlab code:-
clc
close all
clear all
inputImage = imread('Images/eye1.jpg');
if size(inputImage, 3) == 3
inputImage = rgb2gray(inputImage);
end
grayImage = im2double(inputImage);
subplot(3, 2, 1);
imshow(grayImage);
title('Original Image');
subplot(3, 2, 2);
bar(originalHistogram);
title('Histogram (Original Image)');
subplot(3, 2, 3);
imshow(contrastStretchedImage);
title('Contrast Stretched Image');
subplot(3, 2, 4);
bar(contrastStretchedHistogram);
title('Histogram (Contrast Stretched Image)');
subplot(3, 2, 5);
imshow(histEqualizedImage);
title('Histogram Equalized Image');
subplot(3, 2, 6);
bar(equalizedHistogram);
title('Histogram (Histogram Equalized Image)');
13 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
histogram(intensity) = histogram(intensity) + 1;
end
end
end
for i = 1:rows
for j = 1:cols
intensity = round(image(i, j) * 255) + 1;
equalizedImage(i, j) = cdf(intensity);
end
end
end
Output:-
14 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
8. Program for display of bit planes of an image.
Matlab code:-
clc
close all
input_image=imread('penguin.jpg');
[row,col]=size(input_image);
15 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
bit_0(i,j)=bitand(input_image(i,j),1);
end
end
Output :-
16 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
9. Program for display of FFT (1D & 2D) of an image.
Matlab code :-
clc
close all
input_image = imread('lenna.jpg');
image = im2double(input_image);
subplot(2, 2, 1);
imshow(image);
title('Original Image');
subplot(2, 2, 2);
imshow(abs(fft1D));
title('1D FFT');
subplot(2, 2, 3);
imshow(log(1 + abs(fft2D)), []);
title('2D FFT Magnitude');
subplot(2, 2, 4);
imshow(angle(fft2D), []);
title('2D FFT Phase');
fft1D = zeros(size(image));
for row = 1:size(image, 1)
fft1D(row, :) = myFFT1D(image(row, :));
end
fft2D = zeros(size(image));
for col = 1:size(fft1D, 2)
fft2D(:, col) = myFFT1D(fft1D(:, col).');
end
end
17 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
for k = 1:N
for n = 1:N
angle = -2*pi*(k-1)*(n-1)/N;
fft1D(k) = fft1D(k) + signal(n) * exp(1i * angle);
end
end
end
Output:-
18 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
10. Program for computation of mean, standard deviation, Correlation coefficient of the given
image.
Matlab code:-
clc
close all
image = imread('dove1.jpg');
image = im2double(image);
% Compute the correlation coefficient between the original and modified images
correlationValue = customCorrelation(image, modifiedImage);
subplot(1,1,1);
imshow(image);
title('Original Image');
xlabel(sprintf('Mean : %s\nStandard Deviation: %s\nCorrelation Coefficient: %s',num2str(meanValue),
num2str(stdValue),num2str(correlationValue)));
19 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
Output:-
20 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
11. Program for implementation of Image Smoothening Filters (Mean and Median Filtering) of
an Image.
Matlab code:-
clc
close all
inputImage = imread('rose.jpg');
inputImage = im2double(inputImage);
% Set the kernel size for the filters (e.g., 3x3 or 5x5)
kernelSize = 5;
subplot(1, 3, 1);
imshow(inputImage);
title('Original Image');
subplot(1, 3, 2);
imshow(meanFiltered);
title('Mean Filtered Image');
subplot(1, 3, 3);
imshow(medianFiltered);
title('Median Filtered Image');
for y = 1:rows
for x = 1:cols
neighbors = paddedImage(y:y + kernelSize - 1, x:x + kernelSize - 1);
outputImage(y, x) = mean(neighbors(:));
end
end
end
for y = 1:rows
for x = 1:cols
neighbors = paddedImage(y:y + kernelSize - 1, x:x + kernelSize - 1);
outputImage(y, x) = median(neighbors(:));
end
21 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
end
end
Output:-
22 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
12. Program for implementation of Image Sharpening filters and Edge Detection using Gradient
Filters.
Matlab code :-
clc
close all
inputImage = imread('Images/babyelephant.jpg');
inputImage = im2double(inputImage);
subplot(1, 3, 1);
imshow(inputImage),title('Original Image');
subplot(1, 3, 2);
imshow(sharpened),title('Sharpened Image');
subplot(1, 3, 3);
imshow(edgeMagnitude, []);
title('Edge Detected Image');
end
23 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
Output:-
24 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
13. Program for image compression by DCT, DPCM, HUFFMAN coding.
Matlab code :-
clc
close all
inputImage = imread('cameraman.jpg');
inputImage = im2double(inputImage);
blocksize = 16;
quantizationFactor = 0.2;
% Compress the image
compressedImage = compressImageByDCT(inputImage, blocksize, quantizationFactor);
subplot(1, 2, 1);
imshow(inputImage);
title('Original Image');
subplot(1, 2, 2);
imshow(compressedImage);
title('Compressed Image');
for i = 1:numBlocksY
for j = 1:numBlocksX
block = inputImagePadded((i - 1) * blocksize + 1:i * blocksize, (j - 1) * blocksize + 1:j * blocksize);
dctBlock = dct2(block);
quantizedBlock = round(dctBlock / quantizationFactor);
compressedImage((i - 1) * blocksize + 1:i * blocksize, (j - 1) * blocksize + 1:j * blocksize) =
quantizedBlock;
end
end
end
Output :-
25 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
input_image = imread('apple3.bmp');
degraded_image = im2double(input_image);
kernel_size = 5;
kernel = fspecial('gaussian', kernel_size, 1);
noise_variance = 0.02;
noisy_degraded_image = degraded_image + sqrt(noise_variance) * randn(size(degraded_image));
F_input = fft2(input_image);
F_kernel = fft2(kernel, size(input_image, 1), size(input_image, 2));
F_restored = F_input .* H;
restored_image = abs(ifft2(F_restored));
end
Output:-
26 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
15. Program for implementation of image Intensity slicing technique for image enhancement.
Matlab code :-
clc
close all
input_image = imread('puppy.jpg');
if size(input_image, 3) == 3
input_image = rgb2gray(input_image);
end
lower_threshold = 120;
upper_threshold = 200;
new_intensity = 255;
for y = 1:height
for x = 1:width
intensity = input_image(y, x);
if intensity >= lower_threshold && intensity <= upper_threshold
enhanced_image(y, x) = new_intensity;
else
enhanced_image(y, x) = intensity;
end
end
end
enhanced_image = cast(enhanced_image, class(input_image));
end
Output:-
27 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
16. Program for Canny Edge Detection Algorithm.
Matlab code :-
clc
close all
clear all
inputImage = imread('Images/crow.jpg');
if size(inputImage, 3) == 3
inputImage = rgb2gray(inputImage);
end
subplot(1, 2, 1);
imshow(inputImage);
title('Original Image');
subplot(1, 2, 2);
imshow(cannyEdges);
title('Canny Edges');
Output:-
28 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
17. Program to obtain histogram equalization concept.
Matlab code :-
clc
close all
input_image = imread('Images/cameraman.jpg');
if size(input_image, 3) == 3
input_image = rgb2gray(input_image);
end
temp=histogram(1);
for i = 2:256
temp=temp+histogram(i);
histogram(i)=temp;
end
%mapping
for i = 1:rows
for j = 1:cols
input_image(i,j) = round(histogram(input_image(i,j)+1) *255);
end
end
29 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
Output:-
30 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
18. Program to implement smoothing or averaging filter in spatial domain.
Matlab code:-
clc
close all
clear all
originalImage = imread('Images/hibiscus.tif');
if size(originalImage, 3) == 3
originalImage = rgb2gray(originalImage);
end
grayImage = im2double(originalImage);
filterSize = 5; % Change this value for different filter sizes
% Compute the average of the neighborhood and assign it to the smoothed image
smoothedValue = mean(neighborhood(:));
smoothedImage(row, col) = smoothedValue;
end
end
subplot(1, 2, 1);
imshow(grayImage);
title('Original Image');
subplot(1, 2, 2);
imshow(smoothedImage);
title('Smoothed Image');
Output:-
31 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
Matlab code:-
clc
close all
image = imread('morph2.bmp');
se = ones(3);
dilated_image = custom_dilation(image, se);
eroded_image = custom_erosion(image, se);
subplot(2, 3, 2);
imshow(image);
title('Original Image');
subplot(2, 3, 4);
imshow(dilated_image);
title('Dilated Image');
subplot(2, 3, 6);
imshow(eroded_image);
title('Eroded Image');
for i = 1:M
for j = 1:N
region = padded_image(i:i+2*pad_m, j:j+2*pad_n);
output_image(i, j) = max(region(:));
end
end
end
for i = 1:M
for j = 1:N
region = padded_image(i:i+2*pad_m, j:j+2*pad_n);
output_image(i, j) = min(region(:));
end
end
32 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
end
Output:-
33 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
20. Program for DCT/IDCT computation.
Matlab code:-
clc
close all
% Read the input image
inputImage = imread('lenna.jpg');
compressionFactor = 0.05;
numCoefficientsToKeep = round(compressionFactor * numel(dctImage));
sortedCoefficients = sort(abs(dctImage(:)), 'descend');
threshold = sortedCoefficients(numCoefficientsToKeep);
dctImage(abs(dctImage) < threshold) = 0;
subplot(1, 3, 1);
imshow(grayImage);
title('Original Image');
subplot(1, 3, 2);
imshow(dctImage);
title('DCT of Image');
subplot(1, 3, 3);
imshow(reconstructedImage);
title('IDCT of Image');
Output:-
34 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
21. Program to compute the edges in the images using Roberts, Sobel, Canny, Log, Perwitt edge
detection methods.
Matlab code:-
clc
close all
inputImage = imread('Images/crow.jpg');
if size(inputImage, 3) == 3
inputImage = rgb2gray(inputImage);
end
subplot(3, 2, 1);
imshow(inputImage),title('Original Image');
subplot(3, 2, 2);
imshow(robertEdges),title('Roberts Edges');
subplot(3, 2, 3);
imshow(sobelEdges),title('Sobel Edges');
subplot(3, 2, 4);
imshow(cannyEdges),title('Canny Edges');
subplot(3, 2, 5);
imshow(perwittEdges),title('Perwitt Edges');
subplot(3, 2, 6);
imshow(logEdges),title('Log Edges');
Output:-
35 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
22. Program to perform watershed transformation on an image.
Matlab code:-
clc
close all
clear all
inputImage = imread('Images/water_coins.jpg');
grayImage = rgb2gray(inputImage);
thresholdValue = graythresh(grayImage);
binaryMask = imbinarize(grayImage, thresholdValue);
% Find the regional minima to use as markers for the watershed transformation
regionalMinima = imregionalmin(distanceTransform);
overlayImage = inputImage;
overlayImage(labels == 0) = 0;
subplot(2, 2, 1);
imshow(inputImage);
title('Original Image');
subplot(2, 2, 3);
imshow(binaryMask);
title('Binary Mask');
subplot(2, 2, 4);
imshow(overlayImage);
title('Watershed Regions');
colormap(gca, jet); % Apply color map for visualization
colorbar;
Output:-
36 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
23. Program for the dilation and erosion process with a character image example.
Matlab code:-
clc
close all
character_image = imread('morph1.bmp');
se = ones(3);
subplot(2, 3, 4);
imshow(dilated_image);
title('Dilated Image');
subplot(2, 3, 6);
imshow(eroded_image);
title('Eroded Image');
for i = 1:M
for j = 1:N
region = padded_image(i:i+2*pad_m, j:j+2*pad_n);
output_image(i, j) = max(region(:));
end
end
end
for i = 1:M
for j = 1:N
37 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
region = padded_image(i:i+2*pad_m, j:j+2*pad_n);
output_image(i, j) = min(region(:));
end
end
end
Output:-
38 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
24. Program to separate the RGB plane from the color image.
clear all
close all
input_image=imread('mixedfruit.bmp');
red_color =input_image;da
green_color =input_image;
blue_color =input_image;
red_color(:,:,2)=0;
red_color(:,:,3)=0;
green_color(:,:,1)=0;
green_color(:,:,3)=0;
blue_color(:,:,1)=0;
blue_color(:,:,2)=0;
subplot(2, 3, 2);
imshow(input_image);
title('Original RGB Image');
subplot(2, 3, 4);
imshow(red_color);
title('Red Color Plane');
subplot(2, 3, 5);
imshow(green_color);
title('Green Color Plane');
subplot(2, 3, 6);
imshow(blue_color);
title('Blue Color Plane');
Output:-
39 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
25. Program to demonstrate run length encoding and decoding.
Matlab code:-
clc
input_numbers = [10, 10, 2, 2, 2, 30, 4, 4, 4, 4];
encoded_numbers = runLengthEncodeNumbers(input_numbers);
decoded_numbers = runLengthDecodeNumbers(encoded_numbers);
Output :-
Original : 10 10 2 2 2 30 4 4 4 4
Encoded : 2 10 3 2 1 30 4 4
Decoded : 10 10 2 2 2 30 4 4 4 4
40 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
26. Program to read a document image and perform morphological hole filling operation.
Matlab code:-
clc
close all
clear all
inputImage = imread('keyimg1.PNG');
grayImage = rgb2gray(inputImage);
subplot(1, 2, 1);
imshow(inputImage);
title('Original Image');
subplot(1, 2, 2);
imshow(filledImage);
title('Filled Image');
Output:-
41 | P a g e
M.Sc. Computer Science SCT 4.1: DIP Programs
27. Program to demonstrate geometric transformations like scaling, rotate, affine.
Matlab code :-
clc
close all
inputImage = imread('Images/cameraman.jpg');
subplot(2, 3, 2);
imshow(inputImage);
title('Original Image');
% Scaling (Resize the image)
scaleFactor = 1.5; % You can adjust this value for different scaling factors
scaledImage = imresize(inputImage, scaleFactor);
subplot(2, 3, 4);
imshow(scaledImage);
title('Scaled Image');
% Rotation
angle = 120; % You can adjust this value for different rotation angles (in degrees)
rotatedImage = imrotate(inputImage, angle, 'bilinear', 'crop');
subplot(2, 3, 5);
imshow(rotatedImage);
title('Rotated Image');
xlabel(sprintf('Rotated with %g deg angle',angle));
% Affine Transformation
% Define the affine transformation matrix
affineMatrix = [cosd(angle) sind(angle) 0;
-sind(angle) cosd(angle) 0;
0 0 1];
tform = affine2d(affineMatrix);
affineImage = imwarp(inputImage, tform);
subplot(2, 3, 6);
imshow(affineImage);
title('Affine Transformed Image');
Output:-
42 | P a g e