matlab. Распознавание образов. Не обнаруживается объект. Immag Processing

Рейтинг: 1Ответов: 1Опубликовано: 19.04.2015

Я делаю проект, который должен отслеживать красный шар.

Появилась проблема, которую я не знаю, как решить. Код не обнаруживает мяч, и я не могу понять, почему.

Если кто-то найдёт ошибку, пожалуйста, скажите мне, как можно починить. Ниже приведён используемый мной код:

imaqhwinfo
dev_info = imaqhwinfo('winvideo',1)
%info=imaqhwinfo('winvideo')

celldisp(dev_info.SupportedFormats)
vid=videoinput('winvideo',1,'YUY2_640x480');

%% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;

%Achizitie video
start(vid)

%Initializare arduino
ser=serial('COM3','Baudrate',9600);
fopen(ser)
while(vid.FramesAcquired<150)
data = getsnapshot(vid);

%din gray extragem componenta rosie 
r = data(:,:,1);g = data(:,:,2); b = data(:,:,3);
bw1 = ((r >=248) & (g <=2) & (b <=2));
%bw1 = (r - g/2 - b/2);

% stergem pixelii mai mici deci 30
bw1 = bwareaopen(bw1,30);
bw = imfill(bw1,'holes');
[B,L] = bwboundaries(bw,'noholes');
%figure(1); subplot(224); imshow(bw1);

%nr de componente conectate din imagine
%bw = bwlabel(bw1, 8);
 %     bw1 = imfill(bw1,'holes');
 %     [B,L] = bwboundaries(bw,'noholes');

%Blob analiza 
stats = regionprops(L,'Area','Centroid');
circle_threshold = 0.80;
figure(1);
imshow(data);
hold on
for g = 1:length(B)

      % obtain (X,Y) boundary coordinates corresponding to label 'g'
      boundary = B{g};

      % compute a simple estimate of the object's perimeter
      delta_sq = diff(boundary).^2;
      perimeter = sum(sqrt(sum(delta_sq,2)));

      % obtain the area calculation corresponding to label 'g'
      area = stats(g).Area;

      % compute the roundness metric
      metric = 4*pi*area/perimeter^2;
      if metric > circle_threshold
           bc = stats(g).Centroid;
           plot(bc(1),bc(2),'ko');
           a=text(bc(1)+16,bc(2),strcat('X:     ',num2str(round(bc(1))),'    Y:  ', num2str(round(bc(2)))));
           set(a, 'FontName', 'Arial', 'FontWeight','bold','FontSize',12,'Color','yellow');
   %Poz=uint8(bc(1));

    X = round(bc(1));

   [pancmd] = pantilt(X);
   fwrite(ser,pancmd,'char');

   else
              [r,c] = find(L(:,:)==g);
            % [r,c] = find(bwlabel(bw)==g);
              bw(r,c)=0;
        end
end

%  pause
hold off
end
%b.servoWrite(9,0);
%pos = b.servoread(1);


%oprire achizite video
closepreview(vid)
stop(vid);
%delete(vid)

fclose(ser)
%sterge toate variabilele
clear all
sprintf('%s','gata')

Ответы

Ответов пока нет.