Thread Subject: how to select a certain percentage of "bright" pixels from an array

Subject: how to select a certain percentage of "bright" pixels from an array

From: Frank Chang

Date: 28 Aug, 2009 19:11:03

Message: 1 of 3

Hi group,

I have a question regarding a quite unusual array operation which has
puzzled me for a few days. I cannot come up with a reliable solution.
I'd like to hear your thoughts.

The problem can be described as follows.

If I have a 2D array, all elements of which are real numbers. I'd like
to keep a certain percentage of the pixels whose values are high, and
change all the others to zero. For example, if the original array is
like this

[ 4, 5, 6
 7, 8, 9
 1, 2, 3]

And I'd like to keep 20% of the pixels whose values are higher than
the rest. The converted array becomes

[ 0, 0, 0
 0, 8, 9
 0, 0, 0].

I should note that the matrix has uncertain dimensions, and the
percentage I'd like to keep is really case by case. I thought
histogram might be the best way to do this. But I simply could not get
it to work... Could you please shed me some light? Thank you!

Best Regards,
Frank

Subject: how to select a certain percentage of "bright" pixels from an

From: Ashish Uthama

Date: 28 Aug, 2009 19:44:05

Message: 2 of 3

On Fri, 28 Aug 2009 15:11:03 -0400, Frank Chang <etaghtron@gmail.com>
wrote:

> Hi group,
>
> I have a question regarding a quite unusual array operation which has
> puzzled me for a few days. I cannot come up with a reliable solution.
> I'd like to hear your thoughts.
>
> The problem can be described as follows.
>
> If I have a 2D array, all elements of which are real numbers. I'd like
> to keep a certain percentage of the pixels whose values are high, and
> change all the others to zero. For example, if the original array is
> like this
>
> [ 4, 5, 6
> 7, 8, 9
> 1, 2, 3]
>
> And I'd like to keep 20% of the pixels whose values are higher than
> the rest. The converted array becomes
>
> [ 0, 0, 0
> 0, 8, 9
> 0, 0, 0].
>
> I should note that the matrix has uncertain dimensions, and the
> percentage I'd like to keep is really case by case. I thought
> histogram might be the best way to do this. But I simply could not get
> it to work... Could you please shed me some light? Thank you!
>
> Best Regards,
> Frank


The simple part, once you find the threshold you can set the ones below it
to 0 using logical idexing:
image( image<Threshold) = 0;

To find the Threshold, you could either
-use SORT (if your image is small enough). Convert you percentage measure
to an index. Example, top 20% would mean use value at index 9 for the
Threshold for am image with 10 pixels.
-If you know before hand, the range of the data, you might be able to
devise some heuristic measure to obtain the Threshold
-am sure there are more.. :)


Note: What should happen when there are repeated pixel values?

Subject: how to select a certain percentage of "bright" pixels from an

From: Image Analyst

Date: 28 Aug, 2009 20:09:04

Message: 3 of 3

As Ashish alluded to, it can get complicated depending on how picky you are in getting the EXACT percentage.

What I'd do is to just take the histogram, then use cumsum() to get your percentages. Something like this:
clc;
clear all;
close all;
workspace;
imageArray = imread('cameraman.tif');
[pixelCounts grayLevels] = imhist(imageArray);
cdf = cumsum(pixelCounts) / sum(pixelCounts);
subplot(1,4, 1);
imshow(imageArray);
subplot(1,4, 2);
bar(pixelCounts);
subplot(1,4, 3);
plot(cdf);
subplot(1,4, 4);
thresholdIndex = find(cdf < 0.8, 1, 'last');
thresholdValue = grayLevels(thresholdIndex);
thresholdedImage = imageArray;
thresholdedImage(imageArray(:) < thresholdValue) = 0;
imshow(thresholdedImage);
set(gcf, 'Position', get(0, 'ScreenSize')); % Maximize figure.

However, what happens if you want exactly 80% and the bin at 150 gray levels has 79% but if you include the bin at 151 gray levels, you're now up to 84%? Now what do you do? You have to take only SOME of the pixels that have gray level 151 and set only those to zero. Okay, but which do you pick? Do you just randomly select a portion of the 151 gray level pixels from around the image?

For a first shot at it, I'd probably not worry about that since it may not really make much of a difference in what you want to do.
Good luck,
ImageAnalyst

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
histogram Image Analyst 28 Aug, 2009 16:14:21
rssFeed for this Thread

Contact us at files@mathworks.com