I compared fer DCTL Calibration tools and also ACR, and seems they all optimized somehow in own way. Even ACR/Lightroom Camera Calibration have 5 versions, but i am not sure what exactly they changed there.
There is a very nice Matrix Manipulator tool form thatcherfreeman free utility-dctls pack
https://github.com/thatcherfreeman/utility-dctls
Here is code from DCTL which may help to understand overall logic. From my subjective tests Cilindrical model works better than HSV. In HSV model when i adjust R/G/B color checker patches to match original profile, additional Yellow patch always looks slightly incorrect. But in Cilindrical mode all patches match perfectly.
matrix.jpg
Code: Alles auswählen
#line 2
// clang-format off
DEFINE_UI_PARAMS(red_hue, Red Hue, DCTLUI_SLIDER_FLOAT, 0, -60.0, 60.0, 0.1)
DEFINE_UI_PARAMS(red_sat, Red Sat, DCTLUI_SLIDER_FLOAT, 1, 0.0, 3.0, 0.1)
DEFINE_UI_PARAMS(green_hue, Green Hue, DCTLUI_SLIDER_FLOAT, 0, -60.0, 60.0, 0.1)
DEFINE_UI_PARAMS(green_sat, Green Sat, DCTLUI_SLIDER_FLOAT, 1, 0.0, 3.0, 0.1)
DEFINE_UI_PARAMS(blue_hue, Blue Hue, DCTLUI_SLIDER_FLOAT, 0, -60.0, 60.0, 0.1)
DEFINE_UI_PARAMS(blue_sat, Blue Sat, DCTLUI_SLIDER_FLOAT, 1, 0.0, 3.0, 0.1)
DEFINE_UI_PARAMS(model, Model, DCTLUI_COMBO_BOX, 0, {MODEL_HSV, MODEL_CYLINDRICAL}, {HSV, Cylindrical})
DEFINE_UI_PARAMS(invert, Invert, DCTLUI_CHECK_BOX, 0)
// clang-format on
#define PI (3.1415926535897932384626f)
__DEVICE__ float3 mv_33_3(float mat[3][3], float3 v) {
// matrix * vector multiplication
float3 out = make_float3(mat[0][0] * v.x + mat[0][1] * v.y + mat[0][2] * v.z,
mat[1][0] * v.x + mat[1][1] * v.y + mat[1][2] * v.z,
mat[2][0] * v.x + mat[2][1] * v.y + mat[2][2] * v.z);
return out;
}
__DEVICE__ void copy_mat_33(float from[3][3], float to[3][3]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
to[i][j] = from[i][j];
}
}
}
__DEVICE__ void mat_inverse_33(float dest[3][3], float m[3][3]) {
// from https://ardoris.wordpress.com/2008/07/18/general-formula-for-the-inverse-of-a-3x3-matrix/
float det = m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) +
m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
dest[0][0] = (m[1][1] * m[2][2] - m[1][2] * m[2][1]) / det;
dest[0][1] = (m[0][2] * m[2][1] - m[0][1] * m[2][2]) / det;
dest[0][2] = (m[0][1] * m[1][2] - m[0][2] * m[1][1]) / det;
dest[1][0] = (m[1][2] * m[2][0] - m[1][0] * m[2][2]) / det;
dest[1][1] = (m[0][0] * m[2][2] - m[0][2] * m[2][0]) / det;
dest[1][2] = (m[0][2] * m[1][0] - m[0][0] * m[1][2]) / det;
dest[2][0] = (m[1][0] * m[2][1] - m[1][1] * m[2][0]) / det;
dest[2][1] = (m[0][1] * m[2][0] - m[0][0] * m[2][1]) / det;
dest[2][2] = (m[0][0] * m[1][1] - m[0][1] * m[1][0]) / det;
}
__DEVICE__ float mod(float x, float y) {
if (x < 0) {
return y + _fmod(x, y);
} else {
return _fmod(x, y);
}
}
__DEVICE__ float3 cylindrical_to_rgb(float3 hsv) {
// Adapted from Juan Pablo Zambrano's code
float3 lsh = make_float3(hsv.z, hsv.y * 3.0f, mod(hsv.x / 360.0f, 1.0f) * 2.0f * PI);
float ctr = _fabs(lsh.x) * lsh.y * _cosf(lsh.z);
float ctg = lsh.x;
float ctb = _fabs(lsh.x) * lsh.y * _sinf(lsh.z);
float3 c = make_float3(ctr, ctg, ctb);
float mat[3][3] = {
{2.0f / 3.0f, 1.0f, 0.0f},
{-1.0f / 3.0f, 1.0f, _sqrtf(3.0f) / 3.0f},
{-1.0f / 3.0f, 1.0f, -_sqrtf(3.0f) / 3.0f},
};
return mv_33_3(mat, c);
}
__DEVICE__ float3 rgb_to_cylindrical(float3 x) {
// Matrix aligns and scales achromatic white (1, 1, 1) with (0, 1, 0)
// Also rotates input (1, 0, 0) above the red axis and ensures that
// (1, 0, 0) input ends up having a red channel code value of (1).
float mat[3][3] = {
{1.0f, -1.0f / 2.0f, -1.0f / 2.0f},
{1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f},
{0.0f, _sqrtf(3.0f) / 2.0f, -_sqrtf(3.0f) / 2.0f},
};
float3 rotated_color = mv_33_3(mat, x);
float l = rotated_color.y;
float3 hsl = make_float3(0.0f, 0.0f, 0.0f);
float3 normalized_color = rotated_color / _fabs(l);
if (l == 0.0f) {
hsl = make_float3(0.0f, 0.0f, l);
} else {
float theta = _atan2f(normalized_color.z, normalized_color.x);
hsl.x = mod(theta, 2.0f * PI) / (2.0f * PI) * 360.0f;
hsl.y = _hypotf(normalized_color.x, normalized_color.z) / 3.0f;
hsl.z = l;
}
return hsl;
}
__DEVICE__ float3 hsv_to_rgb(float3 hsv) {
float hue = hsv.x;
float sat = hsv.y;
float val = hsv.z;
hue = _fmod(hue + 360.0f, 360.0f);
float c = val * sat;
float x = c * (1.0f - _fabs(_fmod(hue / 60.0f, 2.0f) - 1.0f));
float m = val - c;
float3 rgbp;
if (0.0f <= hue && hue < 60.0f) {
rgbp = make_float3(c, x, 0.0f);
} else if (60.0f <= hue && hue < 120.0f) {
rgbp = make_float3(x, c, 0.0f);
} else if (120.0f <= hue && hue < 180.0f) {
rgbp = make_float3(0.0f, c, x);
} else if (180.0f <= hue && hue < 240.0f) {
rgbp = make_float3(0.0f, x, c);
} else if (240.0f <= hue && hue < 300.0f) {
rgbp = make_float3(x, 0.0f, c);
} else if (300.0f < hue && hue < 360.0f) {
rgbp = make_float3(c, 0.0f, x);
}
return rgbp + m;
}
__DEVICE__ float3 rgb_to_hsv(float3 rgb) {
float r = rgb.x;
float g = rgb.y;
float b = rgb.z;
float c_max = _fmaxf(_fmaxf(r, g), b);
float c_min = _fminf(_fminf(r, g), b);
float delta = c_max - c_min;
float H;
if (delta == 0.0f) {
H = 0.0f;
} else if (r >= g && r >= b) {
H = _fmod((g - b) / delta + 6.0f, (6.0f));
} else if (g >= r && g >= b) {
H = (b - r) / delta + (2.0f);
} else {
// b >= r && b >= g
H = (r - g) / delta + 4.0f;
}
H = H / 6.0f;
float S;
if (c_max == 0.0f) {
S = 0.0f;
} else {
S = delta / c_max;
}
float V = c_max;
S = _clampf(S, 0.0f, 1.0f);
float3 color = make_float3(H * 360.0f, S, V);
return color;
}
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, float p_R, float p_G, float p_B) {
float3 curr_color = make_float3(p_R, p_G, p_B);
// requirements:
// pure red in -> red hue/sat out
// same with green/blue
// pure white in -> pure white out
float3 red_hsv, green_hsv, blue_hsv;
if (model == MODEL_HSV) {
red_hsv = rgb_to_hsv(make_float3(1.0f, 0.0f, 0.0f));
green_hsv = rgb_to_hsv(make_float3(0.0f, 1.0f, 0.0f));
blue_hsv = rgb_to_hsv(make_float3(0.0f, 0.0f, 1.0f));
} else if (model == MODEL_CYLINDRICAL) {
red_hsv = rgb_to_cylindrical(make_float3(1.0f, 0.0f, 0.0f));
green_hsv = rgb_to_cylindrical(make_float3(0.0f, 1.0f, 0.0f));
blue_hsv = rgb_to_cylindrical(make_float3(0.0f, 0.0f, 1.0f));
}
red_hsv.x += red_hue;
red_hsv.y *= red_sat;
green_hsv.x += green_hue;
green_hsv.y *= green_sat;
blue_hsv.x += blue_hue;
blue_hsv.y *= blue_sat;
float3 red_target_rgb, green_target_rgb, blue_target_rgb;
if (model == MODEL_HSV) {
red_target_rgb = hsv_to_rgb(red_hsv);
green_target_rgb = hsv_to_rgb(green_hsv);
blue_target_rgb = hsv_to_rgb(blue_hsv);
} else if (model == MODEL_CYLINDRICAL) {
red_target_rgb = cylindrical_to_rgb(red_hsv);
green_target_rgb = cylindrical_to_rgb(green_hsv);
blue_target_rgb = cylindrical_to_rgb(blue_hsv);
}
float mat[3][3] = {
{red_target_rgb.x, green_target_rgb.x, blue_target_rgb.x},
{red_target_rgb.y, green_target_rgb.y, blue_target_rgb.y},
{red_target_rgb.z, green_target_rgb.z, blue_target_rgb.z},
};
// set these so that all rows sum to 1.0.
float inv[3][3] = {{0.0f}};
mat_inverse_33(inv, mat);
float3 gains = mv_33_3(inv, make_float3(1.0f, 1.0f, 1.0f));
float3 output;
if (invert) {
output = mv_33_3(inv, curr_color) / gains;
} else {
output = mv_33_3(mat, curr_color * gains);
}
return output;
}
Also here is a code from Crosstalk Correction DCTL:
crosstalk.jpg
Code: Alles auswählen
DEFINE_UI_PARAMS(p_GtoR, Green in Red, DCTLUI_SLIDER_FLOAT, 0.0, -1.0, 1.0, 0.001)
DEFINE_UI_PARAMS(p_BtoR, Blue in Red, DCTLUI_SLIDER_FLOAT, 0.0, -1.0, 1.0, 0.001)
DEFINE_UI_PARAMS(p_RtoG, Red in Green, DCTLUI_SLIDER_FLOAT, 0.0, -1.0, 1.0, 0.001)
DEFINE_UI_PARAMS(p_BtoG, Blue in Green, DCTLUI_SLIDER_FLOAT, 0.0, -1.0, 1.0, 0.001)
DEFINE_UI_PARAMS(p_RtoB, Red in Blue, DCTLUI_SLIDER_FLOAT, 0.0, -1.0, 1.0, 0.001)
DEFINE_UI_PARAMS(p_GtoB, Green in Blue, DCTLUI_SLIDER_FLOAT, 0.0, -1.0, 1.0, 0.001)
DEFINE_UI_PARAMS(p_SAT, Saturation, DCTLUI_SLIDER_FLOAT, 1.0, 0.0, 2.0, 0.001)
DEFINE_UI_PARAMS(GLOBAL_SAT, Subtractive Sat, DCTLUI_SLIDER_FLOAT, 0.0, -1.0, 1.0, 0.001)
DEFINE_UI_PARAMS(LIMITER_SAT, Deep Sat, DCTLUI_SLIDER_FLOAT, 0.0, 0.0, 1.0, 0.001)
DEFINE_UI_PARAMS(p_hue_rotation, Hue, DCTLUI_SLIDER_FLOAT, 0.0, -30.0, 30.0, 0.001)
DEFINE_UI_PARAMS(p_preserve_luminance_strength, Preserve Luminance, DCTLUI_SLIDER_FLOAT, 1.0, 0.0, 1.0, 0.001)
DEFINE_UI_PARAMS(p_preserve_luminance, Preserve Luminance, DCTLUI_CHECK_BOX, 1)
DEFINE_UI_PARAMS(p_blend_luminosity, Luminosity Mode, DCTLUI_CHECK_BOX, 0)
__DEVICE__ float lum709(float3 c) {
return c.x * 0.2126f + c.y * 0.7152f + c.z * 0.0722f;
}
__DEVICE__ void preserve_mat_luminance(float M[3][3]) {
const float3 lum_coeff = make_float3(0.2126f, 0.7152f, 0.0722f);
float colLum0 = lum_coeff.x * M[0][0] + lum_coeff.y * M[1][0] + lum_coeff.z * M[2][0];
float colLum1 = lum_coeff.x * M[0][1] + lum_coeff.y * M[1][1] + lum_coeff.z * M[2][1];
float colLum2 = lum_coeff.x * M[0][2] + lum_coeff.y * M[1][2] + lum_coeff.z * M[2][2];
float diff0 = lum_coeff.x - colLum0;
float diff1 = lum_coeff.y - colLum1;
float diff2 = lum_coeff.z - colLum2;
M[0][0] += diff0; M[1][0] += diff0; M[2][0] += diff0;
M[0][1] += diff1; M[1][1] += diff1; M[2][1] += diff1;
M[0][2] += diff2; M[1][2] += diff2; M[2][2] += diff2;
}
__DEVICE__ float3 adjustSat(float3 inColor, float sat) {
float lum = inColor.x * 0.2126f + inColor.y * 0.7152f + inColor.z * 0.0722f;
return _mix(make_float3(lum, lum, lum), inColor, sat);
}
__DEVICE__ float clampVal(float x, float lower, float upper) {
return _fmaxf(lower, _fminf(upper, x));
}
__DEVICE__ float SmootherStep(float edge0, float edge1, float x) {
x = clampVal((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
return x*x*x*(x*(x*6.0f - 15.0f) + 10.0f);
}
__DEVICE__ float Limiter(float val, float limiter) {
return SmootherStep(0.0f, limiter, val);
}
__DEVICE__ float RGB2Sat(float3 rgb) {
float minVal = _fminf(_fminf(rgb.x, rgb.y), rgb.z);
float maxVal = _fmaxf(_fmaxf(rgb.x, rgb.y), rgb.z);
if (maxVal == 0.0f) return 0.0f;
return (maxVal - minVal) / maxVal;
}
__DEVICE__ float3 rgb_to_spherical(float3 rgb) {
const float rtr = rgb.x * 0.81649658f + rgb.y * -0.40824829f + rgb.z * -0.40824829f;
const float rtg = rgb.x * 0.0f + rgb.y * 0.70710678f + rgb.z * -0.70710678f;
const float rtb = rgb.x * 0.57735027f + rgb.y * 0.57735027f + rgb.z * 0.57735027f;
const float art = _atan2f(rtg, rtr);
const float sphr = _sqrtf(rtr*rtr + rtg*rtg + rtb*rtb);
const float spht = art < 0.0f ? art + 2.0f * 3.141592653589f : art;
const float sphp = _atan2f(_sqrtf(rtr*rtr + rtg*rtg), rtb);
return make_float3(sphr * 0.5773502691896258f,
spht * 0.15915494309189535f,
sphp * 1.0467733744265997f);
}
__DEVICE__ float3 spherical_to_rgb(float3 sph) {
sph.x *= 1.7320508075688772f;
sph.y *= 6.283185307179586f;
sph.z *= 0.9553166181245093f;
const float ctr = sph.x * _sinf(sph.z) * _cosf(sph.y);
const float ctg = sph.x * _sinf(sph.z) * _sinf(sph.y);
const float ctb = sph.x * _cosf(sph.z);
return make_float3(
ctr * 0.81649658f + ctg * 0.0f + ctb * 0.57735027f,
ctr * -0.40824829f + ctg * 0.70710678f + ctb * 0.57735027f,
ctr * -0.40824829f + ctg * -0.70710678f+ ctb * 0.57735027f
);
}
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, float p_R, float p_G, float p_B) {
float3 color = make_float3(_clampf(p_R, 0.0f, 1.0f),
_clampf(p_G, 0.0f, 1.0f),
_clampf(p_B, 0.0f, 1.0f));
float3 in_RGB = color;
if (p_blend_luminosity) {
float3 outLum;
float GtoR = p_GtoR * 2.2f;
float BtoR = p_BtoR * 2.2f;
float RtoG = p_RtoG * 0.8f;
float BtoG = p_BtoG * 0.8f;
float RtoB = p_RtoB * 6.2f;
float GtoB = p_GtoB * 6.2f;
outLum.x = in_RGB.y * GtoR + in_RGB.z * BtoR + in_RGB.x * (1.0f - GtoR - BtoR);
outLum.y = in_RGB.x * RtoG + in_RGB.z * BtoG + in_RGB.y * (1.0f - RtoG - BtoG);
outLum.z = in_RGB.x * RtoB + in_RGB.y * GtoB + in_RGB.z * (1.0f - RtoB - GtoB);
in_RGB = in_RGB + (lum709(outLum) - lum709(in_RGB));
}
else {
float originalM[3][3] = {
{ 1.0f - p_GtoR - p_BtoR, p_GtoR, p_BtoR },
{ p_RtoG, 1.0f - p_RtoG - p_BtoG, p_BtoG },
{ p_RtoB, p_GtoB, 1.0f - p_RtoB - p_GtoB }
};
float preservedM[3][3] = {
{ originalM[0][0], originalM[0][1], originalM[0][2] },
{ originalM[1][0], originalM[1][1], originalM[1][2] },
{ originalM[2][0], originalM[2][1], originalM[2][2] }
};
if (p_preserve_luminance) {
preserve_mat_luminance(preservedM);
}
float strength = p_preserve_luminance ? p_preserve_luminance_strength : 0.0f;
float M[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
M[i][j] = originalM[i][j] + strength * (preservedM[i][j] - originalM[i][j]);
}
}
in_RGB = make_float3(
M[0][0]*in_RGB.x + M[0][1]*in_RGB.y + M[0][2]*in_RGB.z,
M[1][0]*in_RGB.x + M[1][1]*in_RGB.y + M[1][2]*in_RGB.z,
M[2][0]*in_RGB.x + M[2][1]*in_RGB.y + M[2][2]*in_RGB.z
);
}
float3 colorSat = adjustSat(in_RGB, p_SAT);
float3 sph = rgb_to_spherical(colorSat);
sph.y = sph.y - (p_hue_rotation * (1.0f/360.0f));
sph.y = sph.y - _floorf(sph.y);
float3 huer = spherical_to_rgb(sph);
float3 rgbIn = huer;
float alphaSat = 1.0f;
if (LIMITER_SAT > 0.0f) {
float sat = RGB2Sat(rgbIn);
alphaSat = Limiter(sat, LIMITER_SAT);
}
float alphaDens = 1.0f;
float cyn_dens = -(0.0f * alphaDens * 0.5f);
float mag_dens = -(0.0f * alphaDens * 0.5f);
float yel_dens = -(0.0f * alphaDens * 0.5f);
float red_dens = -(0.0f * alphaDens);
float grn_dens = -(0.0f * alphaDens);
float blu_dens = -(0.0f * alphaDens);
float red_r = red_dens + 1.0f;
float red_g = red_dens - (GLOBAL_SAT * alphaSat);
float red_b = red_dens - (GLOBAL_SAT * alphaSat);
float grn_r = grn_dens - (GLOBAL_SAT * alphaSat);
float grn_g = grn_dens + 1.0f;
float grn_b = grn_dens - (GLOBAL_SAT * alphaSat);
float blu_r = blu_dens - (GLOBAL_SAT * alphaSat);
float blu_g = blu_dens - (GLOBAL_SAT * alphaSat);
float blu_b = blu_dens + 1.0f;
float cyn_r = cyn_dens - (GLOBAL_SAT * alphaSat);
float cyn_g = cyn_dens + 1.0f;
float cyn_b = cyn_dens + 1.0f;
float mag_r = mag_dens + 1.0f;
float mag_g = mag_dens - (GLOBAL_SAT * alphaSat);
float mag_b = mag_dens + 1.0f;
float yel_r = yel_dens + 1.0f;
float yel_g = yel_dens + 1.0f;
float yel_b = yel_dens - (GLOBAL_SAT * alphaSat);
float3 blk = make_float3(0.0f, 0.0f, 0.0f);
float3 wht = make_float3(1.0f, 1.0f, 1.0f);
float3 red = make_float3(red_r, red_g, red_b);
float3 grn = make_float3(grn_r, grn_g, grn_b);
float3 blu = make_float3(blu_r, blu_g, blu_b);
float3 cyn = make_float3(cyn_r, cyn_g, cyn_b);
float3 mag = make_float3(mag_r, mag_g, mag_b);
float3 yel = make_float3(yel_r, yel_g, yel_b);
float3 out = rgbIn;
if (rgbIn.x > rgbIn.y) {
if (rgbIn.y > rgbIn.z) {
out = rgbIn.x * (red - blk) + blk + rgbIn.y * (yel - red) + rgbIn.z * (wht - yel);
} else if (rgbIn.x > rgbIn.z) {
out = rgbIn.x * (red - blk) + blk + rgbIn.y * (wht - mag) + rgbIn.z * (mag - red);
} else {
out = rgbIn.x * (mag - blu) + rgbIn.y * (wht - mag) + rgbIn.z * (blu - blk) + blk;
}
} else {
if (rgbIn.z > rgbIn.y) {
out = rgbIn.x * (wht - cyn) + rgbIn.y * (cyn - blu) + rgbIn.z * (blu - blk) + blk;
} else if (rgbIn.z > rgbIn.x) {
out = rgbIn.x * (wht - cyn) + rgbIn.y * (grn - blk) + blk + rgbIn.z * (cyn - grn);
} else {
out = rgbIn.x * (yel - grn) + rgbIn.y * (grn - blk) + blk + rgbIn.z * (wht - yel);
}
}
return out;
}
Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.