我正在尝试使用Matlab旋转图像而不使用旋转功能.我实际上是使用变换矩阵制作的。但这还不够好。问题是旋转后的图像"滑动"。让我用图片告诉你。
这是我要旋转的图像:
但是当我旋转它(例如45度)时,它变为:
我在问为什么会这样。这是我的代码,是否存在任何数学或编程错误?
image=torso;
%image padding
[Rows, Cols] = size(image);
Diagonal = sqrt(Rows^2 + Cols^2);
RowPad = ceil(Diagonal - Rows) + 2;
ColPad = ceil(Diagonal - Cols) + 2;
imagepad = zeros(Rows+RowPad, Cols+ColPad);
imagepad(ceil(RowPad/2):(ceil(RowPad/2)+Rows-1),ceil(ColPad/2):(ceil(ColPad/2)+Cols-1)) = image;
degree=45;
%midpoints
midx=ceil((size(imagepad,1)+1)/2);
midy=ceil((size(imagepad,2)+1)/2);
imagerot=zeros(size(imagepad));
%rotation
for i=1:size(imagepad,1)
for j=1:size(imagepad,2)
x=(i-midx)*cos(degree)-(j-midy)*sin(degree);
y=(i-midx)*sin(degree)+(j-midy)*cos(degree);
x=round(x)+midx;
y=round(y)+midy;
if (x>=1 && y>=1)
imagerot(x,y)=imagepad(i,j); % k degrees rotated image
end
end
end
figure,imagesc(imagerot);
colormap(gray(256));
最新回答
- 1月前1 #
- 1月前2 #
您使用的方法(通过采样旋转)最快,最简单,但最不准确.
通过区域映射旋转,如下所示(这是很好的参考),在保留颜色方面要好得多.
但是:请注意,这仅适用于灰度/ RGB图像,而不适用于颜色映射的图像,例如您似乎正在使用的图像.
image = imread('peppers.png'); figure(1), clf, hold on subplot(1,2,1) imshow(image); degree = 45; switch mod(degree, 360) % Special cases case 0 imagerot = image; case 90 imagerot = rot90(image); case 180 imagerot = image(end:-1:1, end:-1:1); case 270 imagerot = rot90(image(end:-1:1, end:-1:1)); % General rotations otherwise % Convert to radians and create transformation matrix a = degree*pi/180; R = [+cos(a) +sin(a); -sin(a) +cos(a)]; % Figure out the size of the transformed image [m,n,p] = size(image); dest = round( [1 1; 1 n; m 1; m n]*R ); dest = bsxfun(@minus, dest, min(dest)) + 1; imagerot = zeros([max(dest) p],class(image)); % Map all pixels of the transformed image to the original image for ii = 1:size(imagerot,1) for jj = 1:size(imagerot,2) source = ([ii jj]-dest(1,:))*R.'; if all(source >= 1) && all(source <= [m n]) % Get all 4 surrounding pixels C = ceil(source); F = floor(source); % Compute the relative areas A = [... ((C(2)-source(2))*(C(1)-source(1))),... ((source(2)-F(2))*(source(1)-F(1))); ((C(2)-source(2))*(source(1)-F(1))),... ((source(2)-F(2))*(C(1)-source(1)))]; % Extract colors and re-scale them relative to area cols = bsxfun(@times, A, double(image(F(1):C(1),F(2):C(2),:))); % Assign imagerot(ii,jj,:) = sum(sum(cols),2); end end end end subplot(1,2,2) imshow(imagerot);
输出:
- 1月前3 #
根据用户指定的角度旋转彩色图像 在Matlab中没有任何图像裁剪。
该程序的输出类似于内置命令" imrotate"的输出。该程序根据用户给定的角度输入动态创建背景。通过使用旋转矩阵和原点平移,获得初始图像和最终图像的坐标之间的关系.利用初始图像和最终图像的坐标之间的关系,我们现在映射每个像素的强度值.
img=imread('img.jpg'); [rowsi,colsi,z]= size(img); angle=45; rads=2*pi*angle/360; %calculating array dimesions such that rotated image gets fit in it exactly. % we are using absolute so that we get positve value in any case ie.,any quadrant. rowsf=ceil(rowsi*abs(cos(rads))+colsi*abs(sin(rads))); colsf=ceil(rowsi*abs(sin(rads))+colsi*abs(cos(rads))); % define an array withcalculated dimensionsand fill the array with zeros ie.,black C=uint8(zeros([rowsf colsf 3 ])); %calculating center of original and final image xo=ceil(rowsi/2); yo=ceil(colsi/2); midx=ceil((size(C,1))/2); midy=ceil((size(C,2))/2); % in this loop we calculate corresponding coordinates of pixel of A % for each pixel of C, and its intensity will be assigned after checking % weather it lie in the bound of A (original image) for i=1:size(C,1) for j=1:size(C,2) x= (i-midx)*cos(rads)+(j-midy)*sin(rads); y= -(i-midx)*sin(rads)+(j-midy)*cos(rads); x=round(x)+xo; y=round(y)+yo; if (x>=1 && y>=1 && x<=size(img,1) && y<=size(img,2) ) C(i,j,:)=img(x,y,:); end end end imshow(C);
- 1月前4 #
检查一下。
这是最快的方法。
img = imread('Koala.jpg'); theta = pi/10; rmat = [ cos(theta) sin(theta) 0 -sin(theta) cos(theta) 0 0 0 1]; mx = size(img,2); my = size(img,1); corners = [ 0 0 1 mx 0 1 0 my 1 mx my 1]; new_c = corners*rmat; T = maketform('affine', rmat); %# represents translation img2 = imtransform(img, T, ... 'XData',[min(new_c(:,1)) max(new_c(:,1))],... 'YData',[min(new_c(:,2)) max(new_c(:,2))]); subplot(121), imshow(img); subplot(122), imshow(img2);
相关问题
- matlab:使用双线性插值调整图像大小,而无需调整大小imagematlabimageprocessinginterpolation2021-01-09 00:25
- 如何在MATLAB中将图像划分为块?matlabimageprocessing2021-01-08 01:25
- matlab:OCR的字符重建和填充matlabimageprocessingocredgedetection2021-01-07 17:54
- arrays:我在将MATLAB代码转换为Python时遇到问题pythonarraysmatlabimageprocessing2020-12-29 03:56
- matlab:对齐已捕获的RGB和深度图像matlabimageprocessingkinectimageregistration2020-12-27 03:55
图像中有孔的原因是因为您正在计算
imagerot
中的位置imagepad
中每个像素的数量 .您需要以其他方式进行计算.也就是说,对于imagerot
中的每个像素 在imagepad
中插值 .为此,您只需要应用逆变换,就旋转矩阵而言,它只是矩阵的转置(只需更改每个sin
上的符号 然后进行另一种翻译)。imagerot
中的像素过多 :还请注意,您的
midx
和midy
需要用size(imagepad,2)
计算 和size(imagepad,1)
分别是因为第一维是行数(高度),第二维是宽度。注意:当您决定采用除最近邻点以外的插值方案时,可以采用相同的方法,如Rody的线性插值示例。
EDIT :我假设您出于说明目的使用循环,但是实际上不需要循环.这是一个最近邻插值(您正在使用的示例)的示例,它保持相同大小的图像,但是您可以对其进行修改以生成包括整个源图像的更大图像:
要将以上内容修改为线性插值,请计算
XYt
中每个坐标的4个相邻像素 并使用分数成分积作为权重执行加权总和.我将其保留为练习,因为它只会使我的答案超出您问题的范围. :)