Gradient desaturation?

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
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Gradient desaturation?

Post by olegk »

Is it possible to gradually desaturate color channels in an image?
E.g. I have a red-cyan anaglyph that shows over-saturation of cyan towards the left side and over-saturation of red towards the right side.
I'd like to extract the green+blue channels and desaturate them with something like min:max where min gives saturation level on the left of the image and max - on the right. And an opposite conversion for the red channel. Then combine of cause.

Your help is very appreciated.
Regards,
Oleg.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient desaturation?

Post by snibgo »

Changing saturation is easy: convert to HSL and manipulate the S (saturation) channel. This could be done with a gradient.

However, I'm not sure that's what you really want. For example, "desaturating the red channel" doesn't make sense. It is fully saturated, and will remain so.

Perhaps you want to change the values in the channels, making them lighter or darker. This should also be easy.

Can you provide a sample input with the required output?
snibgo's IM pages: im.snibgo.com
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: Gradient desaturation?

Post by olegk »

I would attach a sample input image, but seems like the forum infrastructure doesn't support that.
I _think_ gradual desaturation is what I want; cannot be 100% sure of course.
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: Gradient desaturation?

Post by olegk »

Ok, what I probably need is to scale one channel with scale factor increasing gradually from left to right, and do the opposite for the other two channels.
Of course you are right regarding desaturation of a single channel being nonsense.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient desaturation?

Post by snibgo »

Put an image somewhere like Dropbox.com and post the URL here.
snibgo's IM pages: im.snibgo.com
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: Gradient desaturation?

Post by olegk »

Thanks for the interest and for trying to help me!

So, here are the files (all these were 16-bit TIFFs converted to JPEG).
I'm trying single-lens-3D technique; you can take a look at my older thread (viewtopic.php?f=1&t=23930) for some background.

The original image - out of the RAW converter:
http://www.lazyconv.com/galleries/DEMO/ ... C02298.JPG

The half-cooked image after addition of the color-compensation mask; solves most of the color problem, but I'm looking for some further tuning:
http://www.lazyconv.com/galleries/DEMO/ ... 60g092.JPG

A shot of the white wall used to create a color-compensation mask; demonstrates the effect of two halves of the lens's back element being covered with red and cyan filters:
http://www.lazyconv.com/galleries/DEMO/ ... bright.JPG

The color-compensation mask for addition to original images:
http://www.lazyconv.com/galleries/DEMO/ ... bright.JPG


The mask-application command for MS-Windows (with self-explanative variables):
for %%f in (%ORIG_DIR%\*.TIF) do (
convert %%f %MASK_PATH_WEAK% -compose Plus -composite -modulate 100,60 -gamma 0.92 -depth %OUT_DEPTH% -compress LZW %DST_DIR%\%%~nf_s060g092.TIF
)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Gradient desaturation?

Post by fmw42 »

A shot of the white wall used to create a color-compensation mask; demonstrates the effect of two halves of the lens's back element being covered with red and cyan filters:
http://www.lazyconv.com/galleries/DEMO/ ... bright.JPG
What color was the wall? I think your color gradient is not quite the right colors.

What was the command you used to correct the raw file with the color gradient?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient desaturation?

Post by snibgo »

I don't understand what you need, but you might be able to work it out from this.

Code: Select all

set SRC=DSC02298_s060g092.JPG

FOR /F "usebackq" %%L IN (`identify -format "WW=%%w\nHH=%%h\nWWm1=%%w-1\nHHm1=%%h-1" %SRC%`) DO set /A %%L

convert -size %WW%x%HH% ^
  xc: ^
  -sparse-color bilinear ^
0,0,rgb(75%%,45%%,45%%),^
%WWm1%,0,rgb(45%%,75%%,75%%),^
%WWm1%,%HHm1%,rgb(45%%,75%%,75%%),^
0,%HHm1%,rgb(75%%,45%%,45%%) ^
  f.png

convert ^
  %SRC% ^
  f.png ^
  -compose Overlay -composite ^
  s2.png
The "FOR" loop sets four variables.

The first convert creates a mask, f.png. Colours are set at the four corners, starting top-left, clockwise. So the left side is rgb(75%%,45%%,45%%) and the right side is rgb(45%%,75%%,75%%). The colours change in a gradient. So, reading the image from left to right: red changes from 75% to 45% and cyan changes from 45% to 75%.

The second convert uses f.png to change the source image, channel by channel. Where the mask has a value 50%, image pixels are unchanged. Where the mask is > 50%, the image pixel is made lighter; and < 50% make the pixel darker.

The two convert commands could be made into one.

Doubtless the 45% and 75% values need changing.
snibgo's IM pages: im.snibgo.com
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: Gradient desaturation?

Post by olegk »

fmw42 wrote:
A shot of the white wall used to create a color-compensation mask; demonstrates the effect of two halves of the lens's back element being covered with red and cyan filters:
http://www.lazyconv.com/galleries/DEMO/ ... bright.JPG
What color was the wall? I think your color gradient is not quite the right colors.

What was the command you used to correct the raw file with the color gradient?
The wall is sufficiently white :). What made the center of the wall picture look "bezh" is white-balance being preset to "incandescent" in Aftershot Pro converter. This is more or less the light source I used.
Today I got an identical lens (but without my color filters on the back). Using it I tried to make a color-balanced picture of the same wall. E.g. I measured color balance with an unmodified lens (got 2500 M3), then applied the WB value to the shot by a lens with color filters (the source shot for correction image).
The results aren't worth bothering - the most visible change is an extreme correction on the left side; in most cases unwanted.

The commands split into two stages - first create (once per a lens) an addition image , then add it to each shot to correct the later.

##(1) Make a correction image. The RAW was "exposed-to-the-right" - max exposure possible without highlight clipping.
## The convertion of RAW for the correction image is "\DSC02218_16_max.tif".
convert ..\TIFF16\DSC02218_16_max.tif -strip -blur 0x8 -depth 16 -compress LZW ColorVign_16_bright.tif
convert ColorVign_16_bright.tif -gravity center -crop 100x100+0+0 -depth 16 -compress LZW crop100_16_bright.tif

convert crop100_16_bright.tif -blur 0x8 -depth 16 -compress LZW crop100_16_bright_blur.tif
identify ColorVign_16_bright.tif
# 4912x3264
convert crop100_16_bright_blur.tif -resize 4912x3264! -blur 0x8 -depth 16 -compress LZW CentralColor_16_bright.tif
convert CentralColor_16_bright.tif ColorVign_16_bright.tif -compose Minus_Src -composite -blur 0x8 -depth 16 -compress LZW ColorAdd_16_bright.tif

##(2) Use the correction image
for %f in (IN\*.TIF) do convert %f ColorAdd_16_bright.tif -compose Plus -composite -modulate 100,60 -gamma 0.92 -depth 8 -quality 90 OUT\%~nf_fxAdd_b_s060g092.JPG
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: Gradient desaturation?

Post by olegk »

[quote="snibgo"]I don't understand what you need, but you might be able to work it out from this.

Thanks! What you do is create the correction image artificially; sounds attractive.
But my original problem is still there. Look at the output from your script. Green on the left side is oversaturated, as is red on the right side. There are no blue subjects on the left side, otherwise I'd expect cyan to look oversaturted. I want to correct this oversaturation, but frankly I cannot formulate the problem properly.
Or maybe excessive saturation happens to green and magenta? I'm in doubt so far.

And there's another problem - f.png shows apparent vertical banding.

Again, thanks a lot! I keep learning ideas on what could be done with Imagemagick from such answers.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient desaturation?

Post by snibgo »

I don't see any banding in f.png.

I don't know what effect you want so I just guessed at the numbers. If the left side is too green on the left, you might lower those numbers. Etc.

Code: Select all

%IM%convert -size %WW%x%HH% ^
  xc: ^
  -sparse-color bilinear ^
0,0,rgb(75%%,25%%,45%%),^
%WWm1%,0,rgb(45%%,75%%,75%%),^
%WWm1%,%HHm1%,rgb(45%%,75%%,75%%),^
0,%HHm1%,rgb(75%%,25%%,45%%) ^
  f.png
snibgo's IM pages: im.snibgo.com
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: Gradient desaturation?

Post by olegk »

[quote="snibgo"]I don't see any banding in f.png.

Here is my version of f.png with pretty apparent vertical banding:
http://www.lazyconv.com/galleries/DEMO/ ... SL3D/f.png

Generated with the following command:
convert -size 4912x3264 xc: -sparse-color bilinear 0,0,rgb(57%,43%,43%),4911,0,rgb(43%,57%,57%),4911,3263,rgb(43%,57%,57%),0,3263,rgb(57%,43%,43%) f.png

>convert -version
Version: ImageMagick 6.7.1-6 2011-08-12 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP

I checked on two displays to be sure. Tried -blur 0x8 and -blur 0x20, but it's in wane - the strips are too wide and their brightness doesn't rise consistently.

And I need to emphasize that your suggestions are going to solve my problem; several iterations more, and it's done. So many thanks!

Oleg.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient desaturation?

Post by snibgo »

With your f.png, I can't see any banding on my usual screen. On another screen (a large TV), I can. I assume this is a quantisation effect, either in the TV or in how my computer translates the signal for the TV.

Displays may only use 8-bit samples. 43% to 57% is 14%, and 14% of 255 is 34. Do you see about 34 bands? If so, that's the problem, and nothing to worry about.

You are using an old version of IM. With v6.8.6-9, f.png is very slightly different, but not visibly so.
snibgo's IM pages: im.snibgo.com
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: Gradient desaturation?

Post by olegk »

snibgo wrote:With your f.png, I can't see any banding on my usual screen. On another screen (a large TV), I can. I assume this is a quantisation effect, either in the TV or in how my computer translates the signal for the TV.

Displays may only use 8-bit samples. 43% to 57% is 14%, and 14% of 255 is 34. Do you see about 34 bands? If so, that's the problem, and nothing to worry about.

You are using an old version of IM. With v6.8.6-9, f.png is very slightly different, but not visibly so.
I hope you are right. It's hard to count the bands, but there number is indeed around 30.
But then, I tried 49:51 gradient; the resulting f.png has less bands, but still much more than 5.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient desaturation?

Post by snibgo »

To see if f.png has a problem (and I really doubt that it has):

Code: Select all

convert f.png ( +clone -roll +20+0 ) -compose Difference -composite -crop +20+0 +repage fd.png
This finds the difference between f.png and f.png scrolled by 20 pixels, then chops off the side strip where there was wraparound. Choose any number you like. The result should be a constant very dark colour. Then:

Code: Select all

identify -verbose fd.png
I get this result:

Code: Select all

    Depth: 16-bit
  Channel depth:
    red: 16-bit
    green: 16-bit
    blue: 16-bit
Channel statistics:
    Red:
      min: 80 (0.00122072)
      max: 81 (0.00123598)
      mean: 80.0657 (0.00122172)
      standard deviation: 0.247724 (3.78003e-006)
      kurtosis: 10.2953
      skewness: 3.50647
    Green:
      min: 133 (0.00202945)
      max: 134 (0.00204471)
      mean: 133.444 (0.00203622)
      standard deviation: 0.496804 (7.58074e-006)
      kurtosis: -1.94836
      skewness: 0.227233
    Blue:
      min: 80 (0.00122072)
      max: 81 (0.00123598)
      mean: 80.0657 (0.00122172)
      standard deviation: 0.247724 (3.78003e-006)
      kurtosis: 10.2953
      skewness: 3.50647
The colour isn't constant, but varies by no more than 1 (out of 65535) in each channel. This is merely rounding error, and not visible.
snibgo's IM pages: im.snibgo.com
Post Reply