Main Content

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

インテグラル イメージへの複数のフィルターの適用

この例では、インテグラル イメージのフィルター処理を使用して、サイズが異なる複数のボックス フィルターをイメージに適用する方法を説明します。積分イメージは局所的なイメージの和を高速に計算できる便利なイメージ表現です。ボックス フィルターは、各ピクセルの局所的な重み付き和と考えることができます。

イメージをワークスペースに読み取って表示します。

originalImage = imread('cameraman.tif');

figure
imshow(originalImage)
title('Original Image')

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

3 つのボックス フィルターのサイズを定義します。

filterSizes = [7 7;11 11;15 15];

最大のボックス フィルターのサイズに合わせてイメージをパディングします。各次元のパディングは、最大のフィルター サイズの半分に等しい量になります。複製型のパディングを使用すると、境界のアーティファクトの除去に役立ちます。

maxFilterSize = max(filterSizes);
padSize = (maxFilterSize - 1)/2;

paddedImage = padarray(originalImage,padSize,'replicate','both');

パディングされたイメージの積分イメージ表現を関数 integralImage で計算して表示します。積分イメージは、左から右および上から下に単調非減少となります。イメージ内の各ピクセルは、現在のピクセルの上側および左側の全ピクセル強度を合計した和を表します。

intImage = integralImage(paddedImage);

figure
imshow(intImage,[])
title('Integral Image Representation')

Figure contains an axes object. The axes object with title Integral Image Representation contains an object of type image.

サイズが異なる 3 つのボックス フィルターを積分イメージに適用します。関数 integralBoxFilter を使用すると、2 次元ボックス フィルターをイメージの積分イメージ表現に適用できます。

filteredImage1 = integralBoxFilter(intImage, filterSizes(1,:));
filteredImage2 = integralBoxFilter(intImage, filterSizes(2,:));
filteredImage3 = integralBoxFilter(intImage, filterSizes(3,:));

関数 integralBoxFilter は、パディングなしでフィルタリング処理が計算された部分のみを返します。同じ積分イメージをサイズが異なるボックス フィルターでフィルター処理すると、出力のサイズが変わります。これは関数 conv2'valid' オプションと似ています。

whos filteredImage*
  Name                  Size              Bytes  Class     Attributes

  filteredImage1      264x264            557568  double              
  filteredImage2      260x260            540800  double              
  filteredImage3      256x256            524288  double              

インテグラル イメージを計算する前に最大のボックス フィルターに合わせてイメージをパディングしたので、イメージの内容が失われません。filteredImage1 および filteredImage2 の余分なパディングはトリミングすることができます。

extraPadding1 = (maxFilterSize - filterSizes(1,:))/2;
filteredImage1 = filteredImage1(1+extraPadding1(1):end-extraPadding1(1),...
    1+extraPadding1(2):end-extraPadding1(2) );

extraPadding2 = (maxFilterSize - filterSizes(2,:))/2;
filteredImage2 = filteredImage2(1+extraPadding2(1):end-extraPadding2(1),...
    1+extraPadding2(2):end-extraPadding2(2) );

figure
imshow(filteredImage1,[])
title('Image filtered with [7 7] box filter')

Figure contains an axes object. The axes object with title Image filtered with [7 7] box filter contains an object of type image.

figure
imshow(filteredImage2,[])
title('Image filtered with [11 11] box filter')

Figure contains an axes object. The axes object with title Image filtered with [11 11] box filter contains an object of type image.

figure
imshow(filteredImage3,[])
title('Image filtered with [15 15] box filter')

Figure contains an axes object. The axes object with title Image filtered with [15 15] box filter contains an object of type image.

参考

| | |

関連するトピック