Red Ball Tracking Problem
5 views (last 30 days)
Show older comments
Hi. I have a question. A maked project wich should track a red ball. But appear the problem which bedevil me. There i put my code , if someone can help me, please.
if true
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));
% valoarea_prag = 60;
% Xc = 320;%320;
[pancmd] = pantilt(X);
fwrite(ser,pancmd,'char');
% if (Xc-X) > valoarea_prag
% fprintf(ser,'r');
% %%Find out if the X component of the face is to the right of the middle of the screen.
% elseif (X-Xc) > valoarea_prag
% fprintf(ser,'l');
% end
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') end
0 Comments
Answers (2)
Image Analyst
on 19 Apr 2015
See my attached code where I track a green object. Feel free to change the values to track a red object instead.
5 Comments
Image Analyst
on 19 Apr 2015
That should show a whole range of colors. If it's not doing the job, try using delta E like I did in my demo: http://www.mathworks.com/matlabcentral/fileexchange/31118-color-segmentation-by-delta-e-color-difference
Syed Asad Ali
on 22 Feb 2016
Hi..sir please understand me about this intensity formula and threshold for finding green intensity....
r=center(:,:,1); g=center(:,:,2); b=center(:,:,3); green=g-(r/2)-(b/2); bw=(green>30);
in this formula why use /2...and why g is place before r/2 and b/2..and also why use green>30...why use 30 in this...sir pls ans ASAP
1 Comment
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!