HW2sol

7
ECE 440: HW2 solution Image Enhancement Using Filtering Problem 1: MATLAB CODE % Problem #1 - High Boost Filtering % ----------------------------------- clc;clear all; x=imread('hw2_1.bmp','bmp'); figure; subplot(1,2,1); imshow(x); xlabel('Original Image'); % convert image to HSV [h,s,v]=rgb2hsv(x); % Generate a high pass kernel with a=1 a=1; hpf = [-1 -1 -1;-1 a+8 -1 ;-1 -1 -1]; % Convolve the kernel with the image z = conv2(v,hpf,'same'); sizez = size(z); %linear stretch or just % If any of element in z is less than 0, the element becomes 0. % If any of element in z is greater than 1, the element becomes 1. less0 = find(z < 0); z(less0) = 0; more1 = find(z > 1); z(more1) = 1; y1 = hsv2rgb(h,s,z); % convert back to RGB subplot(1,2,2); imshow(y1); title('Sharpened Image');

Transcript of HW2sol

Page 1: HW2sol

ECE 440: HW2 solution

Image Enhancement Using Filtering

Problem 1:

MATLAB CODE

% Problem #1 - High Boost Filtering % ----------------------------------- clc;clear all; x=imread('hw2_1.bmp','bmp'); figure; subplot(1,2,1); imshow(x); xlabel('Original Image'); % convert image to HSV [h,s,v]=rgb2hsv(x); % Generate a high pass kernel with a=1 a=1; hpf = [-1 -1 -1;-1 a+8 -1 ;-1 -1 -1]; % Convolve the kernel with the image z = conv2(v,hpf,'same'); sizez = size(z); %linear stretch or just % If any of element in z is less than 0, the element becomes 0. % If any of element in z is greater than 1, the element becomes 1. less0 = find(z < 0); z(less0) = 0; more1 = find(z > 1); z(more1) = 1; y1 = hsv2rgb(h,s,z); % convert back to RGB subplot(1,2,2); imshow(y1); title('Sharpened Image');

Page 2: HW2sol

Problem 2:

a. Reduce the noise with box filter 3x3, 5x5, 7x7

b. Use 3x3 and 5x5 mean filter.

%Problem 2 - Salt and Pepper Noise %----------------------------------------------------------------- %a clc; clear all; close all; [X,map] = imread('HW2_2.gif','gif'); X = double(X); figure subplot(2,3,2),imshow (X,map); xlabel('original image');

Page 3: HW2sol

%convolve with a box filter 3x3 X1 = conv2(X,1/9*ones(3,3)); subplot(2,3,4),imshow(X1,map); xlabel('image with box filter 3x3'); %convolve with a box filter 5x5 X2 = conv2(X,1/25*ones(5,5)); subplot(2,3,5),imshow(X2,map); xlabel('image with box filter 5x5'); %convolve with a box filter 7x7 X3 = conv2(X,1/49*ones(7,7)); subplot(2,3,6),imshow(X3,map); xlabel('image with box filter 7x7'); %----------------------------------------------------- %b %with 3x3 median filtering process [m, n] = size(X) Y = zeros(m ,n); for r = 2:m-1, for c = 2:n-1, K = X((r-1):(r+1), (c-1:c+1)); newv = median(K(:)); Y(r, c) = newv; end end figure; subplot(1,3,1); imshow(X,map); title('Original Noisy Image'); subplot(1,3,2); imshow(Y,map); title('3x3 Median Filtered Image'); %with 5x5 median filtering process [m, n] = size(X); Y1 = zeros(m ,n); for r = 3:m-2, for c = 3:n-2, K = X((r-2):(r+2), (c-2:c+2)); newv = median(K(:)); Y1(r, c) = newv; end end subplot(1,3,3); imshow(Y1,map); title('5x5 Median Filtered Image'); Problem 3:

Page 4: HW2sol

MATLAB CODE %Problem3: - Frequency Domain Transformation % clc, clear all, close all; I = load('2_1.asc'); %creat blurred image f = conv2(I, 1/9*ones(3,3)); figure subplot(1,3,1); imshow(f); xlabel('Original Blurred Image'); % Laplacian mask h = [0 -1 0; -1 5 -1; 0 -1 0]; % zero padding. [m,n]=size(f); [mh,nh]=size(h); newf=[f;zeros(mh-1,n)]; newf=[newf zeros(m+mh-1,nh-1)]; newh=[h;zeros(m-1,nh)]; newh=[newh zeros(m+mh-1,n-1)]; % DFT of image and mask H=fft2(newh); F=fft2(newf); % Multiply these two DFTs Y=H.*F; % IDFT of the result y=ifft2(Y); subplot(1,3,2); imshow(y); xlabel('Sharpening Image by using frequency domain transformation'); subplot(1,3,3) t=conv2(f,h); imshow(t); xlabel('Sharpening Image by directly 2-D convolution'); Problem4: a.

Page 5: HW2sol

b. Plot g(q) over this range for several values of σ between 5 and 50

Comment: these functions is greatest near zero frequency, and decreases to 0.01 near the ends of the array

c.

Page 6: HW2sol

MATLAB CODE %Problem 4: - Frequency Domain Transformation %a %----------------------------------------------------- clc;clear;close all; x=imread('HW2_4.jpg'); figure; subplot(1,2,1);imshow(x);xlabel('The original image'); %Sharpen using high boost filter % convert image to HSV [h,s,v]=rgb2hsv(x); % Generate a high pass kernel with a=1 a=1; hpf = [-1 -1 -1;-1 a+8 -1 ;-1 -1 -1]; % Convolve the kernel with the image z = conv2(v,hpf,'same'); sizez = size(z); %linear stretch or just % If any of element in z is less than 0, the element becomes 0. % If any of element in z is greater than 1, the element becomes 1. less0 = find(z < 0); z(less0) = 0; more1 = find(z > 1); z(more1) = 1;

Page 7: HW2sol

y = hsv2rgb(h,s,z); % convert back to RGB subplot(1,2,2),imshow(y);xlabel('High boost filtered image'); %---------------------------------------------- %b u=[-256:255]; v=[-256:255]; [U, V] = meshgrid(u,v); q = round(sqrt(U.^2 + V.^2)); count=0; sigma=[5 20 35 50], figure; for i = 1:length(sigma); g = 0.01+(0.99*exp(-q.^2./(2*(sigma(i).^2))))./(sigma(i)*sqrt(pi)); subplot(2,2,i) plot(q,g); xlabel(sprintf('g(q) with sigma =%6.1f',sigma(i))); end %-------------------------------------------------- %c. X = rgb2hsv(x); Xshifted = fftshift(fft2(fftshift(X(:,:,3)))); figure; for i = 1:length(sigma); g = 0.01+(0.99*exp(-q.^2./(2*(sigma(i).^2))))./(sigma(i)*sqrt(pi)); Yshifted = Xshifted ./g; Y(:,:,3) = ifft2(fftshift(Yshifted)); Y(:,:,3) = fftshift(Y(:,:,3)); Y(:,:,3) = Y(:,:,3) / max(max((Y(:,:,3)))); Y = hsv2rgb(Y); subplot(2,2,i);imshow(Y);xlabel(sprintf('Sharpen image with sigma =%6.2f',sigma(i))); Y = X; end