【问题标题】:Rotating an image using imrotate but setting pixels outside of the image to non-zero使用 imrotate 旋转图像,但将图像外部的像素设置为非零
【发布时间】:2015-04-30 17:36:24
【问题描述】:

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

我的问题是这样的。我想旋转图像,但旋转后我还需要有较高的 PSNR 值。有没有什么方法可以在旋转后保留旋转图像中与原始图像像素相似的所有或大部分像素?

【问题讨论】:

  • 我假设您在谈论图像外部的边界值,因为那里没有信息,所以它变为零。你想拥有什么?如果你不想要这些,你总是可以指定'crop'参数来消除那些外部区域,但你的图像会变小。
  • @ManpreetSingh - 我的回答对你有帮助吗?
  • @ManpreetSingh - ECHO!!!

标签: image matlab image-processing rotation


【解决方案1】:

这是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');

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

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

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

结果如下:


【讨论】:

    【解决方案2】:

    在 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);
    

    【讨论】:

      猜你喜欢
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2021-11-20
      • 2022-01-21
      • 1970-01-01
      • 2012-07-31
      相关资源
      最近更新 更多