Code covered by the BSD License  

Highlights from
Automata Engineer 2010 NITK

image thumbnail

Automata Engineer 2010 NITK

by

 

29 Oct 2010 (Updated )

This code solves the problem statement of Automata Engineer 2010 NITK.

get_ore( bw )
function [dir] = get_ore( bw )

%returns the orientation of the bot
%input: the inverted filtered bw image of the arena such that the circles
%       are white

leb=[0 0];
%dir='n';
%area=[0 0];
%cent=[0 0];
i=1;
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
% remove all object containing fewer than 30 pixels
bw = bwareaopen(bw,10);

% fill a gap in the pen's cap
%se = strel('disk',2);
%bw = imclose(bw,se);
% fill any holes, so that regionprops can be used to estimate

% the area enclosed by each of the boundaries
%bw = imfill(bw,'holes');
[B,L] = bwboundaries(bw,'noholes');

% Display the label matrix and draw each boundary
%for k = 1:length(B)
 % boundary = B{k};
 % plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
%end

stats = regionprops(L,'Area','Centroid');

%threshold = 0.94;
%imtool(bw)
% loop over the boundaries
for k = 1:length(B)

  % obtain (X,Y) boundary coordinates corresponding to label 'k'
  boundary = B{k};

  % compute a simple estimate of the object's perimeter
  delta_sq = diff(boundary).^2;
  perimeter = sum(sqrt(sum(delta_sq,2)));

  % obtain the area calculation corresponding to label 'k'
  area = stats(k).Area;

  % compute the roundness metric
  metric = 4*pi*area/perimeter^2;
  if (metric>0.8 && metric<1.2)
      leb(i)=k;
      %area(i)=stats(k).Area;
      %cent(i)=stats(k).Centroid;
      i=i+1;
  end
      
  %met(1,k)=metric;

  % display the results
 % metric_string = sprintf('%2.2f',metric);

  % mark objects above the threshold with a black circle
  %if metric > threshold
   % centroid = stats(k).Centroid;
    %plot(centroid(1),centroid(2),'ko');
  %end

  %text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',...
   %    'FontSize',14,'FontWeight','bold');

end

%title(['Metrics closer to 1 indicate that ',...
 %      'the object is approximately round']);

if(stats(leb(1)).Area > stats(leb(2)).Area)
   lcen=stats(leb(1)).Centroid;
   scen=stats(leb(2)).Centroid;
else
   lcen=stats(leb(2)).Centroid;
   scen=stats(leb(1)).Centroid;
end
%cent=[lcen scen];
if((scen(1)-lcen(1)>25) && abs((scen(2)-lcen(2))<5))
    dir=4;
elseif((lcen(1)-scen(1)>25) && abs((lcen(2)-scen(2))<5))
    dir=2;
elseif((scen(2)-lcen(2)>25) && abs((lcen(1)-scen(1))<5))
    dir=1;
elseif((lcen(2)-scen(2)>25) && abs((lcen(1)-scen(1))<5))
    dir=3;
else dir='\0';
end

    
    % else retake image and try
    
    
end

Contact us