weird behavior with contrast-stretch

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

weird behavior with contrast-stretch

Post by HugoRune »

I came upon some weird behaior with -contrast-stretch, not sure if it is a bug:
ImageMagick 6.5.4-10 2009-08-08 Q16

This is the original image:
Image

convert AdditiveColor.jpg -contrast-stretch 0%,21% test.png
Image
That is ok. Some noise due to jpg compression got enhanced.

convert AdditiveColor.jpg -contrast-stretch 0%,22% test.png
Image
Where did the noise go? how did the image get darker overall?

edit: the shift happens between -contrast-stretch 0%,21.006944% and -contrast-stretch 0%,21.006945%
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: weird behavior with contrast-stretch

Post by fmw42 »

curious! try similar operations with -linear-stretch and see what happens? Anthony and I have been studying both for a long time and Anthony hopes to rewrite something later this year to have more functionality and work in a better manner.
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: weird behavior with contrast-stretch

Post by HugoRune »

fmw42 wrote:curious! try similar operations with -linear-stretch and see what happens? Anthony and I have been studying both for a long time and Anthony hopes to rewrite something later this year to have more functionality and work in a better manner.
I could not reproduce this behaviour with linear-stretch.
(Thought I am not sure whether I tried the right things, this operator confuses me somewhat)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: weird behavior with contrast-stretch

Post by fmw42 »

contrast-stretch 0 means to stretch from the min and max bins to full black and white. For other clip points (black-point and white-point) it will count how much is in the min and max bins plus what you tell it for black-point and white-point.

linear stretch 0 will make no change. However linear stretch 1x1 (not percent) will find the first bin with any count and stretch that to full black and white. It clips the amount you specify for black-point and white-point.

The default on contrast-stretch if you provide one value is to use that for the other value. So contrast-stretch 1% will be the same as contrast-stretch 1x1% and will clip 1% on each side (plus whatever is in the min and max bin).

The default for linear-stretch if you provide one value is to use the complement for the other value. (not a good way to do it). so -linear-stretch 1% is like -linear-stretch 1x99%.

contrast-stretch has some internal way of stretching the histogram bins and is channel sensitive

linear -stretch finds the corresponding bin graylevels for the black-point and white-point and passes those graylevels to -level to do the stretch.

At least this is the best estimate from testing that Anthony I have deduced.

linear-stretch seems to be perhaps better if you don't need to stretch each channel separately. It also allow -linear-stretch 0x1 that will only stretch the high side or -linear-stretch 1x0 that will only stretch the low side. However as said above -linear-stretch 0x0 is a null operation --- no change and thus may be of no importance.

This confusion is why Anthony has some intentions of reworking the functions that use the histogram and attempt to provide better options and the best of both functions or at least what we think might be the right way to do them.

This summary was posted a while back, but I could not find it to reference it. So I had to rewrite it.

Anthony and I are working to rewrite his documentation, but it is not compete yet. However, you can see what we have at http://www.imagemagick.org/Usage/color/#histogram
Post Reply