Hello! I would like to find the centroid formed by three markers (red colored) after tracking them. I got the available code that plots the x,y co-ords of each of these markers successfully. I have added a loop to calculate the mean of these co-ordinates to find the centroid formed by these three markers. I am not able to plot it plzz help!
imaqreset
vid = videoinput('winvideo',1,'MJPG_640x360'); set(vid,'FramesPerTrigger',Inf); set(vid, 'ReturnedColorSpace','rgb'); vid.FrameGrabInterval =5; x=0; y=0; mx=0; my = 0; start(vid); while (vid.FramesAcquired<=200) data = getsnapshot(vid);
diff = imsubtract(data(:,:,1),rgb2gray(data));
diff = medfilt2(diff, [3 3]);
diff = im2bw(diff, 0.18);
diff = bwareaopen(diff , 300);
bw = bwlabel(diff , 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+')
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');
x = x + round(bc(1));
y = y + round(bc(2));
mx = x/3;
my = y/3;
b = text(mx , my , strcat('XC:',round(mx) , 'XY:',round(my))); set(b, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 16, 'Color', 'white');
end
hold off
end
Best Answer