使用imrotate旋转图像但将图像外部的像素设置为非零

时间:2015-04-30 17:36:24

标签: image matlab image-processing rotation

我正在使用离散小波变换进行图像水印的项目。我已经获得了水印图像,现在我用旋转等东西攻击图像。当我将函数imrotate应用于带水印的图像时,未包括在旋转图像中的所有像素变为零,因为它是imrotate的属性。因此,我在原始图像和旋转的水印图像之间获得了非常低的PSNR。

我的问题是这个。我想旋转图像,但旋转后我还需要具有较高的PSNR值。是否有任何方法在旋转后,我能够保留旋转图像中与原始图像的像素类似的全部或大部分像素?

2 个答案:

答案 0 :(得分:2)

这是imrotate的后果。旋转图像时,图像将填充零,以显示未包含在结果中的像素。但是,我可以建议的一件事是,您可以填写任何未包含在常量值中的像素....就像图像的平均强度一样,前提是您的图像是灰度的。

我在这篇文章中做了类似的事情:Convert angle quantitative data into qualitative images

我得到了一个基本图像,我不得不在给定角度列表的情况下旋转图像。当我旋转图像时,我不得不用白色像素填充背景。

因此,一种好方法是提供与原始图像大小相同的全部logical true的图像。您旋转此图像,以便旋转的图像具有false像素,表示这些像素不包含在旋转中,true像素表示包含这些像素。您将反转此蒙版,以便找到最终图像中未包含在旋转结果中的所有像素。您可以使用此蒙版索引旋转的图像,并用平均强度替换零像素。但是,因为您在不同图像之间找到PSNR,所以您可能希望旋转的图像具有与原始图像相同的尺寸,因此请确保在旋转时指定'crop'标记。不幸的是,这会在旋转后删除一些像素,但这是为了使图像的尺寸与原始图像保持一致。

因此,如果您的图片存储在A中,并希望将图像旋转角度theta,请执行此类操作:

%// Define mask
mask = true(size(A));

%// Rotate original image
B = imrotate(A, theta, 'crop');

%// Rotate the mask and invert
maskR = ~imrotate(mask, theta, 'crop');

%// Find mean intensity of original image
meanI = mean(A(:));

%// Assign values in rotated result that are not included
%// with mean intensity
B(maskR) = meanI;

%// Show the images
figure; 
subplot(1,3,1); imshow(A); title('Original Image');
subplot(1,3,2); imshow(maskR); title('Rotated Mask');
subplot(1,3,3); imshow(B); title('Rotated Image');

让我们测试一下。我将使用作为MATLAB图像处理工具箱一部分的cameraman.tif图像。这就是它的样子:

enter image description here

因此,逆时针定义旋转角度为30度:

A = imread('cameraman.tif');
theta = 30;

结果如下:

enter image description here

答案 1 :(得分:0)

在MATLAB中旋转图像并更改背景颜色

这是解决方案!

the final image result is here

    clc; clear;
    img=imread('cameraman.tif');

    padColor=128; %//the color you want for background

    %//one pixel padding for highlitghing the borders of image
    img=padarray(img,[1 1],padColor);


    rotated_img=imrotate(img,30); %//rotation

    [m,n]=size(rotated_img);%size of rotated image

    %//in two for-loops, coloring the rotated image until we see the border(specified earlier)
    for j=1:m                  
        for k=1:n
            if rotated_img(j,k)~= padColor
                rotated_img(j,k)=padColor;
            else
                break;
            end
        end
    end
    for j=m:-1:1
        for k=n:-1:1
            if rotated_img(j,k)~= padColor
                rotated_img(j,k)=padColor;
            else
                break;
            end
        end
    end


    xmin=2; ymin=2;
    width=m-1; height=n-1; 
    %//cropping the one-pixel padding that we had added before
    rotated_img=imcrop(rotated_img,[xmin ymin width height]);

    imshow(rotated_img);