Main Content

이변량 히스토그램을 사용한 색 분석

이 예제에서는 이변량 히스토그램 플롯의 색조를 조정하여 Bin에 대해 추가적으로 자세히 표시할 수 있는 방법을 보여줍니다.

여러 종류의 고추와 채소의 컬러 사진 이미지 peppers.png를 불러옵니다. 부호 없는 8비트 정수형 배열 rgb는 이미지 데이터를 포함합니다.

rgb = imread('peppers.png');
imshow(rgb)

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

각 픽셀의 빨간색 RGB 값과 녹색 RGB 값에 대한 이변량 히스토그램을 플로팅하여 색 분포를 시각화합니다.

r = rgb(:,:,1);
g = rgb(:,:,2);
b = rgb(:,:,3);
histogram2(r,g,'DisplayStyle','tile','ShowEmptyBins','on', ...
    'XBinLimits',[0 255],'YBinLimits',[0 255]);
axis equal
colorbar
xlabel('Red Values')
ylabel('Green Values')
title('Green vs. Red Pixel Components')

Figure contains an axes object. The axes object with title Green vs. Red Pixel Components, xlabel Red Values, ylabel Green Values contains an object of type histogram2. This object represents g vs. r.

도수가 매우 큰 Bin이 조금밖에 없기 때문에 히스토그램은 색조의 아래쪽으로 심하게 치우쳐 있습니다. 그 결과, 대부분의 Bin이 컬러맵의 첫 번째 색인 파란색으로 표시됩니다. 추가적으로 자세히 표시하지 않으면 어떤 색이 더 우세한지 단정하기가 어렵습니다.

더 자세히 보려면 좌표축의 CLim 속성이 0에서 500 사이의 범위를 갖도록 설정하여 히스토그램 색조를 다시 스케일링해야 합니다. 이 경우 도수가 500 이상인 히스토그램 Bin은 컬러맵의 마지막 색인 노란색으로 표시됩니다. 대부분의 Bin 도수는 이보다 더 작은 범위 내에 있기 때문에 Bin의 색은 더 다양하게 표시됩니다.

ax = gca;
ax.CLim = [0 500];

Figure contains an axes object. The axes object with title Green vs. Red Pixel Components, xlabel Red Values, ylabel Green Values contains an object of type histogram2. This object represents g vs. r.

비슷한 방법을 사용하여 빨간색과 파란색, 그리고 녹색과 파란색 중 어떤 색이 더 우세한지 비교할 수 있습니다.

histogram2(r,b,'DisplayStyle','tile','ShowEmptyBins','on',...
    'XBinLimits',[0 255],'YBinLimits',[0 255]);
axis equal
colorbar
xlabel('Red Values')
ylabel('Blue Values')
title('Blue vs. Red Pixel Components')
ax = gca;
ax.CLim = [0 500];

Figure contains an axes object. The axes object with title Blue vs. Red Pixel Components, xlabel Red Values, ylabel Blue Values contains an object of type histogram2. This object represents b vs. r.

histogram2(g,b,'DisplayStyle','tile','ShowEmptyBins','on',...
    'XBinLimits',[0 255],'YBinLimits',[0 255]);
axis equal
colorbar
xlabel('Green Values')
ylabel('Blue Values')
title('Green vs. Blue Pixel Components')
ax = gca;
ax.CLim = [0 500];

Figure contains an axes object. The axes object with title Green vs. Blue Pixel Components, xlabel Green Values, ylabel Blue Values contains an object of type histogram2. This object represents b vs. g.

각각의 경우 파란색이 가장 덜 우세한 색 신호입니다. 세 개의 히스토그램을 모두 살펴보면 빨간색이 우세한 색인 것 같습니다.

RGB 컬러스페이스의 색 히스토그램을 생성하여 결과를 확인해 보겠습니다. RGB 값이 작은 곳에서는 세 가지 색 성분 모두 도수가 치솟았습니다. 반면, 100 이상의 RGB 값은 다른 색 성분보다 빨간색 성분에서 더 자주 발생합니다.

histogram(r,'BinMethod','integers','FaceColor','r','EdgeAlpha',0,'FaceAlpha',1)
hold on
histogram(g,'BinMethod','integers','FaceColor','g','EdgeAlpha',0,'FaceAlpha',0.7)
histogram(b,'BinMethod','integers','FaceColor','b','EdgeAlpha',0,'FaceAlpha',0.7)
xlabel('RGB value')
ylabel('Frequency')
title('Color histogram in RGB color space')
xlim([0 257])

Figure contains an axes object. The axes object with title Color histogram in RGB color space, xlabel RGB value, ylabel Frequency contains 3 objects of type histogram.

참고 항목

|