Page 1 of 1

contrast stretch problems

Posted: 2010-08-15T03:33:27-07:00
by markmarques
After some fiddling with -contrast-stretch option I realized that even with values 0 it clips some information specially in the highlights...

I tried using negative values but with no success ...
let me explain my idea....
i have a 16bits raw image where most of the information ( usually falls into the lower levels) ...
If I apply a gamma correction it does not distributes the color very well ( everything becomes white) ...
If I apply the linear-strech (as the documents in usage report it does nothing for the non-existing values...) , so no pratical use...
If I apply contrast-stretch 0 it works as intended (most of the time ) but in some rare occasions it "clips out" the values in the highlights ...
Any idea to avoid such behaviour ?

How can I define an error margin or percentage to avoid clipping?
I tried with negative values but apparently ( no error whatsoever) the option were ignored as it as never being called ...

Do I need somekind of script (Like those from FRED like "redist" or similar in order to achieve what I need ?

Re: contrast stretch problems

Posted: 2010-08-15T12:03:50-07:00
by fmw42
For linear stretch between min and max values to black and white with no clipping, use -auto-level rather than -contrast-stretch 0 (which does clip a little)

Re: contrast stretch problems

Posted: 2010-08-15T20:54:26-07:00
by anthony
-contrast-stretch uses a binning method to find the color range. As such it clips to bin values.

Re: contrast stretch problems

Posted: 2010-08-18T09:59:13-07:00
by markmarques
I am using the "convert -auto-level" option with success ...

Nonetheless is there any way to define "manually" the histogram value ?

For instance :
-auto-level gives me : low 5: high 205 ( IM 8)
But I needed low 0: high 215 ...

Is it possible to define those values manually ?
Thanks in advance

Re: contrast stretch problems

Posted: 2010-08-18T10:45:12-07:00
by fmw42
For instance :
-auto-level gives me : low 5: high 205 ( IM
But I needed low 0: high 215 ...
auto-level will produce black (0) for min value in image and white (QuantumRange) for max value in image using global stats (from all channels together). Thus you may get this result as only the channel with the min or max value will be changed to black and white.

If you need each channel to go to black and white, then add -channel rgb before -auto-level so that it stretches each channel separately. But this will produce some color balance changes to your image.

Currently there is no way to specify the resulting min and max value other than 0 and QuantumRange with -auto-level.

However if you know the min and max current values and the desired min and max values, you can compute the coefficients a,b (for aX+b) to apply to your image using -function polynomial "a,b" which will do what you need. see http://www.imagemagick.org/Usage/transf ... polynomial

I could help further (with actual a and b coeffs) if you provide the image's current min and max values and the desired min and max values.

Re: contrast stretch problems

Posted: 2010-08-18T20:41:00-07:00
by anthony
markmarques wrote:I am using the "convert -auto-level" option with success ...

Nonetheless is there any way to define "manually" the histogram value ?

For instance :
-auto-level gives me : low 5: high 205 ( IM 8)
But I needed low 0: high 215 ...

Is it possible to define those values manually ?
Thanks in advance
Yes it is called -level!
-level 0,215


See IM Examples, Color Modifications, Level
http://www.imagemagick.org/Usage/color/#level

NOTE at this time -auto-level is not documented in IM examples, basically as I added as a quick add, but then never got time to add other 'features' (though in included code hooks for them).

See my future proposals page...
http://www.imagemagick.org/Usage/bugs/f ... auto-level

Essentually the whole 'morphology' family of operators were added instead.
(And that is not quite complete yet either, but close to it).

Re: contrast stretch problems

Posted: 2010-08-18T20:58:26-07:00
by fmw42
Anthony wrote:Yes it is called -level!
-level 0,215
This would make full white into gray level 215. But I think he wants to go the other way per his statement:
But I needed low 0: high 215 ...
So, I believe you mean +level and then it is only true if his original image contained full white and he wanted it to go to 215 (on Q8 IM). I am not sure what range of values his original image contains.

The only way I know to go from any arbitrary range to some other arbitrary range would be to use -function polynomial.

I am certainly confused. So I think the poster really needs to clarify exactly what he is trying to do and what values he has and what values he desires to have for his image.

Re: contrast stretch problems

Posted: 2010-08-19T15:42:27-07:00
by KonfuseKitty
Haven't we been here before? Isn't the answer to use "-channel rgb", and "-contrast-stretch" with a very small percentage value? On my 15Mp files, I use 0x0.00001%. I've done a lot of tests and can't see any clipping beyond maybe a handful of pixels, difficult to notice.

It's also worth adding that the non-clipping percentage won't always give the optimum colour balance. In my script I first use 0x0.1%, which clips noticeably, then compose the non-clipped version over the highlights. This way I get most of the correct colour but no clipping.

Re: contrast stretch problems

Posted: 2010-08-19T15:53:42-07:00
by fmw42
KonfuseKitty wrote:Haven't we been here before? Isn't the answer to use "-channel rgb", and "-contrast-stretch" with a very small percentage value? On my 15Mp files, I use 0x0.00001%. I've done a lot of tests and can't see any clipping beyond maybe a handful of pixels, difficult to notice.

It's also worth adding that the non-clipping percentage won't always give the optimum colour balance. In my script I first use 0x0.1%, which clips noticeably, then compose the non-clipped version over the highlights. This way I get most of the correct colour but no clipping.

The problem here is that it is not clear whether the poster wanted to stretch the image to full black/white or whether he wanted to change the min and max values to something different from his original image BUT not full black and white.

Re: contrast stretch problems

Posted: 2010-08-19T17:33:21-07:00
by anthony
KonfuseKitty wrote:Haven't we been here before? Isn't the answer to use "-channel rgb", and "-contrast-stretch" with a very small percentage value? On my 15Mp files, I use 0x0.00001%. I've done a lot of tests and can't see any clipping beyond maybe a handful of pixels, difficult to notice.

It's also worth adding that the non-clipping percentage won't always give the optimum colour balance. In my script I first use 0x0.1%, which clips noticeably, then compose the non-clipped version over the highlights. This way I get most of the correct colour but no clipping.
Remember -contrast-stretch collects all the colors into a histogram bin (generally 1024 bins) it then counts the percentage pixels in to find which 'bin' to stretch. As such ALL the pixels in that bin (or beyond) will be made maximum/minimum.

-auto-level works differentially. It scans the image simply looking for just the highest and lowest values and then makes those values pure maximum or minimum. Whether each channel is expanded separately, or together (depending on the -channel Sync flag, on by default) makes no difference. No value will be clipped, though at least one value will be set to zero and one to MaxRGB (also known as QuantumRange).

Remember their is a difference between a value being clipped (and for not HDRI IM being set to 0 or MaxRGB) and a value just being set to 0 or MaxRGB (regardless of if the IM is HDRI). Comparing the floating point values of HDRI results verses the interger non-HDRI results will tell you if a specific color value is being clipped or not.

Re: contrast stretch problems

Posted: 2010-09-15T07:41:18-07:00
by markmarques
AFter some tests I still do not have what I needed ...
Depending on the file "-auto-level" works as it supposed ... With no clipping ...

If I try "-contrast-stretch" with small ammounts like 0.001% I get color posterization (even with IM 16) ...
The "-linear-stretch" does work somehow but also gives color posterization ...
Sometimes "-normalize" does work correctly but it varies from file to file ...

What can I do ?
someone pointed out using -level 0, X ( but X is always different ) ...
Any ideas ?

Re: contrast stretch problems

Posted: 2010-09-15T10:24:31-07:00
by fmw42
I am still confused about exactly what you want to do. Perhaps you can post a link to an example image and tell us what is wrong and what you would like to happen. Or show a resulting image that works the way you want or that does not work in some way and explain why. Also show your command line.