Blog moved to Wordpress.com  

Posted by shashank

There are a lot of features lacking in Blogger.com
The one feature that I particularly missed on Blogger was the ability to select the privacy option on each post.
Hence when Abhishek informed me about Wordpress.com, I discovered this feature on the same and hence I promptly moved my blog to Wordpress.com

I kindly request the interested reader to visit my Wordpress.com blog, the redirection link of which is given below:

http://sgsawant.wordpress.com/

Replacing NaNs with zeros  

Posted by shashank in

%File Name: "nans.m"

%Title: Get rid of NaNs

%Author: Shashank G. Sawant

%Notes: Quite honestly I found this somewhere on the internet.

%Here I have just adapted it to a 2D Matrix according to my req.

mwnans=[1 4 NaN 5; 5 4 98 NaN; NaN NaN 3 2]; %Matrix with NaNs

t=find(isnan(mwnans));

mwonans=mwnans; %Initialize Matrix without %NaNs

mwonans(t)=zeros(size(t));

%somehow the mwonans doesn't have the NaNs which mwnans has


Result:


>> nans
>> mwnans
mwnans =
1 4 NaN 5
5 4 98 NaN
NaN NaN 3 2
>> mwonans
mwonans =
1 4 0 5
5 4 98 0
0 0 3 2
>>


Good Riddance!

Generation of Gaussian Kernel Mask  

Posted by shashank in

After a lot of searching on the internet I couldn’t find a simple function to generate a Gaussian kernel mask of a given size and variance.

Hence the following code:

% ---------------------------------------------------------

% Author: Shashank G. Sawant

% Date: November 04, 2009

% Title: Generation of 2D Gaussian kernel mask

% Details:

% Mask Size: size x size = (2*k+1)x(2*k+1)

% Variance: Sigma

% Notes: The program is inefficient but easy to understand

% Also it accepts only odd numbers as its size parameter

% ---------------------------------------------------------

function H=gkm(size,sigma)

H=zeros(size);

k=(size-1)/2;

for i=1:(2*k+1)

for j=1:(2*k+1)

itpss=double(1/(2*pi*sigma^2));

num=double(((i-k-1)^2+(j-k-1)^2));

den=double(2*sigma^2);

H(i,j)=double(itpss*exp(-num/den));

end

end

The results of the same are as follows:

Result 1::

>> ceil(159*gkm(5,1.3))
ans =
2 4 5 4 2
4 9 12 9 4
5 12 15 12 5
4 9 12 9 4
2 4 5 4 2

The above result is quite a standard in the literature explaining Canny Edge Detection

Result 2:

>> format long

>> gkm(7,0.84089642)

ans =

Columns 1 through 4

0.000000667834529 0.000022915625609 0.000191165460524 0.000387705531367

0.000022915625609 0.000786311390357 0.006559523253494 0.013303467276683

0.000191165460524 0.006559523253494 0.054720490941357 0.110979446595403

0.000387705531367 0.013303467276683 0.110979446595403 0.225079076498442

0.000191165460524 0.006559523253494 0.054720490941357 0.110979446595403

0.000022915625609 0.000786311390357 0.006559523253494 0.013303467276683

0.000000667834529 0.000022915625609 0.000191165460524 0.000387705531367

Columns 5 through 7

0.000191165460524 0.000022915625609 0.000000667834529

0.006559523253494 0.000786311390357 0.000022915625609

0.054720490941357 0.006559523253494 0.000191165460524

0.110979446595403 0.013303467276683 0.000387705531367

0.054720490941357 0.006559523253494 0.000191165460524

0.006559523253494 0.000786311390357 0.000022915625609

0.000191165460524 0.000022915625609 0.000000667834529

The above result matches with the following Wikipedia article:

http://en.wikipedia.org/wiki/Gaussian_blur

Circle Drawing Algorithm in Matlab  

Posted by shashank in


Reading values located on a circle form a part of the template matching algorithm I am planning to implement. Hence I have written the following Matlab code.

Original source for the algorithm: http://cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html

function imgmat = drwcir(imgmat, xCenter, yCenter, radius)

x = 0;

y = radius;

p = floor((5 - radius*4)/4);

imgmat = cirpoints(imgmat, xCenter, yCenter, x, y);

while x < y,

x=x+1;

if (p<0)

p = p + 2*x+1;

else

y=y-1;

p = p + 2*(x-y)+1;

end

imgmat = cirpoints(imgmat, xCenter, yCenter, x, y);

end

return;

function img = cirpoints(img, cx, cy, x, y)

if (x == 0)

img = setpix(img, cx, cy + y);

img = setpix(img, cx, cy - y);

img = setpix(img, cx + y, cy);

img = setpix(img, cx - y, cy);

elseif (x == y)

img = setpix(img, cx + x, cy + y);

img = setpix(img, cx - x, cy + y);

img = setpix(img, cx + x, cy - y);

img = setpix(img, cx - x, cy - y);

elseif (x < y)

img = setpix(img, cx + x, cy + y);

img = setpix(img, cx - x, cy + y);

img = setpix(img, cx + x, cy - y);

img = setpix(img, cx - x, cy - y);

img = setpix(img, cx + y, cy + x);

img = setpix(img, cx - y, cy + x);

img = setpix(img, cx + y, cy - x);

img = setpix(img, cx - y, cy - x);

end

return;

function img = setpix(img,x,y)

img(x,y)=1;

return;



The following commands result in the accompanying picture:


figure;
image=zeros(500);
for i=1:2:250,
image=drwcir(image,250,250,i);
end
imshow(image);


IF statement, Loops and Functions in Matlab  

Posted by shashank in

Switching back and forth from one programming language to the other usually makes me forget the basic commands. Hence the post.

IF statement in Matlab: (Source: http://www.cyclismo.org/tutorial/matlab/if.html)

a = 2;
b = 3;
if (a<b)
j = -1;
end

For Loop in Matlab: (Source: http://www.cyclismo.org/tutorial/matlab/control.html)
>> v = [1:3:10]

v =

1 4 7 10

>> for j=1:4,
v(j) = j;
end
>> v

v =

1 2 3 4

Functions in Matlab: (Source: http://web.cecs.pdx.edu/~gerry/MATLAB/programming/basics.html)

The following 2 functions have to be stored in the files name traparea.m and cart2plr.m respectively. One can include auxiliary functions in the same file but the scope of such functions ends
 function area = traparea(a,b,h)
% traparea(a,b,h) Computes the area of a trapezoid given
% the dimensions a, b and h, where a and b
% are the lengths of the parallel sides and
% h is the distance between these sides

% Compute the area, but suppress printing of the result
area = 0.5*(a+b)*h;

Finally, here is another simple function, cart2plr.m, with two input parameters and two output parameters.

 function [r,theta] = cart2plr(x,y)
% cart2plr Convert Cartesian coordinates to polar coordinates
%
% [r,theta] = cart2plr(x,y) computes r and theta with
%
% r = sqrt(x^2 + y^2);
% theta = atan2(y,x);

r = sqrt(x^2 + y^2);
theta = atan2(y,x);

The following are 2 more examples: (Source: Matlab Help)
Example 1
The existence of a file on disk called stat.m containing
this code defines a new function called stat that calculates
the mean and standard deviation of a vector:

function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));

Example 2
avg is a subfunction within the file stat.m:


function [mean,stdev] = stat(x)
n = length(x);
mean = avg(x,n);
stdev = sqrt(sum((x-avg(x,n)).^2)/n);

function mean = avg(x,n)
mean = sum(x)/n;