在matlab中: 我有一个RGB图像'img'。 如果我写:
tmpImg=imhist(img);
我得到整个图像的直方图。 我想计算'minVal'和'maxVal'之间的像素直方图。
我该怎么做? 感谢
答案 0 :(得分:3)
您可以在每个频道的值范围内使用逻辑索引,例如对于图像I,I
和minVal
之间的maxVal
值
I(I>minVal&I<maxVal)
因此,对于3通道(彩色)图像,您可以按每个通道获得直方图:
I = double(imread('peppers.png')); % example image
minVal = 50;
maxVal = 200;
nBins = 50; % histogram bins
for i=1:3
C = I(:,:,i);
[countsC(i,:),binsC(i,:)] = hist(C(C>minVal&C<maxVal),nBins);
end
figure; hold all; % draw 3 "bounded" histograms on same plot
c = {'r','g','b'};
for i=1:3
stem(binsC(i,:), countsC(i,:), c{i}, '.');
end
axis tight