Matlab ball follower code not working ( suppose to show correct command) for simulation only
1 次查看(过去 30 天)
显示 更早的评论
clear;
clc
vid=videoinput('winvideo',2');%PLEASE CHECK THE RESOLUTION BEFORE TESTING
set(vid,'FramesPerTrigger',1);
set(vid, 'ReturnedColorspace', 'rgb')
%preview(vid);
pause(10);
rgb_image = getsnapshot(vid);
[a b c]= size(rgb_image);
y=a;
x=b;
x1=x/2-120;
x2=x/2+120;
y1=y/2-30;
y2=y/2+30;
%ser=serial('COM1');
%fopen(ser);
while(1)
rgb_image = getsnapshot(vid);
flushdata(vid);
rbar=0;
cbar=0;
e=0;
fR=rgb_image(:,:,1);fG=rgb_image(:,:,2);fB=rgb_image(:,:,3);
I = imsubtract(fR(:,:,1), rgb2gray(rgb_image));
% I=((fR>=80) & (fG<=70) & (fB<=70));%PLEASE SET THE THRESHOLD VALUES BEFORE TESTING
se=strel('disk',20);
B=imopen(I,se);
final=imclose(B,se);
[L,n]=bwlabel(final);
imshow(rgb_image);% THIS LINE IS NEEDED ONLY FOR TESTING SIMULATION
hold on % THIS LINE IS NEEDED ONLY FOR TESTING SIMULATION
for k=1:n
[r,c]=find(L==k);
rbar=mean(r);
cbar=mean(c);
plot(cbar,rbar,'Marker','*','MarkerEdgeColor','B','MarkerSize',20)% THIS LINE IS NEEDED ONLY FOR TESTING SIMULATION
e=(((cbar>=x1)*2*2*2) + ((cbar<=x2)*2*2) + ((rbar>=y1)*2) + (rbar<=y2))
end
hold off
switch (e)
case 5
disp('left');
case 6
disp('left');
case 7
disp('left');
case 9
disp('right');
case 10
disp('right');
case 11
disp('right');
case 13
disp('forward');
case 14
disp('back');
otherwise
disp('stop');
end
end
%fclose(ser);
if true
% code
end
0 个评论
回答(1 个)
Image Analyst
2014-3-23
Usually what most people do when their code is "not working" is to use the debugger . Go here to learn how: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ If you do that it will be much faster for you than debugging via the Answers forum where we have to go back and forth multiple times, with long delays in between, asking questions like "What exactly does 'not working' mean?" Once you learn to debug yourself, you can rapidly solve these questions without us.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!