The largest rectangle inside object

19 views (last 30 days)
Milad
Milad on 23 Nov 2017
Commented: Milad on 25 Nov 2017
How can I get the largest rectangle inside an object with a given center point?
Can it be done by using simple functions in Matlab and without any for s?
  3 Comments
Milad
Milad on 23 Nov 2017
Yes, in the end for will be used. But it's better if I don't write any for s and use the built-in functions because they are faster and nicer.
Image Analyst
Image Analyst on 23 Nov 2017
It's not easy, but I think it would involve bwdist(). Then you have to decide whether the rectangle can be rotated (harder) or parallel with image edges (easier).

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 23 Nov 2017
You might try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'UVvcw.jpg';
% Get the full filename, with path prepended.
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
end
%===============================================================================
% Read in demo image.
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(:, :, 2); % Take green channel.
end
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
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.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')
% Find the white surround.
binaryImage = grayImage > 128;
% Fill the image to get rid of black spot at center.
binaryImage = imfill(binaryImage, 'holes');
% Take the largest blob only.
% binaryImage = bwareafilt(binaryImage, 1);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Take the largest blob only.
edtImage = bwdist(~binaryImage);
% Display the image.
subplot(2, 2, 3);
imshow(edtImage, []);
axis on;
caption = sprintf('Distance Transform Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Find the max of the EDT:
maxDistance = max(edtImage(:));
[rowCenter, colCenter] = find(edtImage == maxDistance)
hold on;
plot(colCenter, rowCenter, 'r+', 'MarkerSize', 30, 'LineWidth', 2);
% Get the boundary of the blob.
boundaries = bwboundaries(binaryImage);
b = boundaries{1}; % Extract from cell.
x = b(:, 2);
y = b(:, 1);
% Get distances from center to each of the edge pixels.
distances = sqrt((x - colCenter).^2 + (y - rowCenter).^2);
% Find the min distance.
[minDistance, indexOfMin] = min(distances)
% Find x and y of the min
xMin = x(indexOfMin)
yMin = y(indexOfMin)
plot(xMin, yMin, 'co', 'MarkerSize', 10, 'LineWidth', 2);
% Get the delta x and delta y from center to corner
dx = abs(colCenter - xMin)
dy = abs(rowCenter - yMin)
% Get edges of rectangle by adding and subtracting deltas from center.
row1 = rowCenter - dy
row2 = rowCenter + dy
col1 = colCenter - dx
col2 = colCenter + dx
% Make a box so we can plot it.
xBox = [col1, col2, col2, col1, col1];
yBox = [row1, row1, row2, row2, row1];
plot(xBox, yBox, 'r-', 'LineWidth', 2);
% Now, for fun, plot it over the binary image.
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image with largest rectangle');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
plot(xBox, yBox, 'r-', 'LineWidth', 2);
  2 Comments
Milad
Milad on 24 Nov 2017
This doesn't work for rectangle shaped objects.
Milad
Milad on 25 Nov 2017
Can you please complete this code?

Sign in to comment.

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!