필터 지우기
필터 지우기

I need to save centroid coordinates in a video

조회 수: 6 (최근 30일)
Havva Jabbar
Havva Jabbar 2019년 12월 19일
댓글: Image Analyst 2019년 12월 20일
How do I save the coordinates of my centroid as I need to get many coordinates?
a = imaqhwinfo;
%[camera_name, camera_id, format] = getCameraInfo(a);
% Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput(winvideo, camera_id, format);
% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
%start the video aquisition here
start(vid)
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=200)
% Get the snapshot of the current frame
data = getsnapshot(vid);
% Now 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));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
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');
allCentroids = [measurements.Centroid];
centroidX = allCentroids(1:2:end); % Extract x centroids.
centroidY = allCentroids(2:2:end); % Extract y centroids.
end
hold off
end
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
% Clear all variables
clear all
sprintf('%s','That was all about Image tracking, Guess that was pretty easy :) ')
I want to do so by creating an array. So far I have built on the red object detection code on matlab.

채택된 답변

Image Analyst
Image Analyst 2019년 12월 19일
You don't need to do this:
sprintf('%s','That was all about Image tracking, Guess that was pretty easy :) ')
instead just do this:
sprintf('That was all about Image tracking, Guess that was pretty easy :) \n');
To save the coordinates, you need to index it.
Get rid of this from the loop:
allCentroids = [measurements.Centroid];
centroidX = allCentroids(1:2:end); % Extract x centroids.
centroidY = allCentroids(2:2:end); % Extract y centroids.
Then save allCentroids in a cell array (since it may have a variable number of centroids per frame) right after you call regionprops() on that frame:
stats = regionprops(bw, 'BoundingBox', 'Centroid');
allCentroids{frameNumber} = vertcat(stats.Centroid);
frameNumber = frameNumber + 1;
Make sure you initialize frameNumber before the while loop
frameNumber = 1;
After that, you can do whatever you want, like call save() to store it in a .mat file on disk, or whatever you want.
  댓글 수: 2
Havva Jabbar
Havva Jabbar 2019년 12월 20일
Thank you so much image analyst. Now, if I want to use the values inside the cell array to do some computation, how can I? What I want to do is detect red points at knee,ankle and hip and calculate angles from it.
Image Analyst
Image Analyst 2019년 12월 20일
You can get the results back out of the cell array like this:
theseCentroids = allCentroids{frameNumber}
Some people in my company are using Cornell University's Open Pose to get angle, location, and distance information from such key locations (jointed locations) on human bodies:
It works remarkably well. I don't know all the implementation details since I've only seen the results, and I haven't done the programming myself.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by