University of Engineering
and and Technology, Taxila
Lab Report #07
DIP Lab
Submitted By:
Shaista (22-SE-90)
Submitted To:
Dr. Saba Awan
Section:
Omega (Ω)
Date:
04-03-2024
TASK # 01:
Without using Matlab built-in functions for applying and creating smoothing filters create
an averaging filter and apply on a noisy image to smooth it.
CODE:
% Read the image
img = imread('cameraman.tif');
img = im2double(img); % Convert to double for processing
% Add noise to the image (salt & pepper noise)
noisy_img = imnoise(img, 'salt & pepper', 0.05);
% Define the averaging filter kernel (3x3)
filter_kernel = ones(3, 3) / 9;
% Initialize the output image
smoothed_img = zeros(size(img));
% Apply the averaging filter
for i = 2:size(img, 1)-1
for j = 2:size(img, 2)-1
% Extract the 3x3 neighborhood
neighborhood = noisy_img(i-1:i+1, j-1:j+1);
% Apply the filter
smoothed_img(i, j) = sum(sum(neighborhood .* filter_kernel));
end
end
% Display the results
figure;
subplot(1, 2, 1);
imshow(noisy_img);
title('Noisy Image');
subplot(1, 2, 2);
imshow(smoothed_img);
title('Smoothed Image (Averaging Filter)');
OUTPUT:
TASK # 03:
Read a video file and apply averaging on it by using image addition. Observe the results
and mention in your findings.
CODE:
video = VideoReader('xylophone.mp4'); % Load video file
frame_avg = zeros(video.Height, video.Width);
frame_count = 0;
while hasFrame(video)
frame = im2double(rgb2gray(readFrame(video))); % Convert to grayscale
frame_avg = frame_avg + frame;
frame_count = frame_count + 1;
end
frame_avg = frame_avg / frame_count; % Compute average frame
% Display result
figure, imshow(frame_avg), title('Averaged Video Frame');
OUTPUT:
TASK # 05:
Create a mask and by applying it on image, extract a particular area of image (using
threshold) and implement logical operations (OR, NOT) by referencing example.
CODE:
img = imread('cameraman.tif'); % Ensure image is read
img = im2double(img); % Convert image to double
threshold = 0.5;
mask = img > threshold; % Create binary mask
not_mask = ~mask; % Apply NOT operation
or_mask = mask | not_mask; % Apply OR operation
% Extract a particular area of the image using thresholding
extracted_region = img .* mask;
% Display results
figure;
subplot(2, 3, 1), imshow(img), title('Original Image');
subplot(2, 3, 2), imshow(mask), title('Masked Image (Thresholding)');
subplot(2, 3, 3), imshow(not_mask), title('NOT Operation on Mask');
subplot(2, 3, 4), imshow(or_mask), title('OR Operation on Mask');
subplot(2, 3, 5), imshow(extracted_region), title('Extracted Region Using
Mask');
LAB 06 tasks
Task 01
Implement negation
transform
A=imread('rice.png');
figure,imshow(A);title('Original Image');
%Image Negative
L=256;
s= (L-1)-A;
figure,imshow(s);title('Image negative -> S = L - 1 - r')
TASK 2 Implement
Logarithmic transform.
A=imread('rice.png');
figure,imshow(A);title('Original Image');
%Log Transformation
%Input Image in type double
r=double(A);
C=1;
S=C*log(1+r);
%maximum value of r is 255, log(256) ensures that the transformed imagescales
properly to fit within the displayable range.
Temp=255/(C*log(256)); %This step calculates a normalization factor.
%Display image range [0 255]
B=uint8(Temp*S);
figure,imshow(B);title('Log Transformation -> S = clog(1+r)');
TASK 3 Implement Gray
level slicing technique
img = imread('cameraman.tif'); % Use your image here
% Define intensity range for slicing
low_threshold = 100; % Lower bound of intensity range
high_threshold = 200; % Upper bound of intensity range
% Define enhancement value
enhanced_value = 255; % Maximum intensity for highlighted region% Perform
gray level slicing without background removal
sliced_img = img; % Copy the original image
sliced_img((img >= low_threshold) & (img <= high_threshold)) =
enhanced_value;
% Display results
figure;
subplot(1,2,1);
imshow(img);
title('Original Image');
subplot(1,2,2);
imshow(sliced_img);
title('Gray Level Slicing (Without Background Removal)');
Task 4: Convert an image
into 8-bit plane images by
taking corresponding bit
values at each pixel of the
image. And compare it with
original image.
A=imread('coins.png');
B=bitget(A,1);
figure, subplot(2,2,1);imshow(logical(B));title('Bit plane 1');
B=bitget(A,2);
subplot(2,2,2);imshow(logical(B));title('Bit plane 2');
B=bitget(A,3);
subplot(2,2,3);imshow(logical(B));title('Bit plane 3');
B=bitget(A,4);
subplot(2,2,4);imshow(logical(B));title('Bit plane 4');
B=bitget(A,5);
figure, subplot(2,2,1);imshow(logical(B));title('Bit plane 5');
B=bitget(A,6);
subplot(2,2,2);imshow(logical(B));title('Bit plane 6');
B=bitget(A,7);
subplot(2,2,3);imshow(logical(B));title('Bit plane 7');
B=bitget(A,8);
subplot(2,2,4);imshow(logical(B));title('Bit plane 8');
clear;
close all;
A = imread('rice.png'); % Read the input image
figure, imshow(A); title('Original Image');
A = double(A); % Convert image to double for computations
G = 0.40; % Gamma = 0.40
C = 1; % Define C as 1 (default scaling factor)
% Apply Gamma Correction
S = C * (A .^ G);
% Normalize Image to 0-255
Temp = 255 / (C * (255 ^ G));
S1 = uint8(Temp * S); % Convert back to uint8
figure, imshow(S1);
title('Gamma corrected Image -> S = Cr^\gamma, \gamma = 0.40, C = 1');
% Power Law (Gamma) Transformation with Different Values of Gamma
GRng = [0.04; 0.10; 0.20; 0.40; 0.67; 1; 1.5; 2.5; 5.0; 10.0; 25.0];
R = 0:255; % Intensity range from 0 to 255
figure, hold on;
for i = 1:length(GRng)
X = C * (R .^ GRng(i)); % Apply Power-law transformation
Temp = 255 / max(X); % Normalize output range
s = Temp * X;
plot(R, s); % Plot the transformation function
text(R(175), s(175), ['\gamma =', num2str(GRng(i))],
'HorizontalAlignment', 'left');
end
title('Plot Equation S = Cr^\gamma');
xlabel('Input Intensity Level, r');
ylabel('Output Intensity Level, s');
axis([0 255 0 255]); % Fix axis limits
hold off;