gpt4 book ai didi

image - 使用 MATLAB imrotate 平铺图像

转载 作者:行者123 更新时间:2023-12-01 06:27:11 30 4
gpt4 key购买 nike

当我使用 MATLAB 的 imrotate 函数时,输出图像缺少部分,并用零填充。

我可以以某种方式使角上的三角形(即缺失的部分)充满图像的相对边缘吗?所以这就像我将图像与周围的 8 个相邻图像平铺,然后旋转并裁剪更大的图像?

感谢您的帮助,

最佳答案

我认为最好的(也是最有效的内存效率)方法是使用 interp2在新的像素中心对原始图像进行采样(原始像素中心“旋转”所需角度的相反)。然后使用mod在这些样本点上,以确保它们落在原始图像的尺寸内。 mod 的额外好处是,超出范围的新 x,y 坐标只需“环绕”到图像的另一侧。

% Load some sample image
load mri
im = double(D(:,:,12));
[rows, cols] = size(im);

% The original pixel centers
[xx,yy] = meshgrid(1:cols, 1:rows);

% Setup the rotation matrix
theta = pi/4;
R = [cos(-theta), -sin(-theta);
sin(-theta), cos(-theta)];

% Center of Rotation (center of the image)
center = [cols/2, rows/2];

% Determine the new pixel centers (rotated)
xy = bsxfun(@minus, [xx(:), yy(:)], center) * R;
xy = bsxfun(@plus, xy, center);

% Mod these using the dimensions of the image to "wrap" around
x = mod(xy(:,1) - 1, cols - 1) + 1;
y = mod(xy(:,2) - 1, rows - 1) + 1;

% Sample the original image at the new pixel centers
im2 = interp2(xx, yy, im, x, y);
im2 = reshape(im2, [rows, cols])

旋转 = 45 度

enter image description here

这适用于任意纵横比(下面的图像演示了 @BlackAdder 提出的问题,其中由于图像又高又窄,repmat [3,3] 不起作用)。

旋转 = 90 度

enter image description here

这还有一个额外的好处,即它不依赖于图像处理工具箱。

关于image - 使用 MATLAB imrotate 平铺图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35126181/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com