# Matlab for moving a robot towards the detected block

the matlab code is used to detect red colored object, but i want to control a bot to move towards the detected object. just need a simple algorithm or idea, controlling the servo i will be able to do it.

%get snapshot
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);

% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);

% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);

% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');

% Display the image
imshow(data)

hold on

%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), '    Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end

hold off

• would you please be more specific about the problem? Is it a computer vision problem or a mere geometric one? – CroCo Aug 5 '14 at 17:11
• computer vision problem, i mean so far i am able to detect the objects and find the distances between the robot and the objects but i'm unable to move the robot to the target object(red block) as shown in the diagram. – Nabajyoti Das Aug 5 '14 at 17:27
• actually i want to design an autonomous bot that uses image processing to locate the block of different colours and to put the block in respective color zone. I will be placing the camera above the center of the arena, such that the camera gets the top view of the arena, and then detects the different colored blocks and the bot. – Nabajyoti Das Aug 5 '14 at 17:45

Image X and Y will reflect x and Y coordinates on the floor. Lens has distortion. Use calibration (if opencv), if high accuracy needed (may not need in this case).