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'});

가중치가 부여된 중심 위치와 가중치가 부여되지 않은 중심 위치를 비교하려면 원본 영상을 표시한 다음 holdplot 함수를 사용하여 영상에 중심을 겹쳐 놓습니다.

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.

참고 항목

| |

관련 항목