How can I solve this centroid related problem ?

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

A centroid is a point, so it doesn't really make sense to talk about a portion of a centroid. If you are talking about the region around the centroid it seems you could just calculate the other relevant coordinates.
Take max of distance between centroid x and bounding box x left, bounding box x right and centroid x, bounding box top and centroid y. That gives your distance. Take left that much from centroid x to up that much from centroid y, similar to right side.
upperleftimage=binaryImage(centroidRow - maxlength:centroidRow,centroidColumn - maxlength:centroidColumn);
upperrightimage=binaryImage(centroidRow - maxlength:centroidRow,centroidColumn:box_x_rightCol);
I am getting the proper upperleftimage but not the proper upperrightimage.
regionprops to extract centroid and bounding box. BB is in the form left x then bottom y then width then height. Centroid is x then y.
C(1)-bb(1), bb(1)+bb(3)-C(1), bb(2)+bb(4)-C(2)
max of those.
Warning: you can end up going outside the image with your scheme.
++
++
+++C++++++
++
++
Centroid location is approximate.
Left distance us 3, right is 6, up is 2, you define your box as square and reaching the edges at least so your square has to be 6 so that you reach the right side. But 6 up or 6 left is outside the image.
Its okey if it goes out of the image object ,I mean image background can be part of this. well can you please tell where I am doing wrong when considering the upperrightimage here.
upperleftimage=binaryImage(centroidRow - maxlength:centroidRow,centroidColumn - maxlength:centroidColumn);
upperrightimage=binaryImage(centroidRow - maxlength:centroidRow,centroidColumn:box_x_rightCol);
I forgot to subtract 1 after adding width or height
I have got the maxlength. When considering two particular square the righthalf square its coming into proper square rather its becoming rectangle please check my above code where i am doing the mistake please.
I do not have access to graphics at the moment; I am answering all these questions by mental modeling.
Can you please check this later ?
Water Roberson Have you checked that ?
Zara Khan
Zara Khan on 28 Apr 2019
Edited: Zara Khan on 28 Apr 2019
Have you checked this ? Please check for this image in the attachements.
Haven't we already covered this hand-splitting thing in some of your other 48 posts?
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

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: How can I get bottom left and bottom right in the same way ?
Darova : i am asking about bottom left and bottom right.
How have you tried to modify the code for col1 etc? It shouldn't be too hard for you to find a way to get the other two quadrants.
Just redefine row1 and row2 to be below the centroid:
row1 = props.Centroid(2);
row2 = props.Centroid(2) + 79;
lowerLeft = binaryImage(row1:row2, col1:col2);
and so on.
Zara Khan
Zara Khan on 21 Jul 2019
Edited: Zara Khan on 21 Jul 2019
I am not able to do this. Dealing with image quadrants are getting complex for me. Can you please help to find this ?
If that's too complicated for you, then the rest of the gesture analysis will be way, way over your head and I suggest you hire someone to do it for you. If you get your confidence up, and want to take another shot at it, I'll give you the last of the 4 quadrants:
col1 = props.Centroid(1);
col2 = props.Centroid(1) + 79;
lowerRight = binaryImage(row1:row2, col1:col2);
but the coding after that will be very much more difficult than this.
Image Analyst: yaa that is absolutely true. But there must be some way to learn all these things.
You can start with this link.
They'll teach you about indexing there and lots of other fundamentals. You should also look over the FAQ for some other, maybe more advanced, things to do.
Image Analyst: Thank you for the link. Now what I am getting , same image for lowerleft and lowerright
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)

Categories

Find more on Convert Image Type 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!