Main Content

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

正規化された相互相関を使ったイメージのレジストレーション

この例では、より大きなイメージ内でテンプレート イメージを検出する方法を示します。時々、1 つのイメージは別の一部になります。正規化された相互相関は、それらの 1 つを変換することで、イメージをどのように結合し、または位置合わせするかを決定するために使うことができます。

手順 1: イメージの読み取り

onion = imread("onion.png");
peppers = imread("peppers.png");

imshow(onion)

Figure contains an axes object. The axes object contains an object of type image.

imshow(peppers)

Figure contains an axes object. The axes object contains an object of type image.

手順 2: 各イメージの部分領域の選択

似ている領域を選択することが重要です。イメージ sub_onion はテンプレートであり、イメージ sub_peppers より小さくなければなりません。以下の非対話型スクリプトまたは対話型スクリプトを使用して、これらの部分領域を取得できます。

% non-interactively
rect_onion = [111 33 65 58];
rect_peppers = [163 47 143 151];
sub_onion = imcrop(onion,rect_onion);
sub_peppers = imcrop(peppers,rect_peppers);

% OR 
   
% interactively
%[sub_onion,rect_onion] = imcrop(onion); % choose the pepper below the onion
%[sub_peppers,rect_peppers] = imcrop(peppers); % choose the whole onion

% display sub images
imshow(sub_onion)

Figure contains an axes object. The axes object contains an object of type image.

imshow(sub_peppers)

Figure contains an axes object. The axes object contains an object of type image.

手順 3: 正規化した相互相関の実行とピークの座標の検出

正規化した相互相関を計算し、表面プロットとして表示します。相互相関行列のピークは、サブイメージが最適相関である場合に発生します。normxcorr2 は、グレースケール イメージでのみ機能するため、normxcorr2 に各サブイメージの赤い面を渡します。

c = normxcorr2(sub_onion(:,:,1),sub_peppers(:,:,1));
figure
surf(c) 
shading flat

Figure contains an axes object. The axes object contains an object of type surface.

手順 4: イメージ間の全オフセットの検出

イメージ間の全オフセットまたは平行移動は、相互相関行列のピークの位置と、サブイメージのサイズおよび位置によって異なります。

% offset found by correlation
[max_c,imax] = max(abs(c(:)));
[ypeak,xpeak] = ind2sub(size(c),imax(1));
corr_offset = [(xpeak-size(sub_onion,2)) 
               (ypeak-size(sub_onion,1))];

% relative offset of position of subimages
rect_offset = [(rect_peppers(1)-rect_onion(1)) 
               (rect_peppers(2)-rect_onion(2))];

% total offset
offset = corr_offset + rect_offset;
xoffset = offset(1);
yoffset = offset(2);

手順 5: 玉ねぎのイメージがピーマンのイメージから抽出されたかどうかの確認

onionpeppers の内に収まる場所について考えます。

xbegin = round(xoffset + 1);
xend   = round(xoffset + size(onion,2));
ybegin = round(yoffset + 1);
yend   = round(yoffset + size(onion,1));

% extract region from peppers and compare to onion
extracted_onion = peppers(ybegin:yend,xbegin:xend,:);
if isequal(onion,extracted_onion) 
   disp("onion.png was extracted from peppers.png")
end
onion.png was extracted from peppers.png

手順 6: ピーマンのイメージのサイズに合わせて玉ねぎのイメージをパディング

上記で決定したオフセットを使用して、onion イメージをパディングし、peppers に重ね書きします。

recovered_onion = uint8(zeros(size(peppers)));
recovered_onion(ybegin:yend,xbegin:xend,:) = onion;
imshow(recovered_onion)

Figure contains an axes object. The axes object contains an object of type image.

手順 7: アルファ ブレンディングを使用したイメージの同時表示

アルファ ブレンディングを使用してpeppers イメージの 1 つの平面を recovered_onion イメージとともに表示します。

imshowpair(peppers(:,:,1),recovered_onion,"blend")

Figure contains an axes object. The axes object contains an object of type image.