1

Based on this question Width and height of a rotated polyshape object - Matlab I was running the cool code. It can extract the height and width of the bounding box but how can I get the “real” object maximum height and width (which is not always the bounding box like in the image attached)?

Code:

clc;
clear all;
 
Image =  imread('E:/moon.gif');
BW = imbinarize(Image);
BW = imfill(BW,'holes');
BW = bwareaopen(BW, 100);
 
stat = regionprops(BW,'ConvexHull','MinFeretProperties');
 
% Compute Feret diameter perpendicular to the minimum diameter
for ii=1:numel(stat)
    phi = stat(ii).MinFeretAngle; % in degrees
    p = stat(ii).ConvexHull * [cosd(phi),-sind(phi); sind(phi),cosd(phi)];
    minRect = max(p) - min(p); % this is the size (width and height) of the minimal bounding box
    stat(ii).MinPerpFeretDiameter = minRect(2); % add height to the measurement structure
    width = minRect(1)
    height = minRect(2)
end

enter image description here

hs100
  • 486
  • 6
  • 20
  • 1
    Please show what you mean by “real” height and width. Why are the minimal Feret diameter and its perpendicular Feret diameter not suitable? – Cris Luengo Aug 12 '20 at 01:17
  • The image results using the script is width = 128.0944 and height = 225.6806. If I am not mistaken these are the bounding box dimensions. What I mean by “real width and height" for example this case is the width of the white pixels (it is not half of the height). – hs100 Aug 12 '20 at 08:51
  • So you want to straighten out the object and then get the width and height, is that it? That is not nearly as simple, it gets complicated really quickly: what if your shape has branches, for example shaped like a starfish? – Cris Luengo Aug 12 '20 at 13:19
  • You are right but I did not think about branched object. How can I do it for this kind of object? – hs100 Aug 13 '20 at 11:41
  • You’d have to reduce the object to a line (see medial axis, skeleton, and thinning), then measure the length of the line as well as the distance of the line to the edges of the original object. It is not easy to do this right. The skeleton will work well as long as there are no branches. – Cris Luengo Aug 13 '20 at 13:49

1 Answers1

-3

You can try this code:-

clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
workspace;  % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;

%===============================================================================
% Get the name of the first image the user wants to use.
baseFileName = 'A.jpg';
folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions).
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
end

%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');

% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.

[rows, columns, numberOfColorChannels] = size(rgbImage)
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(rgbImage);
  % 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 = rgbImage(:, :, 1); % Take red channel.
end

% Display the image.
subplot(2, 2, 2);
imshow(grayImage, []);
axis on;
caption = sprintf('Gray Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;

% Display the histogram of the image.
subplot(2, 2, 3);
histogram(grayImage, 256);
caption = sprintf('Histogram of Gray Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
grid on;
drawnow;

%=======================================================================================
binaryImage = grayImage < 150;
% Keep only the largest blob.
binaryImage = bwareafilt(binaryImage, 1);
% Display the masked image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;

% Get the bounding box of the blob.
props = regionprops(binaryImage, 'BoundingBox');
boundingBox = [props.BoundingBox]

% Display the original image.
subplot(2, 2, 4);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Plot the bounding box over it.
hold on;
rectangle('Position', boundingBox, 'LineWidth', 2, 'EdgeColor', 'r')
RAHIL SHA
  • 5
  • 2