How to find number of intensity levels in an image

10 views (last 30 days)
I'm working with SEM images, for finding pore size distribution, I need to provide Number of intensity levels as an input, so how to determine it?

Accepted Answer

DGM
DGM on 31 Aug 2023
You can use unique() to find the number of unique gray levels if that's what you're after. Consider the following image with 8 gray levels:
inpict = imread('grad8-1.png');
nu = numel(unique(inpict))
nu = 8
But be aware that things like antialiasing and compression may mean that the number of nominal gray levels isn't the same as the actual number of unique values in the image.
inpict = imread('grad8-2.png'); % antialiased
nu = numel(unique(inpict))
nu = 114
inpict = imread('grad8-1.jpg'); % lossy compresssion
nu = numel(unique(inpict))
nu = 228
  12 Comments
William Rose
William Rose on 6 Sep 2023
Thanks for explaining the XY problem clearly and for the link. That is useful concept. I will keep it in mind when I am seeking or trying to give help.
Image Analyst
Image Analyst on 6 Sep 2023
I'm not sure why you need multiple thresholds (because I haven't see the image). It could be that you just need one set of thresholds - a lower one and an upper one - and then your mask is everything in between. SEM images are notoriously hard to threshold and I'm not sure any automatic method would be good. You may want to use an interactive one like my utility:
It could be, if you're lucky, that you ask for 5 thresholds and pick the intensity range between the second and third threshold to mask out the stuff you want (the pores).
You might want to learn about granulometry:

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 31 Aug 2023
Just mentioning imhist in case you wanted the number of pixels at each gray level (distribution) instead the number of unique gray levels (of which I don't think I've ever used and can't imagine why that would even be necessary).
  1 Comment
Walter Roberson
Walter Roberson on 31 Aug 2023
The number of distinct intensity levels can be used as part of huffman encoding.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!