Zainab
Zainab

Reputation: 71

Fetching brightest pixel from a grayscale image

I have a grayscale image from which I need to find the top 0.1% brightest pixels.

I tried using a max function on 0.1% of the pixels but it is not giving me correct results.

Code:

[m,n]=size(image);

num_pixels=m*n;

pixels=floor(num_pixels*0.01)

Here, I got some 7000 number in my variable pixels. I am not getting how to sort these 7000 pixels because it is just giving me one count. I need to get all the pixel values of this count.

Can anybody suggest how to do this in MATLAB.

Upvotes: 1

Views: 784

Answers (1)

informaton
informaton

Reputation: 1482

You can get the top intensity value this way:

  sortedIntensityValues = sort(grayScaleImg(:));  % ascending order
  numPixels = numel(sortedIntensityValues);
  topIntensity = sortedIntensityValues(floor(numPixels*0.999));

This way (as mentioned in comments):

  sortedIntensityValues = sort(grayScaleImg(:),'descend');  % descending order
  numPixels = numel(sortedIntensityValues);
  topIntensity = sortedIntensityValues(floor(numPixels*0.001));

Or, if you have the stats toolbox you can use the prtcile function to do it like this:

  topIntensity = prctile(grayScaleImg(:),99.9); 

Here's proof of concept using the third approach:

  1. Create some code for you to test:

    grayScaleImg = rand(4096,4096);
    
  2. Obtain the intensity for which only 0.1% of pixels are brighter than.

    topIntensity = prctile(grayScaleImg(:),99.9); 
    
  3. Locate the pixels with intensity greater than this (i.e. top 0.1%) and place in a logical index array for reference.

    logicalIndices = grayScaleImg>topIntensity;
    

Upvotes: 1

Related Questions