Main Content

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

グレースケール イメージ内の領域の測定

この例では、グレースケール イメージ内のオブジェクトのプロパティを測定する方法を示します。これを行うには、まずグレースケール イメージをセグメント化してオブジェクトのバイナリ イメージを取得します。次に、関数 regionprops を使用して、バイナリ イメージ内の各オブジェクトに対応する元のグレースケールのピクセル値を解析します。

手順 1: 合成イメージの作成

補助関数 propsSynthesizeImage を使用して、5 つの分離領域を含むグレースケール イメージを作成します。

I = propsSynthesizeImage;
imshow(I)
title('Synthetic Image')

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

手順 2: バイナリ イメージの作成

元のイメージ内にオブジェクトを含んでいるバイナリ イメージを作成することによって、グレースケール イメージをセグメント化します。

BW = I > 0;
imshow(BW)
title('Binary Image')

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

手順 3: グレースケール イメージのピクセル値を使用したオブジェクト プロパティの計算

関数 regionprops は、'WeightedCentroid''MeanIntensity''MinIntensity''MaxIntensity' など、グレースケール イメージと共に使用できるいくつかのプロパティをサポートしています。これらのプロパティでは、計算にオブジェクトの元のピクセル値を使用します。

たとえば、regionprops を使用して、イメージのオブジェクトの中心と重み付き重心の両方を計算できます。オブジェクトと元のグレースケール イメージ (I) を含んでいるバイナリ イメージ (BW) を、引数として regionprops に渡す方法に注目します。

s = regionprops(BW,I,{'Centroid','WeightedCentroid'});

重み付き重心の位置と、重みのない重心の位置を比較するには、元のイメージを表示してから、関数 hold および plot を使用してイメージの重心に重ね合わせます。

imshow(I)
title('Weighted (red) and Unweighted (blue) Centroids'); 
hold on
numObj = numel(s);
for k = 1 : numObj
    plot(s(k).WeightedCentroid(1), s(k).WeightedCentroid(2), 'r*')
    plot(s(k).Centroid(1), s(k).Centroid(2), 'bo')
end
hold off

Figure contains an axes object. The axes object with title Weighted (red) and Unweighted (blue) Centroids contains 11 objects of type image, line. One or more of the lines displays its values using only markers

手順 4: カスタム ピクセル値ベースのプロパティの計算

'PixelValues' プロパティを使用して、元のグレースケール イメージのピクセル値に基づいたカスタム計算を行うことができます。'PixelValues' プロパティは、領域内のピクセルのグレースケール値を含むベクトルを返します。

例として、各領域の標準偏差を計算します。

s = regionprops(BW,I,{'Centroid','PixelValues','BoundingBox'});
imshow(I)
title('Standard Deviation of Regions')
hold on
for k = 1:numObj
    s(k).StandardDeviation = std(double(s(k).PixelValues));
    text(s(k).Centroid(1),s(k).Centroid(2), ...
        sprintf('%2.1f', s(k).StandardDeviation), ...
        'EdgeColor','b','Color','r');
end
hold off

Figure contains an axes object. The axes object with title Standard Deviation of Regions contains 6 objects of type image, text.

次の図は、イメージ内の各オブジェクトに重ね合わされた標準偏差測定を示しています。ラベル番号ごとに標準偏差を示すバー プロットなど、その他の方法でも結果を表示できます。

figure
bar(1:numObj,[s.StandardDeviation])
xlabel('Region Label Number')
ylabel('Standard Deviation')

Figure contains an axes object. The axes object with xlabel Region Label Number, ylabel Standard Deviation contains an object of type bar.

プロットを使用して、データを分割する方法を決定します。たとえば、以下のコードは 50 より小さい標準偏差をもつオブジェクトを識別します。

sStd = [s.StandardDeviation];
lowStd = find(sStd < 50);

imshow(I)
title('Objects Having Standard Deviation < 50')
hold on
for k = 1:length(lowStd)
    rectangle('Position',s(lowStd(k)).BoundingBox,'EdgeColor','y');
end
hold off

Figure contains an axes object. The axes object with title Objects Having Standard Deviation < 50 contains 2 objects of type image, rectangle.

参考

| |

関連するトピック