G'MIC exercises

exercise

(http://4232.cf) #85

dont work. I go to go back to try

:frowning:

I think that I will be able to solve this with iflamate of inkscape


#86

Sorry about the edits. I didn’t know what I wanted to ask.

Question 1

Basically, I want to manipulate the luminance data but it seems that the three methods are different, especially luminance.

luminance_test:
  sp
  +l[0] ac "b 3",ycbcr_y rgb2ycbcr channels 0 endl
  +l[0] +b 3 blend luminance rgb2ycbcr channels 0 endl
  l[0] b 3 luminance endl

The difference is that the luminance method uses coefficients whereas the YCbCr methods use a matrix transform. Maybe I am wrong that they should be equivalent…

Question 2

I have included the scripts for luminance and rgb2ycbcr below.

#@cli luminance
#@cli : Compute luminance of selected sRGB images.
#@cli : $ image.jpg +luminance
luminance :
  e[^-1] "Compute luminance of image$?."
  v - remove_opacity srgb2rgb
  repeat $! l[$>]
  if {s==3} sh 0 sh[0] 1 sh[0] 2 *[1] 0.22248840 *[2] 0.71690369 *[3] 0.06060791 +[1-3] rm[1]
  elif {s!=1} norm n 0,255
  fi endl done
  channels 0 rgb2srgb v +

#@cli rgb2ycbcr
#@cli : Convert color representation of selected images from RGB to YCbCr.
#@cli : $ image.jpg rgb2ycbcr split c
rgb2ycbcr :
  e[^-1] "Convert color representation of image$? from RGB to YCbCr."
  v - mix_rgb 66,129,25,-38,-74,112,112,-94,-18 + 128 / 256
  repeat $!
    sh[$>] 0 +. 16 rm.
    sh[$>] 1,2 +. 128 rm.
  done v +

I would like to adapt both to reflect the luminance of Rec.2020 D50. I might be doing it wrong, so I need people to confirm. This is my version of luminance:

Y50_: skip ${1=0}
  sh 0 sh[0] 1 sh[0] 2
  if $1 *[1] 0.27904 *[2] 0.67535 *[3] 0.04561 # Rec.2020
  else *[1] 0.22249 *[2] 0.7169 *[3] 0.06061   # Rec.709
  fi
  +[1-3] rm[1] channels 0

My math isn’t good, so I settled for the values found in Elle’s Rec2020-elle-V4-rec709.icc and sRGB-elle-V4-srgbtrc.icc. Since I am working in linear gamma, I don’t need srgb2rgb and rgb2srgb. How do I do this to rgb2ycbcr and ycbcr2rgb? @jdc


(Desmis) #87

Hello @afre

I don’t know and I don’t use GMIC :slight_smile:

jacques


Garanti sans virus. www.avast.com


#88

Sorry, my last post was all over the place. I have pared it down to sanity levels. (After 6 edits. :blush:)

@jdc I pinged you regarding the second question because I think you may be able to help me figure out how to do the transformations between RGB and YCbCr.


(Desmis) #89

@afre
Sorry but I had not understand

For this trnasformation, there are many formulas which are all approximate, because we are in RGB, and therefore this does not take into account the workspace (sRGB, Prophoto,…)
Here the one I used in my adaptation of Auto White balance
“Robust automatic WB algorithm using grey colour points in Images” in the branch “autowblocal”
float Y0 = 0.299f * rl+ 0.587f * gl + 0.114f *bl;
float U0 = -0.14713f * rl - 0.28886f * gl + 0.436f * bl;
float V0 = 0.615f * rl - 0.51498f * gl- 0.10001f * bl;

But if you want a transformation good in all cases, you must use instead of YCbCr, xyY
You can make a transformation as the one

void Color::rgbxyY(float r, float g, float b, float &x, float &y, float &Y, const double xyz_rgb[3][3])
{
float xx = ((xyz_rgb[0][0] * r + xyz_rgb[0][1] * g + xyz_rgb[0][2] * b)) ;
float yy = ((xyz_rgb[1][0] * r + xyz_rgb[1][1] * g + xyz_rgb[1][2] * b)) ;
float zz = ((xyz_rgb[2][0] * r + xyz_rgb[2][1] * g + xyz_rgb[2][2] * b)) ;
float som = xx + yy + zz;
x = xx / som;
y = yy / som;
Y = zz / som;
}
Where xyz_rgb[3][3] is the transformation matrix associated to working space
At the end
x ==> red channel
y==> blue channel
Y==> Luminance
all is between 0 and 1, and allows CIE1931 diagram.

I used this transformation for example in “ItcWB” - Iterative temperature correlation white balance" (always in “autowblocal”) - for this procedure (Itcwb), I put a copyright

Jacques


(Desmis) #90

A small complement.
All RGB formulas correspond to the second row of the RGB / XYZ matrix
ex for Aces_p1 {0.284448, 0.671758 , 0.043794}
for sRGB {0.2225045, 0.7168786, 0.0606169}

The formula with 0.299f , 0.587f , 0.114f is a “median” formula often used to take into account all working space…but obviously, it’s never good :slight_smile:


#91

This is what I wanted to know. In other words, Y0 depends on the type of linear RGB being used. U0 and V0 stay the same. Is that correct?

PS According to Wikipedia, the coefficients for U and V differ as well. I guess I need to determine the matrices for Rec.709 and Rec.2020…

PPS @David_Tschumperle I noticed that YUV conversions have a division and multiplication by 255 while YIQ ones don’t. Is this a bug?


(Desmis) #92

It’s the same thing, the coefficients are average values.
The only way to have correct coefficients is to use the transformation xyY (or its derivatives Lab, …)

The values XYZ in the matrix are calculated from the primary working space and the white point. So they will be different for example for Rec2020 in D65 and Rec2020 in D50, and of course for Rec2020 and sRGB, etc.

You can find many matrix, in the branch “testoutputprofile” (RawTherapee) in the file iccmatrices.h

I will be away for about a week

jacques


(David Tschumperlé) #93

I don’t remember why but that’s the range that was decided for these colorspaces.
a YUV with [0,255] range is YCbCr instead.


(Desmis) #94

I am not at home.
But there is a mistake in
Y=zz/som
Must be Y=Y/65535.
Jacques


(http://4232.cf) #95

As I can see the code of “pack_sprites” in gmic?


(David Tschumperlé) #96

In file ‘https://raw.githubusercontent.com/dtschump/gmic/master/src/gmic_stdlib.gmic’, searching for the string pack_sprites : will show you the code of the corresponding command.


#97

Forget if I asked: what is the difference between .gmz, .cimg and .cimgz? I always assumed that z means compressed and that .gmz and .cimgz were the same…


(David Tschumperlé) #98

Yes, the z means the format is compressed (lossless compression).
Basically the .gmz format is equivalent to .cimgz but it also stores the names of the image in the list
(technically, a .gmz is actually a .cimgz with a last image encoding the image names).


#99

Going back to my original question on blurs, I could adapt blur_linear to be asymmetrical by masking out the part of the kernel that I don’t need. (I did that on the image but not the kernel in post #78.)

blurSE

Now, I just need to figure out how to chop the kernel at any angle.


I still need guidance on where to begin exploring de-blurring such a single-direction blur. I have a very unsteady hand when taking photos. However complex the camera shake is in 3d and temporal space, I often find that the blur is prominent in one direction.

Edit: I just noticed deconvolve_fft. That should work on blur_linear but not a single direction one.


#100

Is it possible to go from this 18.919999999999998 to this 18.92? If not, how about echoing only?


(David Tschumperlé) #101

Either by

value={round($value,0.01)}

or

value={_$value}

#102

I was looking for value={_$value}. How does it work? Can I control how it truncates?


(David Tschumperlé) #103

According to the documentation:

If a mathematical expression 
starts with an underscore '_', the resulting value is truncated to a readable format. 
For instance, item '{_pi}' is substituted by '3.14159' (while '{pi}' is substituted by 
'3.141592653589793'). 

Internally, this use printf("%g",value) to do the conversion, which means (taken from the man page of printf):

       g, G   The double argument is converted in style f or e (or F or E
              for G conversions).  The precision specifies the number of
              significant digits.  If the precision is missing, 6 digits are
              given; if the precision is zero, it is treated as 1.  Style e
              is used if the exponent from its conversion is less than -4 or
              greater than or equal to the precision.  Trailing zeros are
              removed from the fractional part of the result; a decimal
              point appears only if it is followed by at least one digit.

#104

I guess the underscore is enough for my purposes. I have a command that finds and outputs the optimum argument of another command; i.e., it makes the image fulfill a certain statistic (mean, median, SD, etc.). It is highly inefficient because I don’t have the programming or mathematical background. Do you have any suggestions on how to reduce the # of iterations and time in solving the problem?

PS @snibgo has IM scripts that find optimum values but I don’t understand the math / code.