0% found this document useful (0 votes)
74 views8 pages

Dip Lab Report 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views8 pages

Dip Lab Report 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Digital Image Processing

LAB REPORT 03

Abdullah Fayyaz Abbasi | FA21-BEE-015

Submitted To:
Dr. Sajjad Ali Haider

Dated: 21th March 2024

Department Of Electrical and Computer Engineering


COMSATS University Islamabad Wah Campus
Task No 1: 1. Consider the following 8x8 image:

3 148 117 148 145 178 132 174


2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183

Threshold it at
(a) level 100 (b) level 150
Code:
% Define the 8x8 image
image = [3 148 117 148 145 178 132 174;
2 176 174 110 185 155 118 165;
0 100 124 113 193 136 146 108;
0 155 170 106 158 130 178 170;
9 196 138 113 108 127 144 139;
6 188 143 183 137 162 105 169;
9 122 156 119 188 179 100 151;
8 176 137 114 135 123 134 183];

% Threshold at level 100


threshold_100 = imbinarize(image, 100);

% Threshold at level 150


threshold_150 = imbinarize(image, 150);

% Display the results


subplot(1, 2, 1);
imshow(threshold_100);
title('Threshold at Level 100');

subplot(1, 2, 2);
imshow(threshold_150);
title('Threshold at Level 150');
Results:

Task No 2: Superimpose the image text.tif onto the image cameraman.tif. You can do
this with:
>> t=imread('text.tif');}
>> c=imread('cameraman.tif');}
>> m=uint8(double(c)+255*double(t));}
Can you threshold this new image m to isolate the text?
Code:
% Read the images
t = imread('trees.tif');
c = imread('cameraman.tif');

% Resize text image to match the dimensions of cameraman image


t_resized = imresize(t, size(c));

% Convert images to double for arithmetic operations


t_double = double(t_resized);
c_double = double(c);

% Superimpose the text image onto the cameraman image


m = uint8(c_double + 255 * t_double);

% Threshold the new image to isolate the text


thresholded_text = imbinarize(m);

% Display the thresholded image


imshow(thresholded_text);
title('Thresholded Text');
Results:

Task No 3: The following small image has grey values in the range 0 to 19.
Compute the grey level histogram and the
mapping that will equalize this histogram. Produce an 8x8 grid containing the grey
values for the new
histogram-equalized image.

12 6 5 13 14 14 16 15
11 10 8 5 8 11 14 14
9 8 3 4 7 12 18 19
10 7 4 2 10 12 13 17
16 9 13 13 16 19 19 17
12 10 14 15 18 18 16 14
11 8 10 12 14 13 14 15
8 6 3 7 9 11 12 12.
Code: % Given image
image = [12 6 5 13 14 14 16 15;
11 10 8 5 8 11 14 14;
9 8 3 4 7 12 18 19;
10 7 4 2 10 12 13 17;
16 9 13 13 16 19 19 17;
12 10 14 15 18 18 16 14;
11 8 10 12 14 13 14 15;
8 6 3 7 9 11 12 12];

% Compute the histogram


histogram = histcounts(image(:), 'BinLimits', [0, 19], 'BinMethod', 'integers');

% Compute the cumulative distribution function (CDF)


cdf = cumsum(histogram);

% Normalize the CDF to obtain the equalization mapping


cdf_normalized = cdf / sum(histogram);
equalization_mapping = round(19 * cdf_normalized);

% Apply the mapping to each pixel in the original image


equalized_image = zeros(size(image));
for i = 1:numel(image)
% Ensure the index doesn't exceed the size of equalization_mapping
idx = min(image(i) + 1, numel(equalization_mapping));
equalized_image(i) = equalization_mapping(idx);
end

% Display the equalized image and the corresponding 8x8 grid


disp('Equalized Image:');
disp(equalized_image);

disp('8x8 Grid:');
disp(reshape(equalized_image, 8, 8));

% Display the histogram of the equalized image


figure;
bar(0:19, histogram);
title('Grey Level Histogram');

% Display the histogram equalization mapping


figure;
plot(0:19, equalization_mapping);
title('Histogram Equalization Mapping');
xlabel('Original Grey Level');
ylabel('Equalized Grey Level');

Results:
Task No 3: Is the histogram equalization operation idempotent? That is, is
performing histogram equalization twice the same as doing it just once?

Code:
% Read an example image
original_image = imread('cameraman.tif');

% Apply histogram equalization twice


equalized_image_1 = histeq(original_image);
equalized_image_2 = histeq(equalized_image_1);

% Display the original and equalized images


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

subplot(1, 3, 2);
imshow(equalized_image_1);
title('Equalized Image (Once)');

subplot(1, 3, 3);
imshow(equalized_image_2);
title('Equalized Image (Twice)');
Result:

Task No 3: Create a dark image with


>> c=imread('cameraman.tif');
>> [x,map]=gray2ind(c);
The matrix x, when viewed, will appear as a very dark version of the cameraman
image. Apply histogram equalization to it, and compare the result with the original
image.

Code:
% Read the "cameraman.tif" image
c = imread('cameraman.tif');

% Convert it to an indexed image


[x, map] = gray2ind(c);

% Apply histogram equalization to the indexed image


equalized_x = histeq(x, map);

% Convert the equalized indexed image back to grayscale


equalized_image = ind2gray(equalized_x, map);

% Display the original and equalized images for comparison


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

subplot(1, 2, 2);
imshow(equalized_image);
title('Equalized Image');
Result:

You might also like