MATLAB: Histogram and intensity of images

imhistmax

Hello. I have a tif image and plot the histogram (its generally 12 bit grayscale, max value =4095)
I want to be able to do 2 things that I am struggling with.
1: I use the following to obtain and count the number of "saturated pixels" . But how can i modify it to say count the number of pixels above a certain value i.e.3500 counts
[maxval1,idx]=max(IM(:))
[row,col]=ind2sub(size(image), idx)
%count elements
n=numel(row)
2: After plotting a histogram of the intensities using
[counts,x] = imhist(IM,maxval);
stem(x,counts,'b');
xlim([0 maxval]);
ylim([0 max(counts)]);
(where I have allowed the axes to scale automatically to the max values), how can I superimpose on top of this in a different colour only part of the histogram that has intensities 3500 to 4095?
Thanks for any help. Jason

Best Answer

Jason, you have to call bar() or stem() twice, with "hold on" in between, once for each color. Something like
bar(counts(1:binNumber), 'BarColor', 'r');
hold on;
bar(counts(binNumber+1), 'BarColor', 'b');
where binNumber is the index where 3500 occurs.
For the first question, I'd do
countMoreThan3500 = sum(counts(binNumber+1:end));