Find the centroid of images after real time tracking.
5 次查看(过去 30 天)
显示 更早的评论
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
1 个评论
Image Analyst
2014-5-10
Please read this and fix your code: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
采纳的回答
Image Analyst
2014-5-10
You don't need a loop to find the means of the blobs. You can do it this way
centroids = [stats.Centroid]; % Make into 2 D array.
xCentroids = mean(centroids(:,1)); % Get x centroid for every blob, if you need that.
yCentroids = mean(centroids(:,2)); % Get y centroid for every blob, if you need that.
% Get mean of all the blobs
meanX = mean(xCentroids);
meanY = mean(yCentroids);
Of course if you want to plot blob-specific things like the bounding box, centroid, a text label, etc., then that part needs to be in a loop. You can use xCentroids and yCentroids inside the loop (if you do the above code before the loop starts) to make it simpler.
3 个评论
Image Analyst
2014-5-12
Lekha's "Answer" moved here since it's not an answer to the original question:
And I tried this out , but its showing the same value of a x,y co-ordinate of the object and not the mean of three co-ords
Image Analyst
2014-5-12
It will plot those individual coordinates unless you delete the call to plot that plots them. Did you put in a call to plot(xMean, yMean)? It won't show that unless you explicitly call plot to tell it to show the centroid.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!