How can I solve this centroid related problem ?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2019-4-28
Haven't we already covered this hand-splitting thing in some of your other 48 posts?
采纳的回答
Image Analyst
2019-4-28
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/217000/0001%20Screenshot.png)
11 个评论
Image Analyst
2019-7-22
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!