# CLAHE - Redistributing excess pixels among histogram bins

In order to clip an image's histogram to a maximum value, according to the CLAHE algorithm, the excess pixels (above the clip limit) will be distributed equally among all the histogram bins (While keeping track none of the bins exceed the max limit of course).

Now, when working with (8,8) region size and distributing 13 pixels on 256 bins, the uniform added value (13/256) will be rounded to zero and so the total number of pixels in histogram will be reduced from (8,8) -> 64px to 64 - 13 = 51px.

That doesn't make sense since I'll be neglecting 13 pixels of the region.

Any tips on how to tackle this part of the algorithm?

you have to redistributed 13 pixels(# of clipped) to the histogram.

// clip histogram
194
195             if (clipLimit_ > 0)
196             {
197                 // how many pixels were clipped
198                 int clipped = 0;
199                 for (int i = 0; i < histSize; ++i)
200                 {
201                     if (tileHist[i] > clipLimit_)
202                     {
203                         clipped += tileHist[i] - clipLimit_;
204                         tileHist[i] = clipLimit_;
205                     }
206                 }
207
208                 // redistribute clipped pixels
209                 int redistBatch = clipped / histSize;
210                 int residual = clipped - redistBatch * histSize;
211
212                 for (int i = 0; i < histSize; ++i)
213                     tileHist[i] += redistBatch;
214
215                 for (int i = 0; i < residual; ++i)
216                     tileHist[i]++;
217             }

• Even though the code might be helpful it would be good if you added some explanations in normal text. – Matt L. Jun 15 '16 at 7:45

In order to distribute 13 pixels on 256 bins, you can divide 256 to 13 (256/13) that gives step size. So you can add 1 to all values on the step size. For example, (256/13) rounds to 20 that you can add 1 to (1, 21, 41, ...) histogram values.