我会用一张图来解释,这样你可以更好地理解,如下所示:
有了上面的结果,一切看起来都很正常。我有一些图像,我尝试使用 imrotate
旋转该图像。在这种情况下,我使用逆时针旋转 45 度。然而,当我尝试反向旋转时(我的目的是在不旋转的情况下获得图像的正常位置),一切都变得很奇怪 - 特别是在图像分辨率方面。如上图所示,由于该图片中有很多噪声,图像分辨率会降低并且图像大小会发生变化。
附加信息
真实图像信息:
- 宽度:512
- 高度:384
裁剪图像信息:
- 宽度:513
- 高度:385
我的问题如下 - 如何获得像原始图像一样干净漂亮的图像?
我写的代码如下所示:
clc;
close all; % Close all figure windows except those created by imtool.
imtool close all;
clearvars; % Get rid of variables from prior run of this m-file.
workspace; % Make sure the workspace panel is showing.
Rotation=45;
%Read the image
imagepad = imread('peppers.png');
%% Image Source Information
info1=size(imagepad);
fprintf('Real Image Information : \n'); % Message sent to command window.
fprintf(' Width : %d\n',info1(2)); % Message sent to command window.
fprintf(' Height : %d\n',info1(1)); % Message sent to command window.
fprintf('------------------------');
%%Try Rotate 45 degree
imRotate = imrotate(imagepad,Rotation);
%%
figure;
subplot(121);
imshow(imagepad);
caption = sprintf('Real Image');
title(caption, 'FontSize', 13);
subplot(122);
imshow(imRotate);
caption = sprintf('Rotate 45 degree result');
title(caption, 'FontSize', 13);
%%try to return the image position
imReturn = imrotate(imRotate,-Rotation);
%%Try to return the size of image
%% find pixel
%im = imread('im.png'); %# load image
[y,x] = find(all(imReturn>0, 3)); %# find black pixels
position = [x,y]; %# display them
[x1]=min(position);
[x2]=max(position);
%%Normal Size
Im2 = imcrop(imReturn,[x1(1) x1(2) (x2(1)-x1(1)) (x2(2)-x1(2))]);
figure, imshow(Im2);
caption = sprintf('Last Result image size');
title(caption, 'FontSize', 13);
%%
figure;
subplot(221);
imshow(imagepad);
caption = sprintf('Real Image');
title(caption, 'FontSize', 13);
subplot(222);
imshow(imRotate);
caption = sprintf('Rotate 45 degree result');
title(caption, 'FontSize', 13);
subplot(223);
imshow(imReturn);
caption = sprintf('Inverse Rotate 45 degree result');
title(caption, 'FontSize', 13);
subplot(224);
imshow(Im2);
caption = sprintf('Crop result');
title(caption, 'FontSize', 13);
%%Different Resolution
subplot(121);
imshow(imagepad);
caption = sprintf('Real Image');
title(caption, 'FontSize', 13);
subplot(122);
imshow(Im2);
caption = sprintf('Crop result');
title(caption, 'FontSize', 13);
%% Image Source Information
info2=size(Im2);
fprintf('Return Image Information : \n'); % Message sent to command window.
fprintf(' Width : %d\n',info2(2)); % Message sent to command window.
fprintf(' Height : %d\n',info2(1)); % Message sent to command window.
fprintf('------------------------');
最佳答案
我不确定图像大小有什么问题。旋转图像时,由于图像的角超出图像尺寸,您必须填充图像以适应旋转。使用旋转后的图像将图像旋转回来后,图像尺寸将保持不变,这就是为什么您需要裁剪图像以获得最终结果。
但是,您遇到了锯齿状噪声错误,因为默认插值算法是最近邻算法。这意味着当您旋转图像时,空洞会被附近邻居的像素填充。这会导致“噪声”,因为孔洞理论上应该由原始图像中的分数位置填充,因此这将促进离开和进入孔洞的平滑过渡。因此,您必须为 imrotate
选择不同的插值算法。改用双线性或双三次插值。
因此,您需要更改 imrotate
调用以使用不同的插值算法。我将在这里使用双三次。第一次旋转时,将代码更改为:
%%//Try Rotate 45 degree
imRotate = imrotate(imagepad,Rotation,'bicubic');
同样当你向后旋转时:
%%//try to return the image position
imReturn = imrotate(imRotate,-Rotation,'bicubic');
当我现在运行你的代码时,我在比较原始图像和旋转的前后图像时得到了这个:
由于插值算法,您会看到没有任何“锯齿状边缘”。虽然涉及一些平滑,但这是双三次插值算法的结果。
关于image - 在 MATLAB 中反转旋转图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36660400/