Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

変換イメージのギャラリーの作成

この例では、チェッカーボード イメージにさまざまな変換を適用することによって、幾何学的変換の多くの特性を示します。

2 次元の幾何学的変換は、ユークリッド平面の各ポイントをユークリッド平面の別のポイントに関連付けるマッピングです。以下の例では、幾何学的変換は、直交座標 (x, y) を使用したポイントを直交座標 (u, v) を使用した別のポイントにマッピングする方法を示す規則によって定義されます。チェッカーボード パターンは、入力イメージの平面における座標格子と、各変換によって発生する歪みのタイプを可視化するのに役立ちます。

関数 checkerboard を使用してチェッカーボードのサンプル イメージを作成します。このイメージは四角形のタイルと 4 つの固有の角をもちます。このイメージにより、幾何学的変換によってチェッカーボードのイメージがどのように変化するかが簡単にわかります。この例を 1 回実行した後に、イメージ I を好みのイメージに変えてみます。

sqsize = 60;
I = checkerboard(sqsize,4,4);
imshow(I)
title("Original")

Figure contains an axes object. The axes object with title Original contains an object of type image.

イメージのサイズを取得して、背景の塗りつぶしの値を指定します。

nrows = size(I,1);
ncols = size(I,2);
fill = 0.3;

相似変換

相似変換には、回転、等方性スケーリング、平行移動が含まれますが、鏡映は含まれません。形状と角度は保持されます。平行線は平行のままであり、直線は直線のままになります。

"x" 方向と "y" 方向の回転角度、倍率、および平行移動の量を指定します。次に、相似幾何学的変換オブジェクトを作成します。

scale = 1.2;
angle = 40;
tx = 0;   
ty = 0;
t_sim = simtform2d(scale,angle,[tx ty]);

相似幾何学的変換をイメージに適用し、結果を表示します。

I_similarity = imwarp(I,t_sim,FillValues=fill);

imshow(I_similarity);
title("Similarity")

Figure contains an axes object. The axes object with title Similarity contains an object of type image.

tx または ty をゼロ以外の値に変更した場合、出力イメージには何の影響もないことがわかります。平行移動を含む、変換に対応する座標を表示する場合は、空間参照情報を含めます。

[I_similarity,RI] = imwarp(I,t_sim,FillValues=fill);

imshow(I_similarity,RI)
axis on
title("Similarity (Spatially Referenced)")

Figure contains an axes object. The axes object with title Similarity (Spatially Referenced) contains an object of type image.

imwarp から出力空間参照オブジェクト RI を渡すことで平行移動の情報が表示されます。表示する出力イメージの部分を指定するには、関数 imwarp で名前と値の引数 OutputView を使用します。

反射相似変換

反射相似変換では、三角形は相似する三角形にマッピングされます。

"x" 方向と "y" 方向の回転角度、倍率、平行移動の量、および鏡映係数を指定します。鏡映を実行するには鏡映係数 r を -1 に指定します。そうでない場合は 1 に指定します。この変換を表す 3 行 3 列の行列を作成します。

scale = 1.5;   
angle = 10;
tx = 0;
ty = 0;
r = -1;

sc = scale*cosd(angle);
ss = scale*sind(angle);

A = [ sc  r*ss  tx;
     -ss  r*sc  ty;
       0     0   1];

反射相似はアフィン変換の一部であるため、幾何学的変換行列からアフィン幾何学的変換オブジェクトを作成します。

t_reflective = affinetform2d(A);

反射相似変換をイメージに適用し、出力空間参照情報とともに結果を表示します。

[I_reflective,RI] = imwarp(I,t_reflective,FillValues=fill);
imshow(I_reflective,RI)
axis on
title("Reflective Similarity")

Figure contains an axes object. The axes object with title Reflective Similarity contains an object of type image.

アフィン変換

アフィン変換では、"x""y" の次元が個別にスケーリングまたはせん断されて、平行移動、鏡映、および回転が行われます。平行線は平行のままです。直線は直線のままです。

一般的なアフィン変換行列を指定します。1 列目と 2 列目の 6 つの要素はすべて異なるものにすることができますが、3 行目は [0 0 1] でなければなりません。幾何学的変換行列からアフィン幾何学的変換オブジェクトを作成します。

A = [  1  1  0; 
     0.3  2  0;
       0  0  1];
t_aff = affinetform2d(A);

一般的なアフィン変換をイメージに適用し、出力空間参照情報とともに結果を表示します。

I_affine = imwarp(I,t_aff,FillValues=fill);
imshow(I_affine)
title("Affine")

Figure contains an axes object. The axes object with title Affine contains an object of type image.

射影変換

射影変換では、四角形は四角形にマッピングされます。直線は直線のままですが、平行線は必ずしも平行のままではありません。

一般的な射影変換行列を指定します。行列の 9 つの要素をすべて異なるものにすることができ、最後の行にも制約はありません。幾何学的変換行列から射影幾何学的変換オブジェクトを作成します。

A = [    1       1  0; 
         0       1  0;
     0.002  0.0002  1];
t_proj = projtform2d(A);   

射影変換をイメージに適用し、出力空間参照情報とともに結果を表示します。

I_projective = imwarp(I,t_proj,FillValues=fill);
imshow(I_projective)
title("Projective")

Figure contains an axes object. The axes object with title Projective contains an object of type image.

区分的線形変換

区分的線形変換では、イメージの個々の領域にアフィン変換が適用されます。この例では、チェッカーボードの左上、右上、左下のポイントは変化しませんが、変換後のイメージの右下隅が元の座標から右側に 50%、下側に 20% 移動するように、イメージの右下の三角形領域が引き伸ばされます。

