Computer Vision hw3
Computer Vision hw3
Homework 3:
Non-linear Operations
Pesented by:
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
subplot(1,2,2);
imshow(I_modified);
title('Image After Equalizing Central Pixel');
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
% Extract channels
X = img_xyz(:,:,1);
Y = img_xyz(:,:,2);
Z = img_xyz(:,:,3);
% Add Noise
% Add Gaussian noise
img_gaussian = imnoise(img, 'gaussian', 0, 0.01);
% 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]);
% 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);
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');
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: