This code detects red objects. I have create a robotic arm and I want via a camera to detect red objects and move the arm in these. So how can I modify the code so I have communication with the servo ???Any help with the code??

vid=videoinput('winvideo',1,'YUY2_320x240');
set(vid,'FramesPerTrigger',inf);
set(vid,'ReturnedColorspace','rgb')
vid.FrameGrabInterval=5;
a=arduino('COM3');
a.servoAttach(9);
a.servoAttach(10);
a.servoAttach(11);
start(vid);
while(vid.FramesAcquired<=100)
data=getsnapshot(vid);
diff_im=imsubtract(data(:,:,1),rgb2gray(data));
diff_im=medfilt2(diff_im,[3,3]);
diff_im=im2bw(diff_im,0.18);
diff_im=bwareaopen(diff_im,300);
bw=bwlabel(diff_im,8);
stats=regionprops(bw,'BoundingBox','Centroid');
imshow(data);
hold on
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+')
end
hold off
end
stop(vid);
flushdata(vid);
clear all;

2 Comments

Forgetting about the red color detection for the moment, are you able to talk to your servo at all yet? Figuring out how to do that, and how to move the arm to a specific location, is something that needs to be done first, or in parallel with your red object detection code.
yes im able to talk to all servos via matlab with a simple m-file code that i giving angles and my arm moves ,but im looking how to modify my code so my arm moves in red objects!!thanks

Sign in to comment.

Answers (1)

The camera calibration routines in the Computer Vision System Toolbox will make this easier for you. http://www.mathworks.com/help/vision/camera-calibration-and-3-d-vision.html You can hopefully be able to convert from a location in your image to a point in 3D real world space and then tell your servo to move the arm there. I don't have code for that. I'm sure it won't be some simple 100 line long chunk of code - it will be somewhat complicated.

3 Comments

bc (1) is the X coordinate and bc (2) is the Y coordinate. How do I use these 2 coordinate to control the 2 servo motor using matlab and arduino support package to track an object... any update on this query please?

Sign in to comment.

Asked:

on 16 May 2015

Commented:

on 25 Dec 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!