movingPoints = [0 0; 0 nrows; ncols 0; ncols nrows;]; 
fixedPoints  = [0 0; 0 nrows; ncols 0; ncols*1.5 nrows*1.2]; 
t_piecewise_linear = fitgeotform2d(movingPoints,fixedPoints,"pwl"); 

I_piecewise_linear = imwarp(I,t_piecewise_linear,FillValues=fill);
imshow(I_piecewise_linear)
title("Piecewise Linear")

Figure contains an axes object. The axes object with title Piecewise Linear contains an object of type image.

正弦変換

次の例とそれに続く 2 つの例では、明示的なマッピングを作成して、等間隔グリッドの各ポイント (xi,yi) を別のポイント (ui,vi) に関連付ける方法を説明します。このマッピングは geometricTransform2d オブジェクトに格納され、imwarp によってイメージの変換に使用されます。

この正弦変換では、各ピクセルの x 座標は変化しません。ピクセルの各行の y 座標は、正弦波パターンに従って上または下にシフトします。

a = ncols/12; % Try varying the amplitude of the sinusoid
ifcn = @(xy) [xy(:,1), xy(:,2) + a*sin(2*pi*xy(:,1)/nrows)];
tform = geometricTransform2d(ifcn);

I_sinusoid = imwarp(I,tform,FillValues=fill);
imshow(I_sinusoid);
title("Sinusoid")

Figure contains an axes object. The axes object with title Sinusoid contains an object of type image.

樽形変換

樽形歪曲はイメージを中心から外側に放射状に変形します。歪みは中心から遠ざかるほど大きくなり、側面が凸状になります。

まず、ピクセルのインデックスを中心からの距離にマッピングする関数を定義します。関数 meshgrid を使用し、イメージの左上隅を原点として、各ピクセルの x 座標と y 座標の配列を作成します。

[xi,yi] = meshgrid(1:ncols,1:nrows);

原点をイメージの中心にシフトします。次に、関数 cart2pol を使用して、x および y 直交座標を円柱座標の角度 (theta) および半径 (r) の座標に変換します。r は、中心ピクセルからの距離の増加に従って線形に変化します。

xt = xi - ncols/2;
yt = yi - nrows/2;
[theta,r] = cart2pol(xt,yt);

3 次項の振幅 a を定義します。このパラメーターは調整できます。次に、r に 3 次項を加算し、r が中心ピクセルからの距離によって非線形に変化するようにします。

a = 1; % Try varying the amplitude of the cubic term.
rmax = max(r(:));
s1 = r + r.^3*(a/rmax.^2);

直交座標系に変換し直します。原点をイメージの右上隅に再シフトします。

[ut,vt] = pol2cart(theta,s1);
ui = ut + ncols/2;
vi = vt + nrows/2;

(xi,yi) と (ui,vi) の間のマッピングを geometricTransform2d オブジェクトに格納します。imwarp を使用して、ピクセルのマッピングに従ってイメージを変換します。

ifcn = @(c) [ui(:) vi(:)];
tform = geometricTransform2d(ifcn);

I_barrel = imwarp(I,tform,FillValues=fill);
imshow(I_barrel)
title("Barrel")

Figure contains an axes object. The axes object with title Barrel contains an object of type image.

ピンクッション変換

ピンクッション歪曲は、3 次方程式に負の振幅があるため樽形歪曲の逆になります。歪みは中心から遠ざかるほど大きくなり、凹面として現れます。

樽形変換と同じ theta および r の値で始めることができます。3 次項の別の振幅 b を定義します。このパラメーターは調整できます。次に、r の 3 次項を減算し、r が中心ピクセルからの距離に対して非線形に変化するようにします。

b = 0.4; % Try varying the amplitude of the cubic term.
s = r - r.^3*(b/rmax.^2);

直交座標系に変換し直します。原点をイメージの右上隅に再シフトします。

[ut,vt] = pol2cart(theta,s);
ui = ut + ncols/2;
vi = vt + nrows/2;

(xi,yi) と (ui,vi) の間のマッピングを geometricTransform2d オブジェクトに格納します。imwarp を使用して、ピクセルのマッピングに従ってイメージを変換します。

ifcn = @(c) [ui(:) vi(:)];
tform = geometricTransform2d(ifcn);
I_pin = imwarp(I,tform,FillValues=fill);
imshow(I_pin)
title("Pin Cushion")

Figure contains an axes object. The axes object with title Pin Cushion contains an object of type image.

まとめ: チェッカーボードのすべての幾何学的変換の表示

figure
subplot(3,3,1),imshow(I),title("Original")
subplot(3,3,2),imshow(I_similarity),title("Similarity")
subplot(3,3,3),imshow(I_reflective),title("Reflective Similarity")
subplot(3,3,4),imshow(I_affine),title("Affine")
subplot(3,3,5),imshow(I_projective),title("Projective")
subplot(3,3,6),imshow(I_piecewise_linear),title("Piecewise Linear")
subplot(3,3,7),imshow(I_sinusoid),title("Sinusoid")
subplot(3,3,8),imshow(I_barrel),title("Barrel")
subplot(3,3,9),imshow(I_pin),title("Pin Cushion")

Figure contains 9 axes objects. Axes object 1 with title Original contains an object of type image. Axes object 2 with title Similarity contains an object of type image. Axes object 3 with title Reflective Similarity contains an object of type image. Axes object 4 with title Affine contains an object of type image. Axes object 5 with title Projective contains an object of type image. Axes object 6 with title Piecewise Linear contains an object of type image. Axes object 7 with title Sinusoid contains an object of type image. Axes object 8 with title Barrel contains an object of type image. Axes object 9 with title Pin Cushion contains an object of type image.

表示されているイメージのスケールは subplot によって変更されています。

参考

関数

オブジェクト

関連するトピック