Вы находитесь на странице: 1из 20

a Nang university of technology ----------

Homework 2
Teaching Assistant:
Tran Thi Minh Hanh

Members: Pham Thi Trang Ong Thi Hoang Anh

Problem
Matlab code:

1: High Boost Filtering

X= imread('HW2_1.bmp'); figure, imshow(X); X1=rgb2hsv(X); V=X1(:,:,3); A=1; H=[-1, -1, -1; -1, A+8, -1; -1, -1, -1]; V=conv2(V, H); X1(:,:,3)=V(2:size(V,1)-1,2:size(V,2)-1); Y=hsv2rgb(X1); figure, imshow(Y); title 'image after using high boost filter';

Output figure:
initial image

image after using high boost filter

Problem

2: Salt-Pepper Noise

a/ Convolve with a (smoothing) box filter (all ones) to reduce the noise. Select a
reasonable size (3x3, 5x5 or 7x7) to trade off noise and blurring. Display and submit.

Matlab code:
[X, map] = imread ('HW2_2.gif'); figure, imshow(X, map); title 'the original image'; filter1=ones(3, 3)./9; Y1=conv2(X, filter1); figure, imshow(Y1, map); title 'result of filter 3x3'; filter2=ones(5, 5)./25;

Y2=conv2(X, filter2); figure, imshow(Y2, map); title 'result of filter 5x5'; filter3=ones(7, 7)./49; Y2=conv2(X, filter3); figure, imshow(Y2, map); title 'result of filter 7x7';

Output figure:
the original image

result of filter 3x3

result of filter 5x5

result of filter 7x7

b/ Now apply a 3 x 3 and 5x5 median filter to the image and compare the result with
the use of box filter. Display and submit. Which approach is more satisfying in terms of image quality?

Matlab code:
[X, map] = imread ('HW2_2.gif'); figure, imshow(X, map); title 'the original image'; %median with size 3x3 n = size(X); Y = zeros(n(1)+2, n(2)+2); Y(2:n(1)+1,2:n(2)+1) = X; Y(1,2:n(2)+1) = X(1,:); Y(end,2:n(2)+1) = X(end,:); Y(2:n(1)+1, 1) = X(:, 1);

Y(2:n(1)+1,end) = X(:,end); out = 0*X; for i=2:n(1)+1 for j=2:n(2)+1 block = Y(i-1:i+1, j-1:j+1); out(i-1,j-1) = median(block(:)); end end figure; imshow(out,map); title('result of filter 3x3'); %median with size 5x5 n = size(X); Y = zeros(n(1)+4, n(2)+4); Y(3:n(1)+2,3:n(2)+2) = X; Y(1,3:n(2)+2) = X(1,:); Y(n(1)+4,3:n(2)+2) = Y(2,3:n(2)+2) = X(1,:); Y(n(1)+3,3:n(2)+2) = Y(3:n(1)+2, 1) = X(:, 1); Y(3:n(1)+2,n(2)+4) Y(3:n(1)+2, 2) = X(:, 2); Y(3:n(1)+2,n(2)+3) out2 = 0*X; for i=3:n(1)+2 for j=3:n(2)+2 block = Y(i-2:i+2, j-2:j+2); out2(i-2,j-2) = median(block(:)); end end figure, imshow(out2, map); title('result of filter 5x5');

X(end,:); X(end,:); = X(:,end); = X(:,end);

Output figure:

the original image

result of filter 3x3

result of filter 5x5

Comment:
The images that is result of filter 5x5 is more better than that of filter 3x3 since the picture of filter 5x5 is more clear and remove much more noise than the filter 3x3. It is obvious that median filter is a good tool to avoid blurring in reducing salt-and-pepper noise.

Problem

3:

Frequency Domain Transformation

In Prob. 3) of the LAB2, you were asked to perform the sharpening of a blurred image f(x,y) by convolving with a Laplacian mask h(x,y). Take the DFT of both image and mask to create F(u,v) and H(u,v). Make sure you zero pad both image and mask to create the same size (what should be the size) so that you can multiply these two DFTs and then perform IDFT to create the sharpening operations.

Matlab code:
x=load ('2_1.asc'); filter1=ones(3, 3)./9; y1=conv2(x, filter1); figure, imshow(y1/256); title('original figure'); h=[0, -1, 0; -1, 5, -1; 0, -1, 0]; n=size(y1); m=size(h); if n(1)<m(1) for i=1:(m(1)-n(1)) y1=[y1; zeros(1, n(2))]; end else for i=1:(n(1)-m(1)) h=[h; zeros(1, m(2))]; end end n=size(y1); m=size(h); if n(2)<m(2) for i=1:(m(2)-n(2)) y1=[y1, zeros(n(1), 1)]; end else for i=1:(n(2)-m(2)) h=[h, zeros(m(1), 1)]; end end H=fft2(h); X=fft2(y1); out=X.*H; OUT=ifft2(out); figure, imshow(OUT/256); title 'image after convolution';

Output figure:

original figure

image after convolution

Problem

4: Frequency Domain Transformation

a)

First, try the spatial domain high-boost filter on this image and report your result.

Matlab code:
X= imread('HW2_4.jpg'); figure, imshow(X); title 'initial image'; X1=rgb2hsv(X); V=X1(:,:,3); A=1; H=[-1, -1, -1; -1, A+8, -1; -1, -1, -1]; V=conv2(V, H); X1(:,:,3)=V(2:size(V,1)-1,2:size(V,2)-1); Y=hsv2rgb(X1); figure, imshow(Y); title 'image after using high boost filter';

Output figure:

initial image

image after using high boost filter

b)
Matlab code:
x= imread('HW2_4.jpg'); x1=rgb2hsv(x); Z=x1(:,:,3); X=fft(Z); X=fftshift(X); u=[-256:255]; v=[-256:255]; [U, V] = meshgrid(u,v); q = round(sqrt(U.^2 + V.^2)); for sigma=[5 20 35 50], g = .99/(sigma*sqrt(pi)).*exp(-q.^2./(2*sigma^2))+.01; figure,

plot(g(:,255)); end

Output figure: For


0.12

0.1

0.08

0.06

0.04

0.02

100

200

300

400

500

600

For

0.04

0.035

0.03

0.025

0.02

0.015

0.01

100

200

300

400

500

600

For
0.026 0.024 0.022 0.02 0.018 0.016 0.014 0.012 0.01

100

200

300

400

500

600

For

0.022

0.02

0.018

0.016

0.014

0.012

0.01 0

100

200

300

400

500

600

c)

Matlab code:

X= imread('HW2_4.jpg'); subplot(3, 2, 1); imshow(X); title 'the original figure'; X1=rgb2hsv(X); V=X1(:,:,3); Y=fftshift(fft2(fftshift(V))); u=[-256:255]; v=[-256:255]; [U, V] = meshgrid(u,v); q = round(sqrt(U.^2 + V.^2)); count=2; for sigma=[5 20 35 50], count=count+1; g = .99/(sigma*sqrt(pi)).*exp(-q.^2./(2*sigma^2))+.01; output=Y.*(1./g); V =fftshift(ifft2(fftshift(output))); X1(:,:,3)=V./sigma; YY=hsv2rgb(X1); subplot(3, 2, count); imshow(YY);

title (sprintf('figure of conv with sigma=%d',sigma)); end

Output figure:
the original figure

figure of conv with sigma=5

figure of conv with sigma=20

figure of conv with sigma=35

figure of conv with sigma=50

Вам также может понравиться