Bulva
Bulva

Reputation: 1248

Histogram equalization (or stretching) of RGB picture in python

I am creating automatically JPG pictures from multispectral data. Created picture is very dark. So I thought it would be best idea change brightness (like Image.Enhance in PIL). But there was a problem, because some pictures need more brightness than others.

Original Image, Histogram equalization of three channels(PIL), and something similar what I need in IrfanView (auto adjust colors)

So next idea was try linear stretching of histogram. So I created script which iterate over RGB tuples and compute new intensity for pixels. There was very small difference. Probably because the range of values was everytime 0-255. Then I tried histogram equalization (ImageOps) for R, G and B but the result was no good, please see middle part of picture. I found on the internet that this is not good approach because colors can change dramatically. It is probably my case.

The best idea looks convert RGB array to HSL and then change luminance but I can't use constant for maximize Luminance because pictures are different and need different constants for. Should I use histogram equalization on Luminance or what is the best approach how stretch or probably better histogram equalization of my picture?

I am looking for something like Image/Auto adjust colors in IrfanView or in some SW are used name Linear Normalization...

I hope that picture will be help to you understand my problem. I probably choose bad way how to achieve my goal.

Thank you for any answer, I will be very glad.


EDIT

Left image for download

Next images I can upload later, today.

Upvotes: 1

Views: 5268

Answers (2)

Jeru Luke
Jeru Luke

Reputation: 21223

I would suggest proceeding with the same approach as you have stated with slight modification.

  • Convert the RGB image to LAB image.
  • Apply localized histogram equalization to the L-channel.
  • Merge it back with the other channels.
  • Convert it back to RGB image.

You can check my answer for this in a different question here:

The code I have there is written for OpenCV using python. You can modify it for C language if you wish.

Let me know if it has helped you!!

Upvotes: 1

Krestone
Krestone

Reputation: 37

I am not sure if this applies, and I have not applied this myself, but I was reading on this article about underwater contrast stretching: http://www.iaeng.org/IJCS/issues_v34/issue_2/IJCS_34_2_12.pdf

What it suggests might help

"In order to address the issues discussed above, we propose
an approach based on slide stretching. Firstly, we use contrast
stretching of RGB algorithm to equalize the colour contrast in
the images. Secondly, we apply the saturation and intensity
stretching of HSI to increase the true colour and solve the
problem of lighting"

Upvotes: 0

Related Questions