MATLAB,編寫程式,將影象旋轉45度(急!)

時間 2022-03-24 17:40:22

1樓:山水阿銳

您好,如果按照你的程式的話,根據提示內容,在計算

i1=round(i*cos(a) - j*sin(a)+ n * sin(a))+1;

j1=round(i*sin(a) + j*cos(a));

這兩個時會出現零值,那麼,在matlab中索引f矩陣就是錯誤的了,你可以設定斷點,然後單步執行一下看看在哪一步出現的零值,你根據旋轉矩陣的計算應該是沒有問題的,關鍵是灰度對映時可能出現零位置,最好在其中加入判斷的語句,j1計算會出現0值。

你這樣寫,是沿座標軸原點旋轉,你說的我原來寫的那個是沿固定點旋轉,實現都是使用旋轉矩陣,沿固定點只是先平移到原點,然後再平移回去,你可以看看,具體的我已經記不太清楚了,呵呵,已經不做影象了。

我寫的那個的程式:

function im_final = imrotate_my(im_path,theta,options)

% im_rotate 兩維影象旋轉以及雙線性灰度插值演算法的實現

% im_path 影象儲存路徑

% theta 旋轉角度,正數表示順時針旋轉

% options 可以為circular(超出範圍部分,按照週期形式擴充套件)

% crop(超出部分置零,即全黑)

% ref. 章毓晉. 影象工程(上冊)——影象處理. 清華大學出版社

% author: lskyp date: 2009.08.12

% version: v1.2 original version: v1.0 im_bilinear.m;im_rotate.m

% with the parameter options added

error(nargchk(2,3,nargin,'string'))

if nargin == 2

options = 'circular';

else

if ~ (strcmp(options,'circular') || strcmp(options,'crop'))

error('錯誤的輸出方法')

endend

im_init = imread(im_path);

im_init = double(im_init);

im_height = size(im_init,1);

im_width = size(im_init,2);

% 分別處理灰度影象和rgb影象

if ndims(im_init) == 3

im_final = zeros(im_height,im_width,3);

r = im_init(:,:,1);

g = im_init(:,:,2);

b = im_init(:,:,3);

r_final = im_final(:,:,1);

g_final = im_final(:,:,2);

b_final = im_final(:,:,3);

else

im_final = zeros(im_height,im_width);

endrot_matrix = [cos(theta) -sin(theta);sin(theta) cos(theta)];

orig_h = (im_height + 1)/2;

orig_w = (im_width + 1)/2;

for h = 1:im_height

for w = 1:im_width

% 平移至原點,旋轉,然後再平移回去

new_position = rot_matrix*[h - orig_h;w - orig_w] + [orig_h;orig_w];

% 超出範圍按週期擴充套件控制,options引數控制

if strcmp(options,'circular')

new_position(1) = mod(new_position(1),im_height);

new_position(2) = mod(new_position(2),im_width);

if new_position(1) == 0

new_position(1) = im_height;

endif new_position(2) == 0

new_position(2) = im_width;

endend

% 如果新位置為整數,那麼直接賦予灰度值或者rgb值,否則,按照雙線性插值計算。

% 使用後向對映

if new_position == round(new_position)

if new_position(1) == 0

new_position(1) = 1;

endif new_position(2) == 0

new_position(2) = 1;

end% 超出範圍控制,options為crop選項,超出範圍置零

if strcmp(options,'crop') && (new_position(1) >= im_height || ...

new_position(2) >= im_width || new_position(1) < 0 || ...

new_position(2) < 0)

if ndims(im_init) == 3

r_final(h,w) = 0;

g_final(h,w) = 0;

b_final(h,w) = 0;

else

im_final(h,w) = 0;

endelse

if ndims(im_init) == 3

r_final(h,w) = r(new_position(1),new_position(2));

g_final(h,w) = g(new_position(1),new_position(2));

b_final(h,w) = b(new_position(1),new_position(2));

else

im_final(h,w) = im_init(new_position(1),new_position(2));

endend

else

h_new = floor(new_position(1));

w_new = floor(new_position(2));

if h_new == 0

h_new = 1;

endif w_new == 0

w_new = 1;

end% 超出範圍控制,options為crop選項,超出範圍置零

if strcmp(options,'crop') && (h_new >= im_height || ...

w_new >= im_width || h_new < 0 || ...

w_new < 0)

if ndims(im_init) == 3

r_final(h,w) = 0;

g_final(h,w) = 0;

b_final(h,w) = 0;

else

im_final(h,w) = 0;

endelse

% 邊界控制

h1 = h_new + 1;

w1 = w_new + 1;

if h1 >= im_height + 1

h1 = mod(h1,im_height);

endif w1 >= im_width + 1

w1 = mod(w1,im_width);

endif ndims(im_init) == 3

% 雙線性插值的實現過程

% ref. 章毓晉. 影象工程(上冊)——影象處理. 清華大學出版社

r_temp1 = r(h1,w_new)*(new_position(1) - h_new) + ...

r(h_new,w_new)*(h_new + 1 - new_position(1));

r_temp2 = r(h1,w1)*(new_position(1) - h_new) + ...

r(h_new,w1)*(h_new + 1 - new_position(1));

r_final(h,w) = r_temp1*(w_new + 1 - new_position(2)) + ...

r_temp2*(new_position(2) - w_new);

g_temp1 = g(h1,w_new)*(new_position(1) - h_new) + ...

g(h_new,w_new)*(h_new + 1 - new_position(1));

g_temp2 = g(h1,w1)*(new_position(1) - h_new) + ...

g(h_new,w1)*(h_new + 1 - new_position(1));

g_final(h,w) = g_temp1*(w_new + 1 - new_position(2)) + ...

g_temp2*(new_position(2) - w_new);

b_temp1 = b(h1,w_new)*(new_position(1) - h_new) + ...

b(h_new,w_new)*(h_new + 1 - new_position(1));

b_temp2 = b(h1,w1)*(new_position(1) - h_new) + ...

b(h_new,w1)*(h_new + 1 - new_position(1));

b_final(h,w) = b_temp1*(w_new + 1 - new_position(2)) + ...

b_temp2*(new_position(2) - w_new);

else

gray_temp1 = im_init(h1,w_new)*(new_position(1) - h_new) + ...

im_init(h_new,w_new)*(h_new + 1 - new_position(1));

gray_temp2 = im_init(h1,w1)*(new_position(1) - h_new) + ...

im_init(h_new,w1)*(h_new + 1 - new_position(1));

im_final(h,w) = gray_temp1*(w_new + 1 - new_position(2)) + ...

gray_temp2*(new_position(2) - w_new);

endend

endend

endif ndims(im_init) == 3

im_final(:,:,1) = r_final;

im_final(:,:,2) = g_final;

im_final(:,:,3) = b_final;

endim_final = im2uint8(mat2gray(im_final));

matlab編寫程式編寫函式判斷兩個數是否互為質數

是兩個數互相不能整除的意思嗎?matlab裡編寫一個函式 實現判斷一個數是否是質數的功能 function isprime x if x 2 x 3 disp 這個數是質數 elseif x 1 mod x,2 0disp 這個數不是質數 elseif x 3 result 1 for i 3 2 ...

編寫程式,求Sn a aa aaaaaaaaaa(n個a)的值,其中a是數字,例如,a 2,n 5時,Sn

include main printf sum ld n sum include stdio.h include conio.h include math.h main long sn 0,si int i 0,j,a,n printf qing shu ru a yu n de zhi n sca...

編寫程式,求Sn a aa aaaaaaaaaa(n個a)的值,其中a是數字

魔天牙 include using namespace std sn a aa aaa n個a ua表示a un表示n 返回值為sn unsigned int sigman unsigned int ua,unsigned int un re n個a ua表示a un表示n uh表示a的位數 十進位...