-contrast-stretch 0% for sequence of images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Posts: 15
Joined: 2018-12-13T10:56:29-07:00
Authentication code: 1152

-contrast-stretch 0% for sequence of images

Post 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

Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com

User avatar
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.

Posts: 15
Joined: 2018-12-13T10:56:29-07:00
Authentication code: 1152

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

Post 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 '\
' 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:

The mean image of the image sequence gave this result:

Code: Select all