# Histogram Equalization - mapping a certain portion of pixels to pure black and white

How can I make sure to map, for example, 7% of the pixels to pure black (0) and pure white (255) in a grayscale image?

Compute the image histogram, then its running integral (i.e. an array T with as many entries as there are gray levels, such as T(k) = sum_{i=0, 1..k} (histogram(i)).

The last element of the integral is, of course, the area of the image. For the lower threshold, start from the left and find the level at which T exceeds your desired fraction. Analogously, for the high threshold, start from the right and find the level at which T goes below your high fraction.

why 7%? surely its not random. if it were totally random which pixels were black you would just pick 7 out of every 100 pixels and make them black and the rest white. do you mean to set a black white threshold of 93%?

• I want to histogram-equalize an image but also want to make sure that the process maps 7% (just an arbitrary number I pick) of the pixels to pure black or white. Jan 30 '15 at 5:59
• there you go then. if its a fixed image, sample 100 pixels, store a buffer of 7 pixels with each index containing pixel number (rand() * 100) of the sampled series, then flip a coin again ( rand() ) and if rand() > 0.5, set the rgb to 255, if its < 0.5 then set the rgb to 0 Jan 30 '15 at 6:04

You probably want to perform histogram stretching, i.e. remap the gray scale so that the lower 7% map to black and the upper 7% map to white, and values in between are spread over 0..255.

As @Francesco explained, accumulate the histogram bins until you reach 7% of the total, starting from either end of the histogram. This will give you two indexes, b and w, and use the linear law

g < b -> 0
b < g < w -> 255 * (g - b) / (w - b)
w < g -> 255


If you truly want histogram equalization, you can compute the remapping rule as usual but based on the 7%-93% portion of the histogram.

Although your claim does not make much sense to me, you may do following steps to ensure get what you described in your question.

1. Construct some discriminative feature space based on the original image.
2. Find two pixels that are separated most in this feature space.
3. Pick one pixel as your landmark, and sort all pixels with respect to their distance away from this landmark.
4. Set the first PERC% of these pixels to black, and the reset to white.

The "discriminative feature" can be any feature. The whole point here is to make different pixels different values. You may directly use raw pixel intensity if you want, but keep in mind of possible cases that there are 10% of 0 pixels, but you only want to set 7% of all pixels to black.

The general idea here is to convert all pixels in an image into a new "ranked" feature space. In this way, selecting PERC% of pixels is equivalent to set a threshold of value PERC to this feature map.

• What is the connection with histogram equalization ?? Oct 1 '15 at 16:24