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??
3 views (last 30 days)
Show older comments
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
Image Analyst
on 16 May 2015
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.
Answers (1)
Image Analyst
on 16 May 2015
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
sidra
on 25 Dec 2017
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?
See Also
Categories
Find more on C4ISR in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!