MATLAB: Compute area of bounding box/convex hull

bounding box/convex hull

I'd like to extract a minimal bounding box surrounding the letters first, then compute the area of the bounding box. My code is as follows and I got allAreas [15510,7,6] and allConvexAreas [15510,7,6]. Is my code correct for finding bounding box? So are numbers 7 and 6 the width and height of the bounding box? How can I extract the figure of the bounding box? So the area should be 7×6 = 42?
filename = 'Box.jpg'; A = importdata(filename); image(A);
% convert to binary image bw = imread('Box.jpg'); bw=im2bw(bw); imshow(bw) L = bwlabel(bw); Box = regionprops(L, 'Area', 'BoundingBox'); Box(2);
C = regionprops(L, 'Area', 'ConvexHull'); C(1);
% Compute the areas of bounding box allAreas = [Box.Area];
% Compute the areas of Convex Hull allConvexAreas = [C.Area];

Best Answer

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'image.png';
% Get the full filename, with path prepended.
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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
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(grayImage);
% 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 = grayImage(:, :, 1); % Take red channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo;
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.



%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
drawnow;
% Turn into a binary image.
binaryImage = grayImage < 200;
% Find all the blobs.
[labeledImage, numberOfRegions] = bwlabel(binaryImage);
fprintf('Found %d regions.\n',numberOfRegions);
% Display the binary image.

subplot(2, 2, 2);
imshow(binaryImage, []);
caption = sprintf('Binary Image with %d Blobs', numberOfRegions);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo;
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(2, 2, 3);
imshow(coloredLabels);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
caption = sprintf('Pseudo colored labels, from label2rgb().\nBlobs are numbered from top to bottom, then from left to right.');
title(caption, 'FontSize', fontSize);
% Find bounding boxes
props = regionprops(labeledImage, 'BoundingBox', 'Area');
% Place bounding boxes over all letters.
% Display the binary image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Binary Image with Bounding Boxes', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo;
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
hold on;
for k = 1 : length(props)
thisBB = props(k).BoundingBox;
fprintf('Blob #%d has a blob area of %.1f and BoundingBox area of %.1f',...
props(k).Area, thisBB(3)*thisBB(4));
rectangle('Position', thisBB, 'EdgeColor', 'r', 'LineWidth', 3);
end
I know it doesn't split apart joined letters correctly so you might look into deep learning, or papers on handwriting analysis listed here: Vision Bibliography