Discrete convolution with a Gaussian Kernel

  • Follow


Hi everyone.

I need to smooth a digital contour of an edge image (represented by an angular bending vector : k_ki) in order to eliminate the quantization noise. From a paper I read that this is done by means of a discrete convolution with a gaussian kernel. I am evaluating the contour using the k angular bending vector k_ki and plotting a graph of it (with angle vs number of pixels of contour). This graph shows the cornerns of the contour of the object (e.g. for a sqaure 4 peaks can be seen or for a triangle 3 peaks can be seen).

Now I am trying to smooth this graph plot data out with this filter. I am using the following code:

%k-angular bending (angle between a_ki and b_ki)
k_ki(g,1)=abs(180-acosd(dot(v1,v2)/(norm(v1)*norm(v2)))); 
figure 
plot(k_ki)

% Convolution (low-pass filtering)
filter = fspecial('gaussian',[s 1], 2.5);  % gaussian kernel where s= size of contour
K_ki = conv(k_ki, filter);     % convolution
figure
plot(K_ki)

Although the plot K_ki is smoothed as needed compared to k_ki, however in this case the second plot becomes with the x axis double the length and data is plotted at the centre instead of the beginning of the plot. Am I missing something?? Also I dont know if conv or con2 has to be used in this case

Regards 
Kenneth 
0
Reply Kenneth 3/31/2010 6:43:04 PM

"Kenneth Galea" <k.galea@hotmail.com> wrote in message <hp053o$eil$1@fred.mathworks.com>...
> Hi everyone.
> 
> I need to smooth a digital contour of an edge image (represented by an angular bending vector : k_ki) in order to eliminate the quantization noise. From a paper I read that this is done by means of a discrete convolution with a gaussian kernel. I am evaluating the contour using the k angular bending vector k_ki and plotting a graph of it (with angle vs number of pixels of contour). This graph shows the cornerns of the contour of the object (e.g. for a sqaure 4 peaks can be seen or for a triangle 3 peaks can be seen).
> 
> Now I am trying to smooth this graph plot data out with this filter. I am using the following code:
> 
> %k-angular bending (angle between a_ki and b_ki)
> k_ki(g,1)=abs(180-acosd(dot(v1,v2)/(norm(v1)*norm(v2)))); 
> figure 
> plot(k_ki)
> 
> % Convolution (low-pass filtering)
> filter = fspecial('gaussian',[s 1], 2.5);  % gaussian kernel where s= size of contour
> K_ki = conv(k_ki, filter);     % convolution
> figure
> plot(K_ki)
> 
> Although the plot K_ki is smoothed as needed compared to k_ki, however in this case the second plot becomes with the x axis double the length and data is plotted at the centre instead of the beginning of the plot. Am I missing something?? Also I dont know if conv or con2 has to be used in this case
> 
> Regards 
> Kenneth 

If I understood you correctly using "imfilter" with 'same' property would solve your problem.
0
Reply Royi 3/31/2010 7:13:04 PM


"Kenneth Galea" <k.galea@hotmail.com> wrote in message <hp053o$eil$1@fred.mathworks.com>...
> Hi everyone.
> 
> I need to smooth a digital contour of an edge image (represented by an angular bending vector : k_ki) in order to eliminate the quantization noise. From a paper I read that this is done by means of a discrete convolution with a gaussian kernel. I am evaluating the contour using the k angular bending vector k_ki and plotting a graph of it (with angle vs number of pixels of contour). This graph shows the cornerns of the contour of the object (e.g. for a sqaure 4 peaks can be seen or for a triangle 3 peaks can be seen).
> 
> Now I am trying to smooth this graph plot data out with this filter. I am using the following code:
> 
> %k-angular bending (angle between a_ki and b_ki)
> k_ki(g,1)=abs(180-acosd(dot(v1,v2)/(norm(v1)*norm(v2)))); 
> figure 
> plot(k_ki)
> 
> % Convolution (low-pass filtering)
> filter = fspecial('gaussian',[s 1], 2.5);  % gaussian kernel where s= size of contour
> K_ki = conv(k_ki, filter);     % convolution
> figure
> plot(K_ki)
> 
> Although the plot K_ki is smoothed as needed compared to k_ki, however in this case the second plot becomes with the x axis double the length and data is plotted at the centre instead of the beginning of the plot. Am I missing something?? Also I dont know if conv or con2 has to be used in this case
==========

Well, it's a standard fact about convolution that it lengthens a signal. However, MATLAB's convolution functions allow you to pass a 'same' flag, which will truncate the result to the domain where the original signal resides.
0
Reply Matt 3/31/2010 7:44:06 PM

"Kenneth Galea" <k.galea@hotmail.com> wrote in message <hp053o$eil$1@fred.mathworks.com>...

> Although the plot K_ki is smoothed as needed compared to k_ki, however in this case the second plot becomes with the x axis double the length 
================

Another thing. If the x-axis is double its original length, it means the filter kernel has the same length as your signal (in the x-direction). In that case, it's inefficient to do a direct convolution using conv() or similar. Instead, you should use FFTs to implement the convolution
0
Reply Matt 3/31/2010 8:55:19 PM

As Matt points out, you are making the Gaussian kernel the same length as your series - hence the increase in length. In fact, since your sigma parameter is 2.5, most of the Gaussian kernel will be extremely close to zero, so you can, with only a small error, truncate it. A length of round(6*sigma), rather than s, in the call to fspecial is likely to be more than enough for most practical purposes (but you can do some comparisons with the long kernel to see how big the errors are). This will speed up your computation greatly if s is large - probably more than the fft would.

It doesn't matter whether you use conv or conv2. Since you are working in 1D, it's neater to stick with conv. It has a 'same' option and will do the truncation for you if you wish. If you use the 'valid' option you don't get any results that are affected by the zero-padding that is otherwise necessary.
0
Reply David 3/31/2010 9:44:07 PM

"David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <hp0fn7$5ol$1@fred.mathworks.com>...

> It doesn't matter whether you use conv or conv2. Since you are working in 1D, it's neater to stick with conv. It has a 'same' option and will do the truncation for you if you wish. If you use the 'valid' option you don't get any results that are affected by the zero-padding that is otherwise necessary.
============

Actually, in older versions of MATLAB I seem to remember that only conv2() but not conv() has the 'same' option, so the OP may have to resort to conv2
0
Reply Matt 3/31/2010 9:54:04 PM

"David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <hp0fn7$5ol$1@fred.mathworks.com>...

> It doesn't matter whether you use conv or conv2. Since you are working in 1D, it's neater to stick with conv. It has a 'same' option and will do the truncation for you if you wish. If you use the 'valid' option you don't get any results that are affected by the zero-padding that is otherwise necessary.
============

Actually, in older versions of MATLAB I seem to remember that only conv2() but not conv() has the 'same' option, so the OP may have to resort to conv2
0
Reply Matt 3/31/2010 10:00:23 PM

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hp0gln$j1h$1@fred.mathworks.com>...
> "David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <hp0fn7$5ol$1@fred.mathworks.com>...
> 
> > It doesn't matter whether you use conv or conv2. Since you are working in 1D, it's neater to stick with conv. It has a 'same' option and will do the truncation for you if you wish. If you use the 'valid' option you don't get any results that are affected by the zero-padding that is otherwise necessary.
> ============
> 
> Actually, in older versions of MATLAB I seem to remember that only conv2() but not conv() has the 'same' option, so the OP may have to resort to conv2


Hi. Thanks to all of you my problem is solved. Actually i tried these two methods and they both worked great:
filter = fspecial('gaussian',[s 1], 2.5);  % gaussian kernel
K_ki = conv2(k_ki, filter, 'same');     % convolution
    OR
K_ki = imfilter(k_ki, filter, 'conv', 'same')

However I still need to ask another question. When trying out a hexagon (with six sides) the NON filtered plot k_ki showed exactly six peaks....i.e. the beginnning and end of the graph could be seen as a continuation. However with the filtered plot K_ki , the plot doesn't seem as a continuity from its start and end point. It seems like there are 7 vertices instead of 6. Why is this so?? 

Regards
Kenneth
0
Reply Kenneth 3/31/2010 11:46:06 PM

7 Replies
754 Views

(page loaded in 0.055 seconds)

Similiar Articles:













7/20/2012 1:36:36 PM


Reply: