Hello, as I wrote some time ago, I wanted to decorrelate an IR scan and a visible light scan (red channel) of the same slide. This because the red channel leaks in the IR and I want to better isolate the dust and scratches before inpainting them.
The idea is to do a linear regression between red channel and IR channel, then to subtract the calculated red-contribution from the IR.
I used the simple formula from Wikipedia.
More specifically, I wanted to replicate this:
I tried first to replicate the final formula:
b= (avg(xy) - avg(x)*avg(y)) / (avg(x^2) - (avg(x))^2)
so I wrote in G’Mic ([-1] is IR, [-2] is red channel):
regression:
# y = IR
# x = original channel (red, luminance)
# y = a + bx
# b= (avg(xy) - avg(x)*avg(y)) / (avg(x^2) - (avg(x))^2)
# a= _y - a * _x
# xy
--mul[-2] [-1]
xy={-1,ia}
-rm[-1]
x={-2,ia}
y={-1,ia}
--sqr[-2]
xx={-1,ia}
-rm[-1]
# (avg(x))^2
x2={{-2,ia}*{-2,ia}}
_b={($xy-($x*$y))/($xx-$x2)}
_a={$y-$_b*$x}
then I thought about the first formula, more specifically the one with summatories:
b= (sum( (x_i-avg(x))*(y_i-avg(y)) )) / (sum( (x_i-x^2)^2) )
and I wrote this code ([-1] is IR, [-2] is red channel):
regression2:
# y = IR
# x = original channel (red, luminance)
# y = a + bx
# b=(sum( (x_i-avg(x))*(y_i-avg(y)) )) / (sum( (x_i-x^2)^2) )
# a=avg(y)-b*avg(x)
--sub[-2] {-2,ia}
--sub[-2] {-2,ia}
--mul[-2] [-1]
b1={-1,is}
-rm[-3,-2,-1]
--sqr[-2]
--sub[-3] {-1,ia}
b2={-1,is}
-rm[-2,-1]
_b={$b1/$b2}
_a={{-1,ia}-$_b*{-2,ia}}
The results are different and I’m not sure why. The first implementation is the one which gives me the best (basically perfect) results.
Could someone help me? I can continue anyway working but I’d like to understand the mistake.
Thanks