Joxler
Joxler

Reputation: 15

How to ignore certain values in A histogram? without using NaN in Matlab?

Say I have an grey scale image S and I'm looking to ignore all values above 250, how do I do it with out using NaN? the reason I don't want to use NaN is because Im looking to take statistical information from the resultant image such as average etc.

Upvotes: 1

Views: 2196

Answers (2)

rayryeng
rayryeng

Reputation: 104504

You can collect all image pixel intensities that are less than 250. That's effectively performing the same thing. If your image was stored in A, you can simply do:

pix = A(A < 250);

pix will be a single vector of all image pixels in A that have intensity of 249 or less. From there, you can perform whatever operations you want, such as the average, standard deviation, calculating the histogram of the above, etc.

Going with your post title, we can calculate the histogram of an image very easily using imhist that's part of the image processing toolbox, and so:

out = imhist(pix);

This will give you a 256 element vector where each value denotes the intensity count for a particular intensity. If we did this properly, you should only see bin counts up to intensity 249 (location 250 in the vector) and you should. If you don't have the image processing toolbox, you can repeat the same thing using histc and manually specifying the bin cutoffs to go from 0 up to 249:

out = histc(pix, 0:249);

The difference here is that we will get a histogram of exactly 250 bins whereas imhist will give you 256 bins by default. However, histc is soon to be deprecated and histcounts is what is recommended to use. Still the same syntax:

out = histcounts(pix, 0:249);

Upvotes: 2

Reed Espinosa
Reed Espinosa

Reputation: 413

You can use logical indexing to build a histogram only using values in your specified range. For example you might do something like:

histogram(imgData(imgData < 250))

Upvotes: 0

Related Questions