2

I want to perform contrast stretching on an image. The image is as shown (The data-tips are created by me in MATLAB to give more information about the image, the timestamp is already there on the image).

enter image description here

Now, how to perform contrast stretching has already been described here. But that formula won't work here since imgMin=0 and imgMax=255. So the image would remain unchanged. So my workaround was local contrast stretching. I am unsure as to how to do this (and also relatively fast i.e. I should be able to contrast stretch frames of video and still play video in realtime. I tried histogram equalization, though it produces realtime results in OpenCV, the contrast stretching is not impressive). To understand if the algorithm is working, I am also open to the use of MATLAB.

I have also shown datatips where you can see the pixel values. I have converted my color image into gray-scale first. In short, the aim is white lanes should become brighter though brightest part of the image takes value 255 (i.e. the sun).

Community
  • 1
  • 1
Autonomous
  • 8,935
  • 1
  • 38
  • 77

2 Answers2

5

You might get the results you are looking for with detail enhancement filters, using a large filter radius. A popular example of detail enhancement is the "Clarity" effect in Adobe Lightroom. What these algorithms do actually amounts to increasing contrast locally.

The Guided Filter can be used for detail enhancement (see page 10 of the linked paper), and it may be sufficiently fast for video, in contrast to the better known Bilateral Filter. It is also relatively easy to implement, at least for grayscale images or for per-channel operation. Matlab code for experimenting is here, it contains a detail enhancement example, if I recall correctly.

Theory: Roughly, the idea is to add a high-pass filtered version of the image to the original image. If you do this with a small high-pass kernel you do sharpening, if you use a large kernel (letting through more frequencies) you do detail enhancement. The Guided Filter is a clever method of avoiding halo artifacts which occur at sharp light-to-dark boundaries when you approach the problem with simple high-pass filters.

DCS
  • 3,354
  • 1
  • 24
  • 40
  • According to the paper, Guided Filter runs in ~12.5 FPS for 1 megapixel of grayscale image. Do you have another reference better performance, or would you consider that as "sufficiently fast for video"? – Yoav Oct 30 '14 at 19:23
  • 1
    There have been a whole bunch of optimizations for these types of filters, some explicitly for video. I don't remember the paper titles, but try to google for 'fast high-dimensional filter', 'fast bilateral filter', or 'bilateral filter for video'. The Bilateral Filter is the 'original' idea, for which the Guided Filter was a speedup. The optimizations can be pretty complex, though. I recall one SIGGRAPH paper by Microsoft Research which was pretty impressive, targeting video. – DCS Nov 05 '14 at 10:22
  • how about trying contrast limited adaptive histogram equalization? – Jeru Luke Dec 15 '16 at 10:07
1

If you have the whole range of colors, I suggest you decide how many pixels are you willing to "sacrifice" as there will be some saturation. You can still use the contrast equation (from your link)

newValue = 255 * (oldValue - minValue)/(maxValue - minValue)

The only difference is you manually decide minValue and maxValue. That way everything below minValue will be 0; similary for 255.

Upgrade - just decide on the percentage you are willing to use and automatically set the boundaries.

Edit - that "local" got me thinking about an "advanced" version. Maybe you could take a look at the histogram (with 256 bins) and see if there are some "holes". Lets say you only have values from 0 to 50 and from 205 to 255. You can then stretch the lower values to 100 and the upper down to 155. The only problem is worse contrast between the lower and the upper values.

Dedek Mraz
  • 814
  • 7
  • 14
  • Problem with this might be deciding minValue and maxValue. What if I have to stretch the contrast of an image taken during the day, then the min-max range changes. – Autonomous Mar 12 '13 at 23:54
  • From the equation in your answer, how can we decide a threshold above which values will start increasing and below that values will decrease? So in above equation, if `maxValue=150` and `minValue=20`, can we get the threshold without trial and error? Or in ohter words, can we predict the `minValue` and `maxValue` if we know the threshold? – Autonomous Mar 17 '13 at 02:12