Digital image processing.

조회 수: 11 (최근 30일)
OLUFEMI SONEYE
OLUFEMI SONEYE 2019년 11월 20일
답변: DGM 2024년 12월 12일 19:55
good afternoon community, how can i rotate an image and still retain the same resolution and size of the original image after rotation? Thank you

답변 (2개)

anjali potnis
anjali potnis 2024년 12월 12일 1:52
please use imrotate function available in matlab

DGM
DGM 2024년 12월 12일 19:55
It's not clear what is expected by rotating an image without changing its size or resolution. What does "size" mean in this context? Likewise, if no scaling operation occurs, then resolution doesn't change. If that's a problem, then I suspect that "resolution" means something other than the obvious.
IPT imrotate() can be used to do zero-padded rotations. You can specify the output geometry:
% the inputs:
inpict = imread('peppers.png'); % 384x512
theta = 30;
% rotate using defaults:
% the full input area is retained,
% but rotation means the output must be larger.
op1 = imrotate(inpict,theta); % 589x636, padded
imshow(op1)
% use the tight bounding box option
% in this case, input geometry and output geometry are the same
% but part of the original image is necessarily lost
op2 = imrotate(inpict,theta,'crop'); % 384x512, padded & cropped
imshow(op2)
If "resolution" is simply meant to imply some subjective measure of quality, the default interpolation method used by imrotate() is nearest-neighbor. It can produce jagged edges. If you want to avoid that, you can specify the interpolation method. If you want the background to be something other than black, you have a number of options, including using imwarp().
Otherwise, we're again left uncertain of expectations.
% otherwise, you'd have to explain what you expect when you say
% that you want to rotate an image without changing its geometry
op3 = imannrotate(inpict,theta,'keepcenter'); % 384x512, no padding or cropping (MIMT ONLY)
imshow(op3)
Of course I'm being facetious, but it's no more absurd than donning our noble web-archaeologist hats in order to read tea leaves. If "size" means "page geometry", then (in this example) the output frame must stay 384x512. If no scaling occurs, then resolution never changes -- so that suggests that "resolution" doesn't mean spatial resolution. It probably expresses some desire to avoid image degradation or loss of content.
MIMT imannrotate() has no practical utility, but in this case it does give us an example. It's literally an answer to the question "how do I rotate an image without changing its geometry or doing spatial interpolation?". As in so many cases of bending the rules of the assignment, the slack dimension in this affair ends up being "rotate" instead of "size" or "resolution".
Aside from my earnest effort to misinterpret a suggestive objective, rotating an image without changing its "size" or its "resolution" is (within these assumptions) only possible for integer multiples of 180 degrees.
k = 2.6; % uh some sort of awkward control parameter???
op4 = rot90(inpict,round(k)*2); % 384x512
imshow(op4)
It's worth noting here that imrotate() internally uses rot90() for angles which are integer-divisible by 90 degrees, so explicitly calling rot90() like this really isn't necessary. It's also worth noting that in older versions, rot90() did not accept mutipage inputs.

Community Treasure Hunt

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

Start Hunting!

Translated by