find barycenter of several objects on an image

14 views (last 30 days)
LeChat
LeChat on 23 Jan 2020
Answered: Image Analyst on 23 Jan 2020
Hi,
I have a logical symmetric matrix in matlab: crossing_table, see attached file as well as the plot of this matrix.
I would like to identify the white "blobs" (objects?) that are on the upper part of the image (above the diagonal) and to compute the barycenter of each of these blobs.
Is there any Matlab function which would allow me to get the coordinates (indexes) of all pixels of a given blob, for each blob?
Thank you for your help!

Accepted Answer

LeChat
LeChat on 23 Jan 2020
I just tried the following, I think I am getting close but I am quite there:
% take the upper part of the matrix (above the diagonal)
crossing_tri=tril(nan(size(crossing_table)))+[tril(crossing_table')]';
% remove the diagonal (with the cloud around it)
for icol=2:size(crossing_tri,2)
lastblack=find(crossing_tri(1:icol,icol)==0,1,'last');
crossing_tri(lastblack:icol,icol)=0;
end
% make it a logical matrix (with no NaN)
ab(isnan(crossing_tri))=0;
figure
imshow(crossing_tri)
hold on
s=regionprops(logical(crossing_tri),'Centroid')
centroids=cat(1,s.Centroid)
plot(centroids(:,1),centroids(:,2),'rx')

More Answers (1)

Image Analyst
Image Analyst on 23 Jan 2020
LeChat, try this:
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;
%===============================================================================
% Read in demo image.
folder = pwd;
baseFileName = 'crossing_table1.mat';
% Get the full filename, with path prepended.
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
s = load(fullFileName)
binaryImage = s.crossing_table;
% Display the image.
hFig = figure;
subplot(1, 2, 1);
imshow(binaryImage, []);
title('Original Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns] = size(binaryImage)
% Take blobs only if they're larger than 300 pixels to get rid of the center diagonal blob.
binaryImage2 = binaryImage & ~bwareaopen(binaryImage, 300);
% Get a mask for the lower triangular part of the image
% Extract only the elements above the main diagonal.
binaryImage2 = triu(binaryImage2, 1);
% Display the image.
subplot(1, 2, 2);
imshow(binaryImage2, []);
title('Masked Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Find the centriods and areas of the blobs, along with coordinates of every point in the blob:
props = regionprops(binaryImage2, 'Area', 'Centroid', 'PixelList');
% Use 'WeightedCentroid' if you want it weighted by the original gray scale image.
allAreas = [props.Area]
% Get all centroids in a N by 2 (x,y) matrix.
centroids = vertcat(props.Centroid);
% props(k).PixelList has the (x,y) coordinates of every white pixel in the kth blob.
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;

Community Treasure Hunt

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

Start Hunting!