Ask Your Question

contrast-stretch with clipping

asked 2012-11-13 05:15:24 -0500

HugoRune gravatar image

updated 2012-11-13 07:10:16 -0500

To contrast-stretch an image I use

CvNormalize(img,img,0,255, NormType.MinMax);

This will set the minimum to 0 and the maximum to 255, stretching all values in between.

But is there a way to specify a percentage of values that should be cut off? If I want to stretch the image so that at least 5% of the resulting array are zeroes and 5% of the resulting array are 255,
(Or alternatively, stretch the image so that 5% of the lowest values and 5% of the highest values are clipped),
what combination of opencv commands could I use to accomplish that?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-11-13 07:39:12 -0500

unxnut gravatar image

Looks to me that the answer lies in histogram matching. You can create the histogram and modify it as per your specs. Then you can apply it to get the desired range. I can help you formulate the code.

edit flag offensive delete link more

answered 2012-11-14 04:36:40 -0500

HugoRune gravatar image

unxnut's suggestions is probably the right way to go at this, but I found a quick hack that gets me mostly what I want:

To stretch an image so the topmost few pixels are clipped:

  • cvErode srcImage n times --> erodedImg // this will remove all the highest localized peaks
  • cvMinMaxLoc erodedImg // this will get the maximum value
  • Scale srcImg * (255.0 / maxValue) // this will set the maximum value of the eroded image to 255 and clip all higher values

removing low peaks works the same, but was not needed in my case

edit flag offensive delete link more


It may be quick to write, but slow to run with all those calls to cvErode. It also quite problematic to decide about reasonable amount of erosions.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-11-14 07:02:23 -0500 )edit

The speed is not an issue compared to all the other stuff that needs to run, and this method happens to match well with the form of noise I am dealing with: There are a couple highly localized noise peaks, one or two pixels wide, that disturb normalization; and those are removed by erosion

HugoRune gravatar imageHugoRune ( 2012-11-14 17:48:33 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2012-11-13 05:15:24 -0500

Seen: 1,204 times

Last updated: Nov 14 '12