【问题标题】:Issues with reversing a rotated image in MATLAB在 MATLAB 中反转旋转图像的问题
【发布时间】:2016-04-16 15:25:27
【问题描述】:

我将用一张图片来解释,以便您更好地理解,如下所示:

有了上述结果,一切正常。我有一些图像,我尝试用imrotate 旋转这个图像。在这种情况下,我使用 45 度逆时针旋转。但是,当我尝试反向旋转时(我的目的是在不旋转的情况下获得图像的正常位置),一切都变得奇怪 - 特别是在图像分辨率下。如上图所示,由于该图片中有很多噪点,图像分辨率会降低,并且图像的大小会发生变化。

其他信息

  • 真实图像信息:

    1. 宽度:512
    2. 身高:384
  • 裁剪图像信息:

    1. 宽度:513
    2. 身高: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('------------------------');

【问题讨论】:

    标签: image matlab image-processing rotation


    【解决方案1】:

    我不确定图片大小有什么问题。旋转图像时,由于图像的角超出图像尺寸,因此您必须填充图像以适应旋转。使用旋转后的图像将图像旋转回来后,图像尺寸将保持不变,这就是您需要裁剪图像以获得最终结果的原因。

    但是,您会遇到锯齿状噪声错误,因为默认插值算法是最近邻算法。这意味着当您旋转图像时,这些孔将被附近邻居的像素填充。这会导致“噪声”,因为理论上这些孔应该由原始图像中的分数位置填充,因此这将促进离开和进入孔的平滑过渡。因此,您必须为imrotate 选择不同的插值算法。请改用双线性或双三次插值。

    因此,您需要更改您的 imrotate 调用以使用不同的插值算法。我将在这里使用双三次。当您先旋转时,将代码更改为:

    %%//Try Rotate 45 degree
    imRotate = imrotate(imagepad,Rotation,'bicubic');
    

    当您向后旋转时也是如此:

    %%//try to return the image position
    imReturn = imrotate(imRotate,-Rotation,'bicubic');
    

    当我现在运行你的代码时,我在比较原始图像和旋转的前后图像时得到这个:

    由于插值算法,您会看到没有任何“锯齿状边缘”。虽然涉及一些平滑,但这是双三次插值算法的结果。

    【讨论】:

    • 非常感谢,这个方法效果很好,但是,图像大小仍然存在一些小问题,它们也会改变,这是我最后一次关于大小的结果:真实图像信息:宽度: 512 高度:384 -----> 返回图像信息:宽度:513 高度:385 ------------------------>> 当我尝试放大,图像的每一边都有一个黑色的附加像素,如何消除该像素并获得像真实图像一样的正确图像尺寸
    • 你不能改变的尺寸,因为图像的尺寸是均匀的......这就是自动裁剪的问题。可以通过使行和列为奇数来避免这种大小差异。你无能为力。
    • 您还有其他代码可以自动裁剪以获得更好的结果吗?
    • @SendiNovian 试试这个:stackoverflow.com/questions/25584353/…
    猜你喜欢
    • 2011-05-28
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多