Solved Assignment 2 for Image Processing | CMSC 426, Assignments of Computer Science

Material Type: Assignment; Professor: Jacobs; Class: Image Processing; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-xh9
koofers-user-xh9 🇺🇸

9 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC426 Homework #2 Haibin Ling ( [email protected] )
1. 10 points Prove that convolving a 1-D signal twice with a Gaussian kernel is equivalent to
convolving the signal with a single Gaussian kernel with a larger standard deviation. (Hint:
Make use of the identity:
A
Z
ZxAx
e
A
dxe
22
1
2
2
2
with A>0.)
Proof:
We know that convolving twice with two filters is convenient as convolving the two
filters first to get a new filter, and then convolve the signal with the new filter. So we only
need to prove that the convolution of a Gaussian kernel with another Gaussian kernel is also
a Gaussian kernel.
Suppose the two 1D Gaussian kernels
)(
1
xg
,
)(
2
xg
are with standard deviations
1
,
2
, then their convolution is
duuxgug )()(
21
du
uxu )
2
)(
exp{
2
1
)
2
exp{
2
1
2
2
2
2
2
1
2
1
du
uxu )
2
)(
2
exp{
2
1
2
2
2
2
1
2
21

duu
x
u
x
du
xxuu
du
xuxu
du
uxu
})(
2
1
exp{}
2
exp{
2
1
}
2
exp{}
2
2)(
exp{
2
1
}
2
2)(
exp{
2
1
}
2
)(
exp{
2
1
2
2
2
2
2
2
1
2
2
2
1
2
2
2
21
2
2
2
1
2
2
1
2
2
2
1
2
1
2
2
2
2
1
21
2
2
2
1
2
1
2
2
1
2
2
2
2
1
21
2
2
2
1
2
2
1
2
2
2
21




Let
0/)(
2
2
2
1
2
2
2
1
A
and
Then
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Solved Assignment 2 for Image Processing | CMSC 426 and more Assignments Computer Science in PDF only on Docsity!

1. 10 points Prove that convolving a 1-D signal twice with a Gaussian kernel is equivalent to

convolving the signal with a single Gaussian kernel with a larger standard deviation. (Hint:

Make use of the identity:

A Ax Zx^ Z e A e^2 dx^2

1 2 2 ^2

^ 

    

with A>0.)

Proof:

We know that convolving twice with two filters is convenient as convolving the two

filters first to get a new filter, and then convolve the signal with the new filter. So we only

need to prove that the convolution of a Gaussian kernel with another Gaussian kernel is also

a Gaussian kernel.

Suppose the two 1D Gaussian kernels g 1^ ( x ), g^ 2 ( x )are with standard deviations ^1 ,

 2 , then their convolution is

    g (^) 1 ( u ) g 2 ( xu ) du

        du u x u ) 2 ( ) exp{ 2 1 ) 2 exp{ 2 1 2 2 2 2 2 1 2   1   ^ 

        du u x u ) 2 ( ) 2 exp{ 2 1 2 2 2 2 1 2  1  2  

           

u du

x

u

x

du

u xu x

du

u x xu

du

u x u

} exp{

exp{

} exp{

exp{

exp{

exp{

2 2 2 2 2 2 1 2 2 2 1 2 2 2 1 2 2 2 2 1 (^22) 1 2 2 2 1 2 1 (^22) 2 2 1 1 2 2 2 2 1 2 1 (^22) 1 (^22) 2 2 1 1 2 2 2 2 1 (^22) 1 (^22) 2 1 2

Let

( )/ 0 2 2 2 1 2 2 2 A   1      and 2 Zx /  2

Then

    g (^) 1 ( u ) g 2 ( xu ) du

exp{

exp{

exp{

exp{

exp{

exp{

2 2 2 1 2 2 2 2 1 2 2 2 2 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 (^22) 2 2 2 2 1 2 2 2 1 2 2 2 1 2 2 2 2 2 1 2

  ^ ^ 

  ^ ^ ^ ^ ^ 

^ 

x

x x

x x

A

Z

A

x

So the result is also a Gaussian function with standard deviation  12   22 , which is

larger than both of the two deviations of the two Gaussian filters.

Furthermore when g 1^ and g^ 2 are same Gaussian kernels, that is,^1 ^2 ^ , we have

} 2 ( 2 ) exp{ 2 2 1 ( ) ( ) 2 2 1 2    xg^ ug xu^ du     

The standard deviation now is 2 .

b) 10 points Write a function to convolve a signal with a kernel in 1D. Test it by convolving a

Gaussian with itself 2 and 3 times. Plot both. Turn in the plots.

Solution:

Matlab codes: Convolve1D.m, hwk2b.m, refer to appendix please.

Output result is following image:

c) 20 points Write a 1D edge detector. This should take 2 parameters, the amount of

smoothing and a threshold on the strength of the edge. Test it by finding the edges in the

following 1D image.

I = [zeros(1,50), .9ones(1,10), zeros(1,10), .6ones(1,40), zeros(1,50)].

Solution:

Matlab codes: EdgeDetect1D.m, hwk2c.m, refer to appendix please.

Output result is following image:

e) 20 points Form and turn in the same plot, this time adding Gaussian noise to the image with a

standard deviation of 1 (see function randn ).

I = [zeros(1,50), 9ones(1,10), zeros(1,10), 3ones(1,40), zeros(1,50)].

Solution:

Matlab codes: hwk2d.m, refer to appendix please.

Output result is following image.

3) 10 points Download two images from the internet, or take two images yourself if you have a

digital camera. The first image should be one you think will be easy to find edges in, the

second image should be one that seems hard. Choose a black and white image, or convert it

with rgb2gray. For each image: Run the canny edge detectors, with the default parameters:

edge(I,'canny',[.02,.1],1). Then play with the parameters until you get the best results you

can. On this final, best, version, circle and label two places where you think the edge

detector made a mistake, if it did. Explain why you think it got the wrong answer.

For each image, turn in the image, the edges found using default parameters, and edges

with the best hand-chosen parameters with points labeled. List the parameters you used.

Also turn in your explanation for why any failures occurred.

Solution:

Matlab codes: hwk3.m, refer to appendix please.

Matlab output result is following image.

4) 20 points Challenge problem (this is optional for extra credit): Consider a 1D box kernel of

width 2m, and height 1. Call this kernel, k. k must lie entirely in the interval 0 to 2pi, so assume

m<pi Write k as a linear combination of the basis elements:

,  sin 2 sin 2 , cos 2 cos 2 , sin sin , cos cos 

Hint: you are free to place k anywhere in the interval 0 to 2pi. Put it somewhere that will

make your life easier.

Solution:

First, it’s easy to see following three properties,

       2  0 2 0 cos(( ) ) cos(( ) ) 2

cos( nx )cos( mx ) dx n m x n m xdx ^   ^                          2 0 2 0 2 0 2 0 21 cos(( )^ ) 21 cos(( ) )^000 21 cos(( )^ ) 21 cos(^0 * )^0122 n mx dx n m x dx n mx dx x dx n m n m

       2  0 2 0 cos(( ) ) cos(( ) ) 2

sin( nx )sin( mx ) dx n m x n mxdx ^   ^                    (^12) cos(( ) ) 21 cos(( ) ) 0 0 0 (^12) cos( 0 * ) 21 cos(( ) ) (^1220) 2 0 2 0 2 0 2 0       n m x dx n m x dx xdx n m x dx n m n m          2  0 2 0 sin(( ) ) sin(( ) ) 2

cos( nx )sin( mx ) dx n m x n mxdx ( 0 0 ) 0 2

( sin(( ) ) sin(( ) ) ) 2

2 0 2 0  (^)           n m xdx n mx dx

Then, based on this we have

  (^)   2 0 || cos n || cos^2 n

  (^)   2 0 || sin n || sin^2 n

Next let’s consider about the box kernel

^  ^  0 ( x )^1 otherwise  mx   m

Write the kernel as

     

1 1 ||sin || sin ||cos || cos ( ) n n n n nx x b nx nxx a

So what we need compute a^ n ,^ bn , for n ^1 ,^2 ,...

While we have

       

 

2 0 1 1 2 0 )cos } ||sin || sin ||cos || cos { ( )cos } {( kxdx nx x b nx nx x kxdx a n n n n  (^)   (^)     

1 2 (^10) 2 0 {sin cos } ||sin || {cos cos } || cos || n n n n (^) nx kxdx nx b nx kx dx nx a        

1 1

||sin ||

|| cos || n

n n n

nx

b

n k

nx

a

k k (^) a kx a

||cos ||

Where

^    0 ( n , k )^1 otherwise nk

So we get

  

2 0 { ( )cos }

ak x kxdx       

m m m m m m kxd kx k kxdx kxdx kxdx 0 0 {cos } ( )

cos

cos

cos

      km k kx k m x sin

sin |

0  

Similarly we get

  

2 0 { ( )sin }

bk x kxdx 0 0

sin

sin

 (^)        

  m m m m kxdx kxdx

So

        

1 1 1 ||cos || cos sin( )

||sin || sin ||cos || cos ( ) n n n n n (^) nx nx nm nx n x b nx nx x a

Y2 = Y2(dMargin+1:dMargin+length(X)); Y3 = Convolve1D(Y2,Y); Y3 = Y3(dMargin+1:dMargin+length(X)); plot(X,Y,'k--', X,Y1,'r:', X,Y2,'g-.', X,Y3,'b-'); legend('original Gaussian', 'convolve once', 'convolve twice', 'convolve three times'); Title('convolve Gauassian with itselft, sigma=2');

File EdgeDetect1D.m

% EdgeDetect1D.m % 1D edge detector. Take 2 parameters, the amount of smoothing % and a threshold on the strength of the edge % % returns a binary image BW of the same size as I, with 1's where % the function finds edges in I and 0's elsewhere. function imRes = EdgeDetect1D(im1D, sigma, threshold) X = ceil(-3sigma:3sigma); fGau = Gaussian(X, sigma); fGra = [1, 0, -1]; dMargin = 1; fDoG = Convolve1D(fGau,fGra); fDoG = fDoG(dMargin+1:dMargin+length(X)); dMargin = floor(length(fDoG)/2); imRes = Convolve1D(im1D, fDoG); imRes = imRes(dMargin+1:dMargin+length(im1D)); resLen = length(imRes); imRes = abs(imRes); peaks = [0, (imRes(2:resLen-1)> imRes(1:resLen-2) & imRes(2:resLen- 1)>=imRes(3:resLen)) | ... (imRes(2:resLen-1)>=imRes(1:resLen-2) & imRes(2:resLen- 1)> imRes(3:resLen)), 0]; imRes = peaks.*imRes; imRes = imRes > threshold;

File hwk2c.m

% hwk2c.m. Testing the result of running EdgeDetect1D. clear all; I=[zeros(1,50),.9ones(1,10),zeros(1,10),.6ones(1,40),zeros(1,50)]; sigma = 1; threshold = .3; imEdge = EdgeDetect1D(I, sigma, threshold); figure(1); clf, hold on;

plot(1:length(I), I,'r:', 1:length(I),imEdge,'b-'); legend('1D signal', '1 = edge', 1); axis([0,160,-.1,1.1]); Title('Finding edge 1D: threshold=0.3');

File hwk2d.m

% hwk2d.m clear all; I=[zeros(1,50),.9ones(1,10),zeros(1,10),.6ones(1,40),zeros(1,50)]; figure(1); clf, hold on; subplot(2,1,1); plot(1:length(I), I,'r'); axis([0,160,-.1,1.1]); grid on; Title('Finding edge 1D: threshold=0.1'); subplot(2,1,2); hold on; threshold = .1; for sigma=.5:.5: imEdge = EdgeDetect1D(I, sigma, threshold); for x=1:length(imEdge) if imEdge(x)>0.99 % 1 means the edge plot(x,sigma,'.r'); end end end axis([0,160,0,26]); grid on; xlabel('1D position'); ylabel('sigma'); Title('red points: positions of edge, no edges found when sigma is large');

File hwk2e.m

% hwk2e.m clear all; IOld = [zeros(1,50), 9ones(1,10), zeros(1,10), 3ones(1,40), zeros(1,50)]; I = IOld + randn(1,length(IOld)); figure(1); clf, hold on; subplot(2,1,1); plot(1:length(IOld), IOld,'b-', 1:length(I), I,'r-'); legend('Original signal', 'Plus noise'); axis([0,160,-1.5,10.5]); grid on; Title('Finding edge for noised 1D image: threshold=0.1'); subplot(2,1,2); hold on; threshold = .1; for sigma=.5:.5: imEdge = EdgeDetect1D(I, sigma, threshold);

Appendix 2: code for problem 3

File hwk3.m

% hwk3.m, homework problem # clear all; I = imread('1.jpg'); E1 = edge(I, 'canny', [.02, .1], 1); E1o = edge(I, 'canny', [.3, .4], 1.5); imwrite(E1,'1E.jpg', 'JPEG'); imwrite(E1o,'1Eo.jpg', 'JPEG'); figure(1); clf; hold on; subplot(2,3,1); imshow(I); title('original image'); subplot(2,3,2); imshow(E1); title('default threshold'); subplot(2,3,3); imshow(E1o); title('threshold=[.3,.4] sig=1.5'); I = imread('2.jpg'); E1 = edge(I, 'canny', [.02, .1], 1); E1o = edge(I, 'canny', [.20, .3], 1.5); imwrite(E1,'2E.jpg', 'JPEG'); imwrite(E1o,'2Eo.jpg', 'JPEG'); subplot(2,3,4); imshow(I); title('original image'); subplot(2,3,5); imshow(E1); title('default threshold'); subplot(2,3,6); imshow(E1o); title('threshold=[.2,.3] sig=1.5');