我正在尝試使用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个相邻畫素 並使用分數成分积作為權重執行加權总和.我將其保留為练习,因為它只会使我的答案超出您問题的範圍. :)