I have video which contains a 1000 frames, in spesific frame I did segmentation and the image become white and black as it attched. how I can find the center mass for this segmented white part.
MATLAB: Image processing – calculate center mass and its initial moment
center massimage processingImage Processing Toolboximage segmentation
Related Solutions
Basically you said you've already thresholded your image to create the binary image. so now you just call regionprops():
% Threshold this frame of the video.
binaryImage = grayImage > someThreshold;binaryImage = bwareafilt(binaryImage, 1); % Take the largest blob ONLY to get rid of noise.
props = regionprops(binaryImage, 'Centroid');% Now if you're on frame #k of the video
xCentroid(k) = props.Centroid(1);yCentroid(k) = props.Centroid(2);
The key to getting the centroid of all the blobs together instead of getting each individually is to make a labeled image where the image is floating point, not binary. If it's binary, regionprops() will internally label each connected component and report the centroids of each component individually, which you said you don't want when you said "center of mass of the black circles" (at least that's how I'm interpreting it).
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 IMAGE
folder = pwd;baseFileName = 'image.png';% 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; endendgrayImage = 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.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage); % ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end% Display the image.
subplot(2, 2, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');impixelinfo;hFig = gcf;hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;%--------------------------------------------------------------------------------------------------------% EXTRACTION OF IMAGE
% Get a binary image
binaryImage = grayImage < 128;% Get rid of the surround.
binaryImage = imclearborder(binaryImage);subplot(2, 2, 2);imshow(grayImage, []);impixelinfo;title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');impixelinfo;% Fill holes
binaryImage = imfill(binaryImage, 'holes');% Display the image.subplot(2, 2, 3);imshow(binaryImage, []);title('Black Blobs', 'FontSize', fontSize, 'Interpreter', 'None');axis('on', 'image');% Find the center of mass, which is the same as the centroid since all we have is a binary image.
% Casting the binary image to double is the KEY to making sure we get just one
% centroid rather than a centroid for each and every blob individually.
labeledImage = double(binaryImage);props = regionprops(labeledImage, 'Centroid')xCentroid = props.Centroid(1);yCentroid = props.Centroid(2);% Plot a red crosshairs there
hold on;plot(xCentroid, yCentroid, 'r+', 'MarkerSize', 200, 'LineWidth', 3);caption = sprintf('Centroid of black blobs at (%.2f, %.2f)', xCentroid, yCentroid);title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
If you actually want all the individual centroids, you can do
labeledImage = double(binaryImage);
but you said you already did that and have the individual centroids already.
Best Answer