Coordinates of objectes in the images

20 次查看(过去 30 天)
JM
JM 2024-11-14,22:32
回答: Walter Roberson 2024-11-14,23:21
Hallo everyone,
Now I want to track the objects in the images. I use centroid of objects, because the objects only falll down in the y direction, so I just want to fix x coordinate in the sequence images and only find the difference of y coordinate, if two images with same x coordinate, means that is the same objects that i can follow. and of course if i know the difference of y coordinate, i can calcute the velocity. so how to achive that? left one is first image, right side is second image.
This following two inages in which I draw single object.
% Parameters
imageFolder = ''; % Specify your image folder
images = dir(fullfile(imageFolder, '*.png')); % Assuming images are in png format
numImages = length(images);
threshold = 118; % Adjust as needed for particle detection
% Preallocate arrays for tracking
positions = [];
% Loop through each image to detect particles
for i = 1:numImages
% Read the image
img = imread(fullfile(imageFolder, images(i).name));
% Convert to grayscale (if colored)
if size(img, 3) == 3
grayImg = rgb2gray(img);
else
grayImg = img; % If already grayscale, no conversion needed
end
% Apply threshold to create a binary image
% binaryImg = grayImg threshold;
% Label connected components
[labels, numParticles] = bwlabel(binaryImg);
% Measure properties of the particles
stats = regionprops(labels, 'Centroid');
% Store the current positions of the particles
if ~isempty(stats)
positions = [positions; i * ones(numParticles, 1), vertcat(stats.Centroid)];
end
% Visualize tracking (optional)
imshow(img);
hold on;
if ~isempty(stats)
for j = 1:numParticles
plot(stats(j).Centroid(1), stats(j).Centroid(2), 'r*', 'MarkerSize', 20);
end
end
hold off;
pause(5); % Adjust pause to control playback speed
end
% Post-processing: velocity calculation
if size(positions, 1) > 1 % Ensure there is more than one position
velocities = zeros(size(positions, 1) - 1, 3); % Preallocate velocity array
for i = 1:(size(positions, 1) - 1)
% Calculate velocity components
dt = 1; % Assuming uniform time intervals for each frame; adjust if necessary
velocities(i, 1) = (positions(i + 1, 2) - positions(i, 2)) / dt; % Velocity in x
velocities(i, 2) = (positions(i + 1, 3) - positions(i, 3)) / dt; % Velocity in y
velocities(i, 3) = positions(i, 1); % Frame index
end
% Visualize velocities
figure;
quiver(positions(1:end-1, 2), positions(1:end-1, 3), velocities(:, 1), velocities(:, 2));
title('Velocity Field');
xlabel('X Position');
ylabel('Y Position');
axis equal;
axis tight; % Adjust axis limits to the data
else
disp('Not enough data to calculate velocities.');
end
This aboved code is can detect all the centroid position, however it doesn't meet my expectation.
Thanks so much in advance
JM

回答(1 个)

Walter Roberson
Walter Roberson 2024-11-14,23:21
You velocity logic is incorrect.
You build up a list of centroids. There can be more than one centroid in any image, and you concatenate the centroid lists between all of the images. There can be a different number of centroids detected for each image, and because of the way the label algorithm works, potentially the order of objects could switch a little between images.
But your velocity calculation is
(positions(i + 1, 2) - positions(i, 2)) / dt
This is comparing different objects within the same image (mostly), but at boundaries between images it is comparing the last object detected in one image to the first object detected in another image.
You should be instead approximately grouping objects based on x coordinates, and calculating velocities only within the groups.
You should not be grouping based on exact equality of x coordinates of centroids: as objects fall, the exact match between ideal centroid and discretized centroid is likely to vary a little. You should group based on ismembertol() or the like.
When grouping, you need to potentially introduce placeholders for objects that are missing in some images, since they might show up again in later images. You cannot just smash all related objects together in a single list because your velocity estimate depends upon time, and an object that disappears on the 4th frame and reappears on the 6th frame needs to be estimated with (dt*3) instead of (dt)
You have unanswered questions about what to do if two different objects happen to have (approximately) the same X centroid coordinates.

类别

Help CenterFile Exchange 中查找有关 Tracking and Motion Estimation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by