使用gamma校正提升jpg亮度

This commit is contained in:
nuknal
2024-05-30 11:22:34 +08:00
parent 7d9ec46750
commit 07ee4d88d4
18 changed files with 174 additions and 98 deletions

39
pkg/producer/fit.go Normal file
View File

@@ -0,0 +1,39 @@
package imageproc
import (
"gonum.org/v1/gonum/mat"
)
// PolynomialFit fits a polynomial of given degree to the data points (x, y).
func PolynomialFit(x, y []float64, degree int) ([]float64, error) {
n := len(x)
// Create the Vandermonde matrix
vander := mat.NewDense(n, degree+1, nil)
for i := 0; i < n; i++ {
for j := 0; j <= degree; j++ {
vander.Set(i, j, pow(x[i], j))
}
}
// Create the right-hand side vector
yVec := mat.NewVecDense(n, y)
// Solve the least squares problem
var qr mat.QR
qr.Factorize(vander)
coeffs := mat.NewDense(degree+1, 1, nil)
err := qr.SolveTo(coeffs, false, yVec)
return coeffs.RawMatrix().Data, err
}
// pow is a helper function to calculate power of a number.
func pow(x float64, n int) float64 {
if n == 0 {
return 1
}
res := x
for i := 1; i < n; i++ {
res *= x
}
return res
}