Is it possible to reproduce Ylevels filter with ImageMagick?

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?".
Post Reply
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Is it possible to reproduce Ylevels filter with ImageMagick?

Post by konstantin »

Is it possible to reproduce avisynth's Ylevels filter with ImageMagick?
http://avisynth.nl/index.php/Ylevels
http://forum.doom9.org/showthread.php?t=79898

To make dark video screenshots brighter and visible.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by snibgo »

I can't easily figure out what the Ylevels filter does.

For lightening dark pixels, without also lightening light pixels, "+sigmoidal-contrast" with a midpoint above 50% is useful.

For example, "+sigmoidal-contrast 8,70%" gives this transfer curve:

Image
You could use this:

Code: Select all

convert rose: -colorspace YIQ -channel R +sigmoidal-contrast 8,70% +channel -colorspace sRGB s.png
snibgo's IM pages: im.snibgo.com
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by konstantin »

I don't understand exactly as well how it works, but the visual appearance of the videos are very nice with this Ylevels filter applied.
Last edited by konstantin on 2014-09-16T16:35:31-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by fmw42 »

It appears to be an IM -gamma function per comments at https://ffmpeg.org/pipermail/ffmpeg-use ... 20584.html. Though I suspect it is like IM -levels, that allows blackpoint, whitepoint and gamma adjustment, but on the Y channel of YCbCr).

Just use it in place of sigmoidal-contrast in snibgo's command which operates on the intensity channel (or use the L channel from LAB, or I from HSI). However, there are a number of similar non-linear operators, such as -evaluate log, -gamma, or a one sided -sigmoidal-contrast Vx0%.

I use the sigmoidal-contrast in my script sigmoidal, which tries to emulate Photoshops non-linear brightness-contrast.
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by konstantin »

Here you can see Ylevels filter in action:
Full resolution image: http://i.imgur.com/etjLWz7.jpg

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

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by fmw42 »

Do you have access to the control for it? If so, please provide a screen snap. From you images, it appears to be equivalent to IM -levels or just -gamma operating on the Y channel from YCbCr (or YIQ or YUV). But you can also try L from LAB (see my script enhancelab) or I from HSI. Just change snibgo's command to

Code: Select all

convert rose: -colorspace YCbCr -channel R -gamma gammaval +channel -colorspace sRGB s.png
or

Code: Select all

convert rose: -colorspace YCbCr -channel R -level bpt,wpt,gammaval +channel -colorspace sRGB s.png

see
http://www.imagemagick.org/script/comma ... .php#gamma
http://www.imagemagick.org/script/comma ... .php#level
http://www.imagemagick.org/Usage/color_mods/#level
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by konstantin »

Sorry, what does exactly mean: "control for it" and "screen snap"? Could you be more specific and verbose? (I am not a native english speaker), My avisynth file (yl.avs) looked like this when I generated the above image (one have to install MaskTools.dll and ylevels.avsi to the avisynth plugin directory, or load them manually with loadplugin("masktools.dll") and import("ylevels.avsi") ):

Code: Select all

ImageSource("000.jpg").trim(0,16)
w=last.width
h=last.height
cr=(4 - (w % 4) % 4)
cb=(2 - (h % 2) % 2)
AddBorders(0, 0, cr, cb, $000000)
converttoyv12()
Animate(last, 0,16, "ylevels", 0,1.0,255, 0, 255,0,2.5,255, 0, 255)
scriptClip( """ subtitle("Gamma applied:" + LeftStr(string(current_frame/10.0+1.0),3) )""")
#Ylevels(0,1.8,255, 0, 255) 
converttorgb24()
crop(0,0,-cr,-cb)
ImageWriter("ylevels",0,16,"jpg")
AddBorders(0, 0, cr, cb, $000000)
converttoyv12()
The last two lines are needed only when somebody is on Linux, and use avs2yuv.exe to run the script and render the images like this:

Code: Select all

wine avs2yuv.exe yl.avs - > /dev/null
fmw42 wrote:Do you have access to the control for it? If so, please provide a screen snap. From you images, it appears to be equivalent to IM -levels or just -gamma operating on the Y channel from YCbCr (or YIQ or YUV). But you can also try L from LAB (see my script enhancelab) or I from HSI. Just change snibgo's command to

Code: Select all

convert rose: -colorspace YCbCr -channel R -gamma gammaval +channel -colorspace sRGB s.png
or

Code: Select all

convert rose: -colorspace YCbCr -channel R -level bpt,wpt,gammaval +channel -colorspace sRGB s.png

see
http://www.imagemagick.org/script/comma ... .php#gamma
http://www.imagemagick.org/script/comma ... .php#level
http://www.imagemagick.org/Usage/color_mods/#level
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by fmw42 »

What I was asking was if you have the Avisynth program and when you run it, there is a graphical window for you to interact. When the window is open, can you capture the view of the window so we can see what the graphical controls look like.

Nevertheless, the line from your file has this

Code: Select all

Ylevels(0,1.8,255, 0, 255) 
This is the standard arguments for a level-like operation such as in Photoshop and GIMP.

It is likely

0 for input black point
1.8 for gamma
255 for input white point
0 for output white point
255 for output black point

In this sequence, the only argument that causes a change of the image is 1.8 for gamma (where gamma=1 would be no change).

So in IM, all you need do is some thing like

Code: Select all

convert inputimage -colorspace YCbCr -channel R -gamma 1.8 +channel -colorspace sRGB outputimage
If you want more control, then use

Code: Select all

convert inputimage -colorspace YCbCr -channel R -level X,1.8,Y +channel -colorspace sRGB outputimage
where X=0 by default but could be made larger and Y=255 by default but could be made smaller. The 1.8 can be varied to get different results as in your examples. X=input black point and Y=input white point.
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by konstantin »

Oh, I see. There is no graphical control window for this filter called Ylevels. It can be used only with function arguments.
I have tried the first command you mentioned, this is the result:

Image

Top image was generated with avisynth, bottom with ImageMagick. They reasonably differ, the bottom is a bit darker.
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by konstantin »

Btw, is there any way to automatically guess the right gamma value for an image?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by fmw42 »

konstantin wrote:Btw, is there any way to automatically guess the right gamma value for an image?
Not that I know. IM has an auto-gamma function that tries. I have a script, auto-gamma that is similar but allows you to set the mid gray level (IM used 0.5 i.e. 50%).

You can try the IM -auto-gamma where you have -gamma in your code. You can also try using other colorspaces, such as LAB.

If you are on Linux or Mac OS or Windows with Cygwin, you can try my script. I also have one that does white balance and brightness contrast along with auto-gamma. see my autotone.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by snibgo »

konstantin wrote:Btw, is there any way to automatically guess the right gamma value for an image?
I have discovered that when gamma is the only control, I always prefer the gamma that maximises the standard deviation of the image.

In my experience, this is always true for photographs (including video). It may not be true for more graphical images.

This is now part of my standard default processing of photographs. Video is more complex as we need a consistent gamma for all the frames in one shot, and the shots in a sequence need to have the same "look".

IM's in-built auto-gamma chooses the gamma that makes the mean value 50%. This generally doesn't maximise the SD, and doesn't give the "best" image.
snibgo's IM pages: im.snibgo.com
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by konstantin »

Do you mean that standard deviation which is listed by the command "identify -verbose"?

Code: Select all

 Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 390x527+0+0
  Resolution: 96x96
  Print size: 4.0625x5.48958
  Units: PixelsPerInch
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 205530
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 195.229 (0.765604)
      standard deviation: 78.3089 (0.307094)
      kurtosis: 0.368077
      skewness: -1.36697
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 135.378 (0.530892)
      standard deviation: 68.3243 (0.267938)
      kurtosis: -0.739838
      skewness: -0.537471
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 131.736 (0.516611)
      standard deviation: 66.8117 (0.262007)
      kurtosis: -0.720457
      skewness: -0.525522
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 154.114 (0.604369)
----------------------------------------------------------------------
+    standard deviation: 71.3309 (0.279729)       +
----------------------------------------------------------------------
      kurtosis: -0.0524484
      skewness: -0.678321
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 390x527+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 94
  Orientation: Undefined
  Properties:
    date:create: 2014-10-21T01:25:02+02:00
    date:modify: 2014-10-21T01:25:02+02:00
    jpeg:colorspace: 2
    jpeg:sampling-factor: 2x2,1x1,1x1
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by fmw42 »

I am pretty sure snibgo's SD is standard deviation
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is it possible to reproduce Ylevels filter with ImageMag

Post by snibgo »

Yes. In particular, a script finds the value of XX that maximises:

Code: Select all

convert in.miff -gamma XX -format "%%[fx:standard_deviation]\n" info:
My rule of thumb ("always choose the gamma that maximises the SD") is true for my photos, but not other people's. For example, applying this to a portrait where the face was low-contrast but the image also contained bright sky gave a horrible result. Cropping out the face, finding the gamma that maximised the SD of the face, then applying that gamma to the entire photo, was a big improvement.
snibgo's IM pages: im.snibgo.com
Post Reply