# 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? 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. 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. 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).

Original answer was mis-understood:

Height and Width is distance (far, smaller image) then PID to control speed, slow down when near

Left, Central, Right of the block is angle of block as reference to vehicle coordinate frame and calculate the motion angle

• sorry i didn't get you, actually i want to move the robot to the detected object without using any sensor. Like giving a specific color to the robot and then moving the bot until robot color centroid and object color centroid comes together. Aug 2 '14 at 19:51
• Sorry I mis-understood that camera is on robot (looking eye). see youtube.com/watch?v=bSeFrPrqZ2A Use distinct color for block and robot so that they can be 'far different' under HSV color space. Read image, convert to HSV space. Locate two color blocks. Calculate centroid of two color bloc as x0,y0 and x1,y1. use 2D trig to calculate distance and to-go direction. The example is opencv, believe there are similar functions in matlab
– EEd
Aug 2 '14 at 20:03
• Each robot has color patch on top. Ceiling Camera look down and see the robot patches cs.cmu.edu/~robosoccer/small/#media
– EEd
Aug 2 '14 at 20:12
• one more help needed, i am able to find out the distance between two centroids, but how to find the angles? Please help Aug 2 '14 at 20:51
• If camera is 0 to 99 on xy dimension (replace with real resolution like 640 X 480). Says x0y0 is 0,0 (bottom left corner of camera) x1y1 is 99,99 (top right corner). the angle is 45 degrees. For very accurate result (may not needed) opencv has camera calibration to map out lens distortion error (straight line seen as slightly curved by camera)
– EEd
Aug 2 '14 at 21:12