0

imrotate を使用せずに画像を回転させる独自のアルゴリズムを実行しようとしています。

clear all
img1 = imread('image1.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);

for i=1:m
    for j=1:n

        t = uint16((i)*cos(thet)-(j)*sin(thet)+m+100);
        s = uint16((i)*sin(thet)+(j)*cos(thet));
        if i>0 && j>0 && i<=m && j<=n

            try rotatedImg(t,s,1)=img1(i,j,1);
            catch
               a=1; 
            end

        end
    end
end


figure;
imshow(rotatedImg);

ただし、何らかの理由で、特定の角度で画像の一部が切り取られているため、画像全体がウィンドウに表示されません。私はそれを適切に行う方法を理解できないようです。画像が切り取られないように、毎回異なる角度でウィンドウを大きくする必要があるようです。

また、私の画像は、ある種の補間を行う必要があると思われる黒い点でいっぱいであることが判明しました。どうすればいいですか?

*私が使用している画像は ( http://i.stack.imgur.com/kDdx5.jpg ) で、これらの角度で回転しています - pi/6, pi/2 , ((pi/6)*4) *

4

1 に答える 1

2

さて、次の 2 つの問題があります。

  1. いつものように原点を中心に回転します。これが、角度ごとにオフセット (100) を調整する必要がある理由です。より良い解決策は、画像の中心を中心に回転することです。

  2. あなたはどんな種類の補間もしていません。それ自体が理由ではありませんが、丸め誤差のために、宛先イメージのすべてのピクセルにヒットしない場合があります。目的の画像を繰り返し処理し、ソースから正しいピクセルを取得することをお勧めします。

これが私の解決策です:

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);
于 2013-10-26T12:54:28.330 に答える