Info

此问题已关闭。 请重新打开它进行编辑或回答。

I am comparing the ball position and according to position i am sending string through serial communication to controller. But that program gives error at some times n some times run successfully

1 次查看(过去 30 天)
The error will occur at "if " condition, the error is giving below -:
" ??? Undefined function or method 'bc' for input arguments of type 'double'.
Error in ==> say2 at 48
if (bc(1)<x1); "
Could you give me the reason of this? I didn't get solution .
code is:-
clc;
clear all;
%program to detect red color only
vid = videoinput('winvideo',1,'YUY2_640x480'); % Capture the video frames using the videoinput function
% Set the triggring of the video object and getting frames
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb');
vid.FrameGrabInterval = 5; %setting interval between two frames
start(vid); %start the video aquisition here
data = getsnapshot(vid);
[a b c]= size(data);
x=b;
x1=x/2-120;
x2=x/2+120;
ser=serial('COM1');
fopen(ser);
while(1) %Set a loop that stop after 100 frames of aquisition
data = getsnapshot(vid); % Get the snapshot of the current fram
flushdata(vid);
% 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));
diff_im = medfilt2(diff_im, [3 3]); % median filter to filter out noise
diff_im = im2bw(diff_im,0.18); % Convert the grayscale image into a binary image.
diff_im = bwareaopen(diff_im,300); % Remove all those pixels less than 300px
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid'); % Set the properties for each region
imshow(data)
hold on
%Looping to bound the red objects in a bounding box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
double(bc)
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
if (bc(1)<x1);
disp('right') ,fprintf(ser,'R');
elseif ((bc(1))>x2)
disp('left') ,fprintf(ser,'L');
elseif (x1<(bc(1))<x2)
disp('forwd') ,fprintf(ser,'F');
else
disp('stop') ,fprintf(ser,'S');
end
hold off
end
stop(vid); % Stop the video aquisition.
flushdata(vid); % Flush all the image data stored in the memory buffer.

回答(1 个)

Image Analyst
Image Analyst 2012-4-16
Probably because there are no detected regions. Put this in before your "for" loop
if isempty(stats)
return;
end

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by