0% found this document useful (0 votes)
6 views

Assign 1

Uploaded by

Kanish Mahato
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assign 1

Uploaded by

Kanish Mahato
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Assignment 1:

Digital Image Processing

Submitted by

Kanish Mahato
21103070
CSE

1/13
1. Create a chessboard image with 64 blocks consisting of white and
black squares with 50-pixel width and height. Save the image as
chess.jpg. Read the image and display it.

Code :-

pixel = 50;
chess = repmat(eye(2), 4, 4);
chess_image = kron(chess, ones(pixel));
imwrite(chess_image, 'chess.jpg');

image = imread('chess.jpg');
figure;
imshow(image);
colormap(gray);
title('Chessboard');

Output:-

2/13
2. Read a gray scale image and verify that the image is a 256-level image.
Display the image in gray levels 128, 64, 32, 16, 8, 4, 2. What are your
observations regarding the outputs produced?

Code:-

image = imread('girl.png');
image = im2gray(image)
if max(image(:)) <= 255 && min(image(:)) >= 0
disp('The image is a 256-level grayscale image.');
else
disp('The image is not a 256-level grayscale image.');
end

figure;
subplot(3, 3, 1);
imshow(image);
title('256 Levels');

3/13
image128 = uint8(floor(double(image) / 2) * 2);
subplot(3, 3, 2);
imshow(image128);
title('128 Levels');

image64 = uint8(floor(double(image) / 4) * 4);


subplot(3, 3, 3);
imshow(image64);
title('64 Levels');

image32 = uint8(floor(double(image) / 8) * 8);


subplot(3, 3, 4);
imshow(image32);
title('32 Levels');

image16 = uint8(floor(double(image) / 16) * 16);


subplot(3, 3, 5);
imshow(image16);
title('16 Levels');

image8 = uint8(floor(double(image) / 32) * 32);


subplot(3, 3, 6);
imshow(image8);
title('8 Levels');

image4 = uint8(floor(double(image) / 64) * 64);


subplot(3, 3, 7);
imshow(image4);
title('4 Levels');

image2 = uint8(floor(double(image) / 128) * 128);


subplot(3, 3, 8);
imshow(image2);
title('2 Levels');

My Obervation:-

The image quality decreasing as the number of gray levels is reduced


and becoming black image in last level.

4/13
Output:-

3.Read a gray scale image and perform the following intensity


transformations on the image:

a. Image Negative
b. Power law with gamma = 0.2
c. Histogram Equalization

Code:-

5/13
image = imread('girl.png');

if size(image, 3) == 3
image = rgb2gray(image);
else
disp('The image is already grayscale.');
end

imageNegative = 255 - image;


gamma = 0.2;
imagePowerLaw = = uint8(double(image).^0.2);
imageHistEq = histeq(image);

figure;
subplot(2, 2, 1);
imshow(image);
title('Original Image');

subplot(2, 2, 2);
imshow(imageNegative);
title('Negative Image');

subplot(2, 2, 3);
imshow(imagePowerLaw);
title('Power Law (Gamma = 0.2)');

subplot(2, 2, 4);
imshow(imageHistEq);
title('Histogram Equalization');

figure;
imhist(imageHistEq);
title('Histogram of Equalized Image');

figure;
imhist(image);
title('Histogram of Original Image');

6/13
Output:-

7/13
8/13
4. Read a gray scale image and apply Gaussian low pass filter and
Laplacian high pass filter on the image in the spatial domain. What is
your observation regarding the output?

Code:-

image = imread('girl.png');

if size(image, 3) == 3
image = rgb2gray(image);
else
disp('The image is already grayscale.');
end

gaussianFilter = fspecial('gaussian', [5 5], 1);


imageGaussian = imfilter(image, gaussianFilter, 'same');

laplacianFilter = fspecial('laplacian', 0.2);


imageLaplacian = imfilter(image, laplacianFilter, 'same');

figure;

subplot(3, 1, 1);
imshow(image);
title('Original Image');

subplot(3, 1, 2);
imshow(imageGaussian);
title('Gaussian Low-Pass Filter');

subplot(3, 1, 3);
imshow(imageLaplacian, []);
title('Laplacian High-Pass Filter');
The image is already grayscale.

Output:-

9/13
My Observation :

Gaussian Low Pass Filter:


 Observation : Blurs the image by reducing high-frequency
components, resulting in a smoother, less detailed image.

Laplacian High Pass Filter:


 Obsearvation: Enhances edges and fine details by highlighting
rapid intensity changes, resulting in an image with more
pronounced edges and contrast.

5. Read a gray scale image and apply Gaussian low pass filter and
Laplacian high pass filter on the image in the frequency domain. What
is your observation regarding the output?

10/13
Code:-

img = imread('girl.png');

if size(img, 3) == 3
img = rgb2gray(img);
end

img = double(img);
[M, N] = size(img);

F = fft2(img);
Fshift = fftshift(F);

D0 = 30;
[x, y] = meshgrid(-N/2:N/2-1, -M/2:M/2-1);
H = exp(-(x.^2 + y.^2) / (2 * D0^2));
H = double(H);

G = H .* Fshift;
G_ishift = ifftshift(G);
img_gaussian_filtered = ifft2(G_ishift);
img_gaussian_filtered = abs(img_gaussian_filtered);

Hlaplacian = fspecial('laplacian', 0.2);


Hlaplacian = double(Hlaplacian);
Hlaplacian_freq = fft2(Hlaplacian, M, N);
Hlaplacian_freq = fftshift(Hlaplacian_freq);

L = Hlaplacian_freq .* Fshift;
L_ishift = ifftshift(L);
img_laplacian_filtered = ifft2(L_ishift);
img_laplacian_filtered = abs(img_laplacian_filtered);

figure;
subplot(3, 1, 1);
imshow(img, []);
title('Original Image');

11/13
subplot(3, 1, 2);
imshow(img_gaussian_filtered, []);
title('Gaussian Low Pass Filtered');

subplot(3, 1, 3);
imshow(img_laplacian_filtered, []);
title('Laplacian High Pass Filtered');

Output:-

My Observation :-

Gaussian Low Pass Filter:

 Observation: The image appearing less detailed, more uniform,


with softened edge and reduced noise.

12/13
Laplacian High Pass Filter:

 Observation: The image appears sharped with more pronounced


edge and textures, highlighting feature and contrast.

13/13

You might also like