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

7 views (last 30 days)
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);
imshow(rgbImage);
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);
imshow(thisMask);
title('Latest Mask', 'FontSize', fontSize);
subplot(2, 2, 3);
imshow(mask);
title('Cumulative Mask', 'FontSize', fontSize);
end
% 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);
imshow(maskedRgbImage);
title('Masked RGB Image', 'FontSize', fontSize);

Answers (2)

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');
imshow(im);
h = drawrectangle('Rotatable',true);

Richard
Richard on 8 Feb 2022
Edited: Richard on 9 Feb 2022
Hello Mei, I have also tried this from ROI object (more specific by function drawrectangle), but the problem was that position of ROI object was not aligned properly, and the resulting crop was not rotated as my drawn rectangle. So I come up with solution, which is quite satisfying:
%% Read image
img=imread('Mobile_Photo.jpg');
imshow(img);
%% Draw, move or rotate rectangle ROI
roi=drawrectangle('Rotatable',true);
%% ... or set exact position
roi.Position=[2652.30984962457,23.7071503674254,42.2737770387466,1546.61976273993];
roi.RotationAngle=3.187093117235868;
%% Crop image
BW = createMask(roi);
BW=imrotate(BW,90-roi.RotationAngle);
rotimg=imrotate(img,90-roi.RotationAngle,'bilinear');
stats = regionprops(BW,'BoundingBox');
cropimg = imcrop(rotimg,stats.BoundingBox);
imshow(cropimg);
This works pretty well, and I have used it also with dynamically changing plot on moving ROI coordinates. The 90 degrees can be of course changed to your precise need. I hope it helps. Best Regards. Richard

Community Treasure Hunt

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

Start Hunting!