MATLAB Answers

How to get bounding boxes around rectangular rooms which are rotated by some angle ?

6 views (last 30 days)
I have a floorplan which is rotated with some angle. I do not know the angle. Can I get the bounding box around the rooms in floor plan . If it is not rotated, using regionprops(), I am able to get the bounding boxes. But if it is rotated, since the bounding box do not rotates, I am unable to get the bounding box around the rooms. I want to get the coordinates of those rooms. Is there any other way to get the coordinates of the rooms with/without using bounding box method?Or I sit possible to get the coordinates of the 4 line segments of each room which bound the rooms?


Sign in to comment.

Accepted Answer

Matt J
Matt J on 23 Jun 2020
You could also use this FEX file to get the vertices of each room,
though you would have to generate an image of each room separately. You could also use it to find the corners of the convex hull of the floorplan.


Show 7 older comments
Matt J
Matt J on 27 Jun 2020
As a simple example, the attached file contains two box shaped regions
load Boxes
and here is how I woul make a duplicate image with only the smaller box.

Sign in to comment.

More Answers (2)

KSSV on 23 Jun 2020
You can find the required pixel values anf get the points. Check below:
I = imread("image.jpeg") ;
[y,x] = find(I~=255) ;
hold on
% Bounding box
BB = [min(x) min(y) ;max(x) max(y)] ;


Show 2 older comments
Thank you for your information. Could you please tell me is there any way to distingush the points to the room they belong to? I want to have like a set of corner points of individual rooms. Is there any way to achieve that?
I can not have the individual images for rooms. So that's why I am asking.
Image Analyst
Image Analyst on 27 Jun 2020
The bounding box is aligned with the edges of the image. It does not tilt the bounding box to align with the edges of your rooms like in your tilted image.
See my answer if you want the boundaries of the rooms regardless if they're tilted or not.

Sign in to comment.

Image Analyst
Image Analyst on 27 Jun 2020
Here is one way to get the coordinates of the boundary points of each room. Each boundary is shown outlining the room in a different color.
% By Image Analyst
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 long g;
format compact;
fontSize = 22;
folder = pwd;
baseFileName = 'image.jpeg';
% 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);
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.
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% Get a binary image
binaryImage = imbinarize(grayImage);
% Get rid of big white frame around the border of the image.
binaryImage = imclearborder(binaryImage);
subplot(2, 1, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% Get the coordinates around each room
boundaries = bwboundaries(binaryImage);
numRooms = length(boundaries);
% Plot them
roomColors = hsv(numRooms);
hold on;
for k = 1 : numRooms
thisBoundary = boundaries{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
plot(x, y, '-', 'Color', roomColors(k, :), 'LineWidth', 4);


Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by