Page 1 of 1

-contrast-stretch 0% for sequence of images

Posted: 2019-10-26T05:09:13-07:00
by mviereck
I want to apply '-contrast-stretch 0%' on a sequence of similar images.
The automation of the special '0%' argument leads to more or less intensive changes for different images in the sequence.
I would like to have the same change on all images.

A possible attempt I think of:
- Generate a mean image with '-evaluate-sequence mean',
- Run 'convert mean.png -contrast-stretch 0% stretch.png'.
- Find out the difference between these images to get arguments for '-contrast-stretch A%xB%'
- Apply this on all images of the sequence.

Can you give me a hint how to accomplish this?
I've compared the output of 'identify' and see differences in min and max values of color channels. Probably I can use this to somehow calculate some percent values for -contrast-stretch. But maybe there is a more direct way.
Ideally, something like 'convert mean.png -contrast-stretch 0% info:' would give me the arguments.

In general, the same question comes up for '-auto-level' and '-auto-gamma', too.

Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101

Re: -contrast-stretch 0% for sequence of images

Posted: 2019-10-26T08:09:33-07:00
by snibgo
"-contrast-stretch 0%" does the same as "-channel RGB -auto-level +channel". That is, it applies a "-level A%,B%" to the red channel so its values are spread between 0 and 100%. A% is the minimum input red value, and B% is the maximum. And similarly for the green and blue channels.

I'm not sure what you want to do. Perhaps you want to apply the same "-level" to the red channel of each image, and likewise for the green and blue channels.

Re: -contrast-stretch 0% for sequence of images

Posted: 2019-10-26T10:58:47-07:00
by fmw42
Append all the images together. Then apply -contrast-stretch 0%. Then separate the result into individual images again. That is providing you have enough resources available if your images are very large.

Otherwise, process one image the way you want in Photoshop or GIMP or Imagemagick. Create a HALD image. Apply the same processing to the HALD image. Bring the processed HALD image back and use -hald-clut to apply it to all the images. See https://imagemagick.org/Usage/color_mods/#hald-clut

But note, as snibgo points out, if each image's min and max value are different, they likely need different processing as -contrast-stretch will stretch the min to black and the max to white. That may not be best for all your images.

Re: -contrast-stretch 0% for sequence of images

Posted: 2020-03-03T10:34:49-07:00
by mviereck
Thank you for your hints and ideas!

Meanwhile I have an attempt that seems to be pretty close, maybe you can confirm or correct it.
With "-evaluate-sequence mean" I generate a mean image of the image sequence that should get the same auto-level, auto-gamma or stretch-contrast 0% for each image.

From this mean image I get some values:

Code: Select all

Imageproperties="$(convert "$Meanimage" -format '\
Imagemean=%[mean]
Imagequantumrange=%[fx:quantumrange]
Imageautogamma=%[fx:log(mean)/log(0.5)]
Imageminlevel=%[fx:minima*100]
Imagemaxlevel=%[fx:maxima*100]
Imageminlevelred=%[fx:minima.r*100]
Imagemaxlevelred=%[fx:maxima.r*100]
Imageminlevelgreen=%[fx:minima.g*100]
Imagemaxlevelgreen=%[fx:maxima.g*100]
Imageminlevelblue=%[fx:minima.b*100]
Imagemaxlevelblue=%[fx:maxima.b*100]
' info:)"

eval $Imageproperties
With these values I found:
-auto-gamma is same as "-gamma $Imageautogamma"
-auto-level is same as "-level ${Imageminlevel}%x${Imagemaxlevel}%"
-contrast-stretch 0% is same as "-contrast-stretch ${Imageminlevel}x${Imagemaxlevel}"

I am applying this on all images of the sequence. I am aware that this might cause some information loss for some images in the sequence; but none of them is far of the mean image, so it should be no great harm.

Edit: An iconfied example of an image sequence:
Image

The mean image of the image sequence gave this result:

Code: Select all

Imagemean=219.04
Imagequantumrange=255
Imageautogamma=0.219302
Imageminlevel=58.0392
Imagemaxlevel=98.0392
Imageminlevelred=82.3529
Imagemaxlevelred=98.0392
Imageminlevelgreen=74.5098
Imagemaxlevelgreen=95.2941
Imageminlevelblue=58.0392
Imagemaxlevelblue=90.1961