distance between two centroids of objects in an image using image processing toolbox in matlab
2 views (last 30 days)
Show older comments
From the image given I would like to find the distance of green dotted line.Where the direction line is to define path of objects which are placed on either side I would like to find centroids, from that would like to extract lengths of both blue and green dotted lines
5 Comments
Answers (1)
Image Analyst
on 13 Dec 2022
Here is one way. I ignored the drawn centerline and I fit the blob centroids to a line, though there are other ways you could possibly define the centerline.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'gait.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgb2gray(grayImage);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Show the histogram
subplot(2, 2, 2);
imhist(grayImage);
title('Histogram of Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
grid on;
%--------------------------------------------------------------------------------------------------------
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
lowThreshold = 0;
highThreshold = 200;
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage)
%--------------------------------------------------------------------------------------------------------
% Create a mask
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Fill blobs
mask = imfill(mask,"holes");
% Determine areas
% props = regionprops(mask, 'Area');
% allAreas = sort([props.Area])
% Take largest blobs only.
mask = bwareafilt(mask, [10000, inf]);
% Get rid of the vertical black line.
% Determine aspect ratios. Vertical line will have a high aspect ratio.
props = regionprops(mask, 'Area', 'BoundingBox');
allAreas = sort([props.Area])
bb = vertcat(props.BoundingBox);
aspectRatios = [bb(:, 4) ./ bb(:, 3)]
lineIndex = find(aspectRatios > 20)
labeledImage = bwlabel(mask);
labeledImage(labeledImage == lineIndex) = 0;
mask = logical(labeledImage);
% Display the final mask.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
% Determine Centroids
props = regionprops(mask, 'Centroid');
xyCentroids = vertcat(props.Centroid)
xCentroids = xyCentroids(:, 1);
yCentroids = xyCentroids(:, 2);
% Sort centroids from top to bottom (increasing y).
[yCentroids, sortOrder] = sort(yCentroids, 'ascend')
xCentroids = xCentroids(sortOrder)
hold on
plot(xCentroids, yCentroids, 'r+', 'MarkerSize', 25, 'Linewidth', 2)
% Fit the x,y to a line to get the centerline.
coefficients = polyfit(yCentroids, xCentroids, 1);
xFit = 1:rows;
centerLineColumns = polyval(coefficients, xFit);
plot(centerLineColumns, xFit, 'r-', 'Linewidth', 2);
centerlineX = polyval(coefficients, yCentroids)
deltax = centerlineX - xCentroids
% Get the delta x from the centroid to the centerline column.
for k = 1 : length(xCentroids)
thisX = xCentroids(k)
centerlineX(k)
% Draw a green line from centroid to centerline
line([thisX, centerlineX(k)], [yCentroids(k), yCentroids(k)], 'LineWidth', 3, 'Color', 'g')
end
% Plot the deltas
subplot(2, 2, 4);
bar(deltax);
grid on;
xlabel('Step Number', 'FontSize', fontSize)
ylabel('Delta X from Centerline', 'FontSize', fontSize)
title('Delta X from Centerline', 'FontSize', fontSize)
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!