kakush
kakush

Reputation: 3484

Calculate histogram with minVal and maxVal thresholds

In matlab: I have an RGB image 'img'. if I write :

tmpImg=imhist(img);

I get the histogram of the whole image. I want to calculate the histogram of pixels between 'minVal' and 'maxVal'.

How do I do that? thanks

Upvotes: 1

Views: 753

Answers (1)

gevang
gevang

Reputation: 5014

You can use logical indexing inside the value range for each channel, i.e. for an image I, the values of I between minVal and maxVal are

I(I>minVal&I<maxVal)

So, for a 3-channel (color) image you can have the histograms per channel as follows:

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

Upvotes: 3

Related Questions