histogram and intensity of images

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

3 Comments

I have answered the first question myself, but still can't do the 2nd part. Thanks
b=(IM(:)>3700)
c=sum(b)
For completeness and anyone else wanting to find out, this is how I obtained the "index" and plotted both:
ind = find(x>3700);
id=ind(1)
%Plot upto this.
stem(x(1:id),counts(1:id), 'b', '.');
hold on
stem(x(id+1:end),counts(id+1:end), 'r', '.');
hold off
You can simplify the first two lines to a single line by adding options to find() to extract the first element.
id = find(x>3700, 1, 'first');

Sign in to comment.

 Accepted 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));

4 Comments

Thanks!
x contains the gray levels. So scan that
binNumber = find(x<=3500, 1, 'last');
I have an image and i can calculate sum of intensities along x and y axis using following code. How can we plot sum of intensities on the image along x and y axis: many thanks for your help.
What I've done in this case is to have two additional axes, one on the left of the image axes, and one below it. I plot the vertical profile along the one on the left, with x and y switched, and the horizontal profile along the axes underneath the image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
verticalSum = sum(grayImage, 2);
horizontalSum = sum(grayImage, 1);
axes(handles.axesLeft); % Switch to the axes to the left of the image axes.
plot(verticalSum, 1:rows, 'b-', 'LineWidth', 2);
grid on;
axes(handles.axesBelow); % Switch to the axes below the image axes.
plot(1 : columns, horizontalSum, 'b-', 'LineWidth', 2);
grid on;

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!