How can I solve this centroid related problem ?

49 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.
  14 Comments
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);
uiwait(warndlg(errorMessage));
return;
end
fullFileName = baseFileName; % Don't include folder - it will find it because it's on the search path.
end
%=======================================================================================
% Read in demo image.
binaryImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(binaryImage)
if numberOfColorChannels > 1
binaryImage = rgb2gray(binaryImage);
end
% Display image.
subplot(2, 3, 2);
imshow(binaryImage, []);
impixelinfo;
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')
drawnow;
% 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);
imshow(upperLeft);
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);
imshow(upperRight);
axis('on', 'image');
caption = sprintf('Upper Right Image: %d rows by %d columns', rows3, columns3);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
0001 Screenshot.png
  11 Comments
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)

Community Treasure Hunt

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

Start Hunting!