さて、次の 2 つの問題があります。
いつものように原点を中心に回転します。これが、角度ごとにオフセット (100) を調整する必要がある理由です。より良い解決策は、画像の中心を中心に回転することです。
あなたはどんな種類の補間もしていません。それ自体が理由ではありませんが、丸め誤差のために、宛先イメージのすべてのピクセルにヒットしない場合があります。目的の画像を繰り返し処理し、ソースから正しいピクセルを取得することをお勧めします。
これが私の解決策です:
clear all
img1 = imread('ngc6543a.jpg');
imshow(img1);
[m,n,p]=size(img1);
thet = pi/6;
m1=round(m*1.5);
n1=round(n*1.5);
rotatedImg = zeros(m1,n1, 3, 'uint8');
tic
for i=1:m1
for j=1:n1
p = [i; j] - [m1/2; n1/2];
source = [cos(thet), sin(thet); -sin(thet), cos(thet)] * p;
source = source + [m/2; n/2];
t = int16(source(1));
s = int16(source(2));
if t>0 && s>0 && t<=m && s<=n
rotatedImg(i,j,:) = img1(t,s,:);
end
end
end
toc
figure;
imshow(rotatedImg);
それは問題ないように見えますが、双一次補間をお勧めします。
なるほど、画像は 2 次ではありませんね。その場合、新しい次元を old*1.5 として計算するだけでは十分ではありませんが、より正確 (またはより寛大) です。
これは、すべての角度と任意の画像で機能する最終的なソリューションです。インデックスが (y,x) であるため、Matlab は少し面倒ですが、それ以外の場合はコードは問題ないはずです。
clear all
img1 = imread('kDdx5.jpg');
imshow(img1);
[orgHeight,orgWidth,p]=size(img1);
thet = pi/7;
matrix = [cos(thet), -sin(thet); sin(thet), cos(thet)];
p1 = abs(matrix * [orgWidth/2; orgHeight/2]);
p2 = abs(matrix * [orgWidth/2; -orgHeight/2]);
corner = [max(p1(1), p2(1)); max(p1(2), p2(2))];
newWidth = ceil(2*corner(1));
newHeight = ceil(2*corner(2));
rotatedImg = zeros(newHeight, newWidth, 3, 'uint8');
tic
for i=1:newWidth
for j=1:newHeight
p = [i; j] - [newWidth/2; newHeight/2];
source = matrix * p;
source = source + [orgWidth/2; orgHeight/2;];
t = int16(source(1));
s = int16(source(2));
if t>0 && s>0 && s<=orgHeight && t<=orgWidth
rotatedImg(j,i,:) = img1(s,t,:);
end
end
end
toc
figure;
imshow(rotatedImg);