22ADS508: Lab.
Digital Image Processing
Name :- Pratik Thul Roll no. :- 46
Section:- A Subject :- DIP- Lab
Aim :- write and execute the MATLAB code to learn the image filtering of smoothing and
sharpening in special domain.
Code :-
% Read the image
A = imread("nature_1.jpg");
% Convert to grayscale
grayA = rgb2gray(A);
% Display original grayscale image
figure;
imshow(grayA);
title('Original Grayscale Image');
% Quantize grayscale image
n = 20; % Number of quantization levels
grayA_quantized = uint8(floor(double(grayA) / (256 / n)) * (256 / n));
% Display quantized grayscale image
figure;
subplot(1,2,1); imshow(grayA); title('Original Grayscale Image');
subplot(1,2,2); imshow(grayA_quantized); title('Quantized Grayscale Image');
% Extract color channels
AR = A(:,:,1); % Red channel
AG = A(:,:,2); % Green channel
AB = A(:,:,3); % Blue channel
% Quantize each color channel
AR_quantized = uint8(floor(double(AR) / (256 / n)) * (256 / n));
AG_quantized = uint8(floor(double(AG) / (256 / n)) * (256 / n));
AB_quantized = uint8(floor(double(AB) / (256 / n)) * (256 / n));
% Reconstruct quantized RGB image
A_quantized = cat(3, AR_quantized, AG_quantized, AB_quantized);
% Display RGB images
figure;
subplot(1,2,1); imshow(A); title('Original RGB Image');
subplot(1,2,2); imshow(A_quantized); title('Quantized RGB Image');
% RGB channel separation
figure;
subplot(1,3,1); imshow(AR); title('Red Channel');
subplot(1,3,2); imshow(AG); title('Green Channel');
subplot(1,3,3); imshow(AB); title('Blue Channel');
% Negative transformation
S1 = 255 - AR; % Negative of Red channel
S2 = 255 - AG; % Negative of Green channel
S3 = 255 - AB; % Negative of Blue channel
S4 = 255 - rgb2gray(A); % Negative of Grayscale image
% Display negative images
figure;
subplot(2,2,1); imshow(S1); title('Negative Red Channel');
subplot(2,2,2); imshow(S2); title('Negative Green Channel');
subplot(2,2,3); imshow(S3); title('Negative Blue Channel');
subplot(2,2,4); imshow(S4); title('Negative Grayscale Image');
% Logarithmic transformation
c = 1; % Scaling constant for log transformation
% Perform log transformation on grayscale image
grayA_log = c * log(1 + double(grayA));
grayA_log = mat2gray(grayA_log) * 255; % Normalize to [0, 255]
grayA_log = uint8(grayA_log);
% Display log-transformed grayscale image
figure;
imshow(grayA_log);
title('Log-Transformed Grayscale Image');
% Perform log transformation on each color channel
AR_log = c * log(1 + double(AR));
AG_log = c * log(1 + double(AG));
AB_log = c * log(1 + double(AB));
% Normalize the results to the range [0, 255]
AR_log = mat2gray(AR_log) * 255;
AG_log = mat2gray(AG_log) * 255;
AB_log = mat2gray(AB_log) * 255;
AR_log = uint8(AR_log);
AG_log = uint8(AG_log);
AB_log = uint8(AB_log);
% Reconstruct log-transformed RGB image
A_log = cat(3, AR_log, AG_log, AB_log);
% Display log-transformed RGB image
figure;
subplot(1,2,1); imshow(A); title('Original RGB Image');
subplot(1,2,2); imshow(A_log); title('Log-Transformed RGB Image');
Screenshot :-