Home » 2014

# Yearly Archives: 2014

## Basic MATLAB

Using the break command to stop the while loop for given condition

```t = 1;
while 1 % infinite loop
t = t+1;
if (t > 1000 ) % condition
break      % break the loop
end
end
```

## MATH Art

Math Formulas^2 + Creative Idea + square root (Randomness) – Time = Art !! Combining Art with Engineering, Science and Math. Check out more at Sevard.org ## Vehicle detection, blob analysis

clc; clear all; close all;

```% Read a grayscale or color image from the file
% Display the grayscale image I
figure(1)
imshow(img)``` Convert the grayscale image I to a binary image

```bwi = im2bw(img, graythresh(img));
bwi =~bwi; % As bacground is light its better to inverse the image
figure(2)
imshow(bwi)``` Remove from a binary image all connected components (objects) that have fewer than 100 pixels.do some adjsutements based on image

```bwi = bwareaopen(bwi,100);
figure(3);
imshow(bwi);
% Create morphological structuring element, something which resemples the
% blob
str = strel('square',20);
bw = imdilate(bwi,str);``` Trace region boundaries in binary image, As you see there might be some false detections.

```[B,L] = bwboundaries(bw,'noholes');
figure(4);
imshow(img);
hold on
for i = 1:length(B)
plot(B{i}(:,2), B{i}(:,1), 'r', 'LineWidth', 2.5)
end``` ## World Magnetic Model,Matlab

```close all;clear all;clc;
height = 500000; % A scalar value, in meters
lat = -90:2.5:90;  % geodetic latitude, in degrees, where north latitude is
%positive, and south latitude is negative
lon = -180:2.5:180;  % geodetic longitude, in degrees,
%where east longitude is positive,and west longitude is negative
% dyearA scalar decimal year. Decimal year is the desired year in
%a decimal format to include any fraction of the year that has already passed.
sla = size(lat);
sln = size(lon);
for i = 1:sla(2)
for j =1:sln(2)
[Bxyz, h, dec, dip, f] = ...
wrldmagm(height, lat(i), lon(j), decyear(2014,1,22),'2010');
% Note that WMM-2010 isvalid from January 1, 2010, until December 31,
% 2014
% Bxyz Magnetic field vector in nanotesla (nT)
% h Horizontal intensity in nanotesla (nT)
% dec Declination in degrees
% dip Inclination in degrees
% f Total intensity in nanotesla (nT)
Bx(i,j) = Bxyz(1,1);
By(i,j) = Bxyz(2,1);
Bz(i,j) = Bxyz(3,1);
Bm(i,j) = f;
%          [Bxyz(i,j), h(i,j), dec(i,j), dip(i,j), f(i,j)] = ...
%             wrldmagm(height, lat(i), lon(j), decyear(2014,1,22),'2010');
end
end```

Z component

```xwidth = 640;
ywidth = 320;
colormap jet
hFig = figure(1);
contourf(lon,lat,Bz)
hold on
set(gcf,'PaperPositionMode','auto')
set(hFig, 'Position', [100 100 xwidth ywidth])
axis([-180 180 -90 90])
colorbar
contour(0:179,-90:89,topo(:,1:180),[0 0],'.k')
contour(-180:-1,-90:89,topo(:,181:360),[0 0],'.k')
ylabel('Latitude [deg]')
xlabel('Longitude [deg]')
title('World Magnetic Model, Bz(nT) at Alt = 500 km')``` Component X

```hFig = figure(2);
contourf(lon,lat,Bx)
hold on
set(gcf,'PaperPositionMode','auto')
set(hFig, 'Position', [100 100 xwidth ywidth])
axis([-180 180 -90 90])
colorbar
contour(0:179,-90:89,topo(:,1:180),[0 0],'.k')
contour(-180:-1,-90:89,topo(:,181:360),[0 0],'.k')
ylabel('Latitude [deg]')
xlabel('Longitude [deg]')
title('World Magnetic Model, Bx(nT) at Alt = 500 km')``` Component Y

```hFig = figure(3);
contourf(lon,lat,By)
hold on
set(gcf,'PaperPositionMode','auto')
set(hFig, 'Position', [100 100 xwidth ywidth])
axis([-180 180 -90 90])
colorbar
contour(0:179,-90:89,topo(:,1:180),[0 0],'.k')
contour(-180:-1,-90:89,topo(:,181:360),[0 0],'.k')
ylabel('Latitude [deg]')
xlabel('Longitude [deg]')
title('World Magnetic Model, By(nT) at Alt = 500 km')``` Total intensity in nanotesla (nT)

```hFig = figure(4);
contourf(lon,lat,Bm)
hold on
set(gcf,'PaperPositionMode','auto')
set(hFig, 'Position', [100 100 xwidth ywidth])
axis([-180 180 -90 90])
colorbar
contour(0:179,-90:89,topo(:,1:180),[0 0],'.k')
contour(-180:-1,-90:89,topo(:,181:360),[0 0],'.k')
ylabel('Latitude [deg]')
xlabel('Longitude [deg]')
title('World Magnetic Model, Total intensity(nT) at Alt = 500 km')``` ## Distance Transform Path Planning Algorithm

clc;close all;clear all;

Input Map

```RGB = imread('Mapn.png');
figure('Position',[600 0 600 1000],'color','k');
image(RGB);
hold on;
axis off
axis image
hold off;
% Convert to binary image matrix and inverse matrix values
I = rgb2gray(RGB);
binaryImage = im2bw(RGB, 0.3);
binaryImage = 1-binaryImage;
% Inital pose
xs = 540; ys = 750;
% Goal Pose
xg = 150; yg = 150;``` Distance to Goal

```sz  = size(binaryImage);
for i = 1:sz(1)
for j = 1:sz(2)
rg(i,j) = sqrt((i-yg)^2 +(j - xg)^2);
end
end
% Pose Matrix
pose = zeros(sz(1),sz(2));
pose(ys,xs) = 1;
pose(yg,xg) = 1;```
```Di = 15; % Distance of influence of the obstacles
% Compute for every pixel the distance to the nearest obstacle(non zero
% elements which after inversion corespondent to the obstacles)
[D,IDX] = bwdist(binaryImage);
for i = 1:sz(1)
for j = 1:sz(2)
rd = D(i,j);
if (rd <= Di)
rg(i,j)  = 1e6;
end
end
end```

Distance Transform Path Planning Algorithm

```figure('Position',[600 0 600 1000],'color','k');
hold on;
image(RGB);
colormap gray
contour(rg,15);
spy(pose,'*r');
axis off
axis image
title('Distance Transform Path Planning Algorithm','color','w');
x = xs; y = ys;
last = rg(y-1,x-1);
while (x ~= xg)||(y ~= yg)
dis =[ rg(y-1,x-1), rg(y-1,x),rg(y-1,x+1);
rg(y,x-1), rg(y,x) ,rg(y,x+1);
rg(y+1,x-1), rg(y+1,x),rg(y+1,x+1)];
m = min(dis(:));
[r,c] = find(dis == m);
rg(y,x) = 1E6;
y = y-2+r;
x = x-2+c;
pose(y,x) = 1;
end
spy(pose,'.r');
set(gcf, 'InvertHardCopy', 'off');
hold off;``` 