Clear Filters
Clear Filters

How can I solve this centroid related problem ?

3 views (last 30 days)
I have a binary image. I want to consider the upper portion of the centroid only. Then for a particular length I want to consider two square from both sides of centroid . I have attached two pictures to give an idea.
Image Analyst
Image Analyst on 28 Apr 2019
Haven't we already covered this hand-splitting thing in some of your other 48 posts?
Zara Khan
Zara Khan on 28 Apr 2019
Image Analyst: I think I haven't posted this earlier. May be you haven't not followed what we were discussing about. Well you have a very good reputation sir, I have followed many of yours answered but you often come with rude language's.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 28 Apr 2019
So in my typically rude fashion, I've spent several minutes of my time to develop a turnkey demo specially for you. Adapt as needed. Let us know of any questions.
% Get the name of the image the user wants to use.
baseFileName = 'img.png';
folder = pwd;
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);
fullFileName = baseFileName; % Don't include folder - it will find it because it's on the search path.
% Read in demo image.
binaryImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(binaryImage)
if numberOfColorChannels > 1
binaryImage = rgb2gray(binaryImage);
% Display image.
subplot(2, 3, 2);
imshow(binaryImage, []);
axis('on', 'image');
caption = sprintf('Original image: %d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.15 1 0.85]);
% 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')
% Find the centroid:
props = regionprops(binaryImage, 'Centroid');
% Mark with crosshairs.
hold on;
plot(props.Centroid(1), props.Centroid(2), 'r+', 'MarkerSize', 170, 'LineWidth', 2);
% Find box 80 pixels above and to the left
row1 = props.Centroid(2) - 79;
row2 = props.Centroid(2);
col1 = props.Centroid(1) - 79;
col2 = props.Centroid(1);
upperLeft = binaryImage(row1:row2, col1:col2);
% Get the dimensions of the image.
[rows2, columns2, numberOfColorChannels] = size(upperLeft)
subplot(2, 2, 3);
axis('on', 'image');
caption = sprintf('Upper Left Image: %d rows by %d columns', rows2, columns2);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Find box 80 pixels above and to the right
col1 = props.Centroid(1);
col2 = props.Centroid(1) + 79;
upperRight = binaryImage(row1:row2, col1:col2);
% Get the dimensions of the image.
[rows3, columns3, numberOfColorChannels] = size(upperRight)
subplot(2, 2, 4);
axis('on', 'image');
caption = sprintf('Upper Right Image: %d rows by %d columns', rows3, columns3);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
0001 Screenshot.png
Zara Khan
Zara Khan on 21 Jul 2019
Image Analyst: Thank you for the link. Now what I am getting , same image for lowerleft and lowerright
Image Analyst
Image Analyst on 22 Jul 2019
Show your code. Are you sure you used the updated col1 and col2 like I showed? They're not the same for each side. I think you must have used the col1 and col2 from the left side for both of them. Here's how you should do it:
row1 = props.Centroid(2);
row2 = props.Centroid(2) + 79;
col1 = props.Centroid(1) - 79;
col2 = props.Centroid(1);
lowerLeft = binaryImage(row1:row2, col1:col2);
col1 = props.Centroid(1);
col2 = props.Centroid(1) + 79;
lowerRight = binaryImage(row1:row2, col1:col2);

Sign in to comment.

More Answers (0)


Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!