MATLAB Answers

How to automatic rotate the crop rectangle area (imrect function) like image below?

18 views (last 30 days)
Mei Synn Tan
Mei Synn Tan on 30 Aug 2018
Edited: KSSV on 10 Nov 2020
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 = 15;
% Define the number of ellipses
numberOfEllipses = 1;
% Read in a color demo image:
rgbImage = imread('Windflower.jpg');
% Get the dimensions of the image. numberOfColorChannels should be = 3.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Define a mask that will have all of the ellipses in it.
mask = false(rows, columns);
% Display the color image.
subplot(2, 2, 1);
hold on; % Don't let ellipses we draw on it blow away the image.
% caption = sprintf('Drag out %d ellipses. Double click inside to accept it', numberOfEllipses);
% title(caption, 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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')
% Now ask user to draw the ellipses.
for k = 1 : numberOfEllipses
% Now draw elliptical region.
subplot(2, 2, 1); % Switch drawing axes back to the original one.
% Update instructions.
caption = sprintf('Drag out %d more rectangle. Double click inside to accept it', numberOfEllipses - k + 1);
title(caption, 'FontSize', fontSize);
ellipseObject = imrect(gca);
%ellipseObject = imellipse(gca);
setColor(ellipseObject, 'red');
position = wait(ellipseObject); % This is a list of (x,y) coordinates of the ellipse perimeter.
% Plot the ellipse over the image to keep track of what we drew.
% plot(position(:, 1), position(:, 2), 'r-', 'LineWidth', 2);
% Create a mask (binary image) from what user drew.
thisMask = createMask(ellipseObject);% Get mask for this ROI.
% Combine this mask into our master mask
% that contains all the ellipses by ORing it in.
mask = mask | thisMask;
% Just for optional information, display this mask and the cumulative mask.
subplot(2, 2, 2);
title('Latest Mask', 'FontSize', fontSize);
subplot(2, 2, 3);
title('Cumulative Mask', 'FontSize', fontSize);
% Now use this mask to mask all of these regions
% from the original RGB image.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Now image will be black outside the ellipses.
% If we want the surround to be white, not black, so first make an all white image.
inverseMask = 255 * uint8(~mask); % = white outside ellipse, zero inside ellipse.
% Now mask in white by adding 255.
maskedRgbImage = bsxfun(@plus, maskedRgbImage, cast(inverseMask, 'like', rgbImage));
% Display the final masked image.
subplot(2, 2, 4);
title('Masked RGB Image', 'FontSize', fontSize);

Answers (1)

Tim Jackman
Tim Jackman on 27 Sep 2018
Edited: KSSV on 10 Nov 2020
The new rectangle ROI drawrectangle supports rotation:
For example:
im = imread('windflower.jpg');
h = drawrectangle('Rotatable',true);

Community Treasure Hunt

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

Start Hunting!

Translated by