I'm working with a dataset of grayscale images (values ranging from [0,1]) and would like the average pixel intensity of each image to be the same (let's say 0.5). However, a simple multiplication would cause the range of pixels in the image to go out of the range [0,1].

If I'm correct, there are many ways to modify the pixels so that the mean is a desired value, but I'm looking for something that subjectively wouldn't change the appearance of the image too much (other than making it slightly lighter/darker). When I think "make sure the values are within a certain range", I think of a sigmoid function, but I'm not quite sure how to apply it to the task at hand.

Any guidance or ideas would be greatly appreciated! Thanks.

(not a homework question btw, just a side project)

  • 1
    $\begingroup$ Use one linear transformation for values less than the mean to move the mean to $1/2.$ Then another linear transformation for values greater than the mean to keep transformed values within $[0,1].$ (This may give some pretty strange looking transformed images.) // Also, you might be able to find a transformation $y = x^\alpha$ (where $\alpha> 0$ depends on the mean) which of course keeps values within $[0,1],$ and will also move the mean to $1/2.$ $\endgroup$
    – BruceET
    May 9, 2020 at 5:56

1 Answer 1


I couldn't think of any "closed form" solutions, so I ended up using an iterative approach. The method is as follows:

If the average pixel intensity is higher (brighter) than the desired mean, just iteratively multiply the image by a value less than $1$, say $0.995$, until the image drops below my desired value.

The case is a little different for brightening an image, since if you multiply it by a value over $1$, then pixel intensities will be above the max value ($1$). Instead, you brighten the image like this: im = im*(1-x)+1*x where x is some constant between $0$ and $1$, and the $1$ denotes the max brightness value.

Using this method, I can get the mean of the image to my desired value $\pm eta$ within 50 iterations, which runs fast enough on the computer for my task.

It doesn't quite adhere to my original requirement of maintaining the range of the image, but it works for my use case.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.