Computer Vision
Homework 3:
Non-linear Operations
Pesented by:
Zahraa muhaned mohammed faeq
MSc stg 1
Homework 3 – Non-linear Operations:
1. Code parts 1 – 3 of the histogram equalization algorithm given in section 2.2.2 of the lecture
notes as a function that receives a luminance array to calculate the scaled cumulative histogram
based on the entire array, then apply it only to the central pixel of the array. The function should
return the equalized central pixel only.
I = imread('C:\Users\ahmed\Pictures\computer vision\Lenna.png');
I_gray = rgb2gray(I); % Convert to grayscale if the image is colored
% Compute the histogram
[counts, bins] = imhist(I_gray);
% Compute the cumulative distribution function (CDF)
cdf = cumsum(counts);
cdf_normalized = (cdf - min(cdf)) / (max(cdf) - min(cdf)) * 255;
% Find the coordinates of the central pixel
[m, n] = size(I_gray);
center_x = round(m / 2);
center_y = round(n / 2);
% Get the original value of the central pixel
central_pixel_value = I_gray(center_x, center_y);
% Apply histogram equalization to the central pixel only
equalized_pixel = round(cdf_normalized(central_pixel_value + 1));
% Create a new image with the central pixel modified
I_modified = I_gray;
I_modified(center_x, center_y) = equalized_pixel;
% Display the original and modified images
figure;
subplot(1,2,1);
imshow(I_gray);
title('Original Image');
subplot(1,2,2);
imshow(I_modified);
title('Image After Equalizing Central Pixel');
% Print the values before and after equalization
fprintf('Central pixel value before equalization: %d\n', central_pixel_value);
fprintf('Central pixel value after equalization: %d\n', equalized_pixel);
Central pixel value before equalization: 104
Central pixel value after equalization: 89
2. Write a program that transforms an image to the XYZ colorspace, then passes only the
luminance channel Y to MATLAB’s function nlfilter as a means to apply a sliding window
operation. The operation applied by nlfilter (through an anonymous function; see section 2.4 of
the lecture notes for an example) isthe function coded in step 1. Next, the program should
perform part 4 of the algorithm given in section 2.2.2 of the lecture notes to apply the same
pixel-wise scaling (calculated based on how nlfilter scaled Y) to X and Z chroma channels. Test
the locally adaptive histogram equalization operation implemented insteps 1 – 2 on few color
images.
% Read the image
img_rgb = imread('C:\Users\ahmed\Pictures\ph\8.jpg');
img_rgb = im2double(img_rgb); % Convert to double precision
% Convert from RGB to XYZ color space
img_xyz = rgb2xyz(img_rgb);
% Extract channels
X = img_xyz(:,:,1);
Y = img_xyz(:,:,2);
Z = img_xyz(:,:,3);
% Define the window size for the filter
window_size = [7 7]; % Larger window for stronger effect
% Apply a non-linear filter (alpha-trimmed mean filter)
alpha = 0.2; % 20% trimming
filtered_Y = nlfilter(Y, window_size, @(block) trimmean(block(:), alpha * 100));
% Compute scaling factor based on how Y was changed
scale_factor = (filtered_Y ./ (Y + eps)).^1.5; % Exaggerate the effect
% Apply the same scaling to X and Z channels
X_scaled = X .* scale_factor;
Z_scaled = Z .* scale_factor;
% Combine the scaled XYZ channels
img_xyz_filtered = cat(3, X_scaled, filtered_Y, Z_scaled);
% Convert back to RGB
img_rgb_filtered = xyz2rgb(img_xyz_filtered);
% Ensure values are in valid range
img_rgb_filtered = max(0, min(1, img_rgb_filtered));
% Display original and filtered images
figure;
subplot(1,3,1), imshow(img_rgb), title('Original Image');
subplot(1,3,2), imshow(img_rgb_filtered), title('Filtered Image');
subplot(1,3,3), imshow(abs(img_rgb - img_rgb_filtered) * 5), title('Difference');
3. Use MATLAB’s function imnoise to add the following types of noise to ‘Lenna.png’ image:
Gaussian noise, Poisson noise (also known as shot noise), and salt & pepper noise. Use the noisy
images to evaluate the effectiveness of the following filters: 3 × 3 median filter, 3 × 3 25%-
trimmed mean filter, bilateral filter, and non-local means filter (estimate the parameters of the
last two filters from each noisy image; see section 2.4.1 for a guide on how to look for flat
regions to estimate σ).
img = imread('C:\Users\ahmed\Pictures\computer vision\Lenna.png');
img = im2double(rgb2gray(img)); % Convert to double and grayscale
% Add Noise
% Add Gaussian noise
img_gaussian = imnoise(img, 'gaussian', 0, 0.01);
% Add Poisson noise
img_poisson = imnoise(img, 'poisson');
% Add Salt & Pepper noise
img_salt_pepper = imnoise(img, 'salt & pepper', 0.05);
% Apply Filters
% 3x3 Median Filter
img_gaussian_median = medfilt2(img_gaussian, [3 3]);
img_poisson_median = medfilt2(img_poisson, [3 3]);
img_salt_pepper_median = medfilt2(img_salt_pepper, [3 3]);
% 3x3 25%-Trimmed Mean Filter
img_gaussian_trimmed = trimmed_mean_filter(img_gaussian, 3, 25);
img_poisson_trimmed = trimmed_mean_filter(img_poisson, 3, 25);
img_salt_pepper_trimmed = trimmed_mean_filter(img_salt_pepper, 3, 25);
% Bilateral Filter
sigma_d = 2;
sigma_r = 0.1;
img_gaussian_bilateral = imbilatfilt(img_gaussian, sigma_r, sigma_d);
img_poisson_bilateral = imbilatfilt(img_poisson, sigma_r, sigma_d);
img_salt_pepper_bilateral = imbilatfilt(img_salt_pepper, sigma_r, sigma_d);
% Non-local Means Filter
img_gaussian_nlm = imnlmfilt(img_gaussian);
img_poisson_nlm = imnlmfilt(img_poisson);
img_salt_pepper_nlm = imnlmfilt(img_salt_pepper);
% Evaluate Effectiveness
psnr_gaussian_median = psnr(img_gaussian_median, img);
psnr_poisson_median = psnr(img_poisson_median, img);
psnr_salt_pepper_median = psnr(img_salt_pepper_median, img);
psnr_gaussian_trimmed = psnr(img_gaussian_trimmed, img);
psnr_poisson_trimmed = psnr(img_poisson_trimmed, img);
psnr_salt_pepper_trimmed = psnr(img_salt_pepper_trimmed, img);
psnr_gaussian_bilateral = psnr(img_gaussian_bilateral, img);
psnr_poisson_bilateral = psnr(img_poisson_bilateral, img);
psnr_salt_pepper_bilateral = psnr(img_salt_pepper_bilateral, img);
psnr_gaussian_nlm = psnr(img_gaussian_nlm, img);
psnr_poisson_nlm = psnr(img_poisson_nlm, img);
psnr_salt_pepper_nlm = psnr(img_salt_pepper_nlm, img);
% Display PSNR values
disp('PSNR values:');
disp(['Gaussian Median: ', num2str(psnr_gaussian_median)]);
disp(['Poisson Median: ', num2str(psnr_poisson_median)]);
disp(['Salt & Pepper Median: ', num2str(psnr_salt_pepper_median)]);
disp(['Gaussian Trimmed Mean: ', num2str(psnr_gaussian_trimmed)]);
disp(['Poisson Trimmed Mean: ', num2str(psnr_poisson_trimmed)]);
disp(['Salt & Pepper Trimmed Mean: ', num2str(psnr_salt_pepper_trimmed)]);
disp(['Gaussian Bilateral: ', num2str(psnr_gaussian_bilateral)]);
disp(['Poisson Bilateral: ', num2str(psnr_poisson_bilateral)]);
disp(['Salt & Pepper Bilateral: ', num2str(psnr_salt_pepper_bilateral)]);
disp(['Gaussian NLM: ', num2str(psnr_gaussian_nlm)]);
disp(['Poisson NLM: ', num2str(psnr_poisson_nlm)]);
disp(['Salt & Pepper NLM: ', num2str(psnr_salt_pepper_nlm)]);
% Step 5: Visualize Results
figure;
subplot(2, 2, 1), imshow(img), title('Original');
subplot(2, 2, 2), imshow(img_gaussian), title('Gaussian Noise');
subplot(2, 2, 3), imshow(img_poisson), title('Poisson Noise');
subplot(2, 2, 4), imshow(img_salt_pepper), title('Salt & Pepper Noise');
figure;
subplot(2, 2, 1), imshow(img_gaussian_median), title('Gaussian Median');
subplot(2, 2, 2), imshow(img_poisson_median), title('Poisson Median');
subplot(2, 2, 3), imshow(img_salt_pepper_median), title('Salt & Pepper Median');
subplot(2, 2, 4), imshow(img_gaussian_trimmed), title('Gaussian Trimmed Mean');
figure;
subplot(4, 2, 1), imshow(img_poisson_trimmed), title('Poisson Trimmed Mean');
subplot(4, 2, 2), imshow(img_salt_pepper_trimmed), title('Salt & Pepper Trimmed
Mean');
subplot(4, 2, 3), imshow(img_gaussian_bilateral), title('Gaussian Bilateral');
subplot(4, 2, 4), imshow(img_poisson_bilateral), title('Poisson Bilateral');
subplot(4, 2, 5), imshow(img_salt_pepper_bilateral), title('Salt & Pepper
Bilateral');
subplot(4, 2, 6), imshow(img_gaussian_nlm), title('Gaussian NLM');
subplot(4, 2, 7), imshow(img_poisson_nlm), title('Poisson NLM');
subplot(4, 2, 8), imshow(img_salt_pepper_nlm), title('Salt & Pepper NLM');
% Trimmed Mean Filter Function
function filtered_img = trimmed_mean_filter(img, window_size, trim_percent)
[rows, cols] = size(img);
filtered_img = zeros(rows, cols);
pad = floor(window_size / 2);
img_padded = padarray(img, [pad pad], 'symmetric');
for i = 1:rows
for j = 1:cols
window = img_padded(i:i+window_size-1, j:j+window_size-1);
window_sorted = sort(window(:));
trim = floor(numel(window_sorted) * trim_percent / 100);
window_trimmed = window_sorted(trim+1:end-trim);
filtered_img(i, j) = mean(window_trimmed);
end
end
end
PSNR values:
Gaussian Median: 26.7024
Poisson Median: 35.4519
Salt & Pepper Median: 34.5496
Gaussian Trimmed Mean: 27.7464
Poisson Trimmed Mean: 35.1597
Salt & Pepper Trimmed Mean: 34.0451
Gaussian Bilateral: 29.2312
Poisson Bilateral: 30.5594
Salt & Pepper Bilateral: 24.7581
Gaussian NLM: 30.037
Poisson NLM: 43.9392
Salt & Pepper NLM: 22.1964