constrast enhancement for browser image

This commit is contained in:
nuknal
2024-07-17 12:45:29 +08:00
parent 63f995ccfe
commit cab03827f0
3 changed files with 164 additions and 13 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/airbusgeo/godal"
log "github.com/sirupsen/logrus"
"gocv.io/x/gocv"
"starwiz.cn/sjy01/image-proc/pkg/config"
)
func GTiffToJPG(ftiff, fjpg string, reversed bool) error {
@@ -58,12 +59,24 @@ func GTiffToJPG(ftiff, fjpg string, reversed bool) error {
channels := gocv.Split(img)
for i, ch := range channels {
// 2. 计算图像的最小值和最大值
minVal, maxVal, _, _ := gocv.MinMaxLoc(ch)
// 3. 将16位图像线性拉伸到8位图像
scale := 255.0 / (maxVal - minVal)
shift := -minVal * scale
ch.ConvertToWithParams(&channels[i], gocv.MatTypeCV8U, scale, shift)
switch config.GCONFIG.BrowserImg.Enhancement {
case NoEnhancement:
case StretchToMinimumMaximum:
minVal, maxVal, _, _ := gocv.MinMaxLoc(ch)
ce := NewContrastEnhancement(int(minVal), int(maxVal))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
value := int(uint16(ch.GetShortAt(y, x)))
value = ce.enhance(value)
ch.SetShortAt(y, x, int16(value))
}
}
case StretchToCumulativeCutMinMax:
channels[i] = cumulativeCountCutEnhancement(ch,
config.GCONFIG.BrowserImg.CumulativeCutLower,
config.GCONFIG.BrowserImg.CumulativeCutUpper)
ch.Close()
}
}
img8bit := gocv.NewMat()
@@ -82,14 +95,16 @@ func GTiffToJPG(ftiff, fjpg string, reversed bool) error {
image.Point{X: img8bit.Cols() / 2, Y: img8bit.Rows() / 2},
0, 0, gocv.InterpolationCubic)
gocv.IMWrite(fjpg, img8bit)
// 7. 应用伽玛校正提升亮度
gammaCorrected := applyGammaCorrection(img8bit, 1.6) // 伽玛值
defer gammaCorrected.Close()
ok := gocv.IMWriteWithParams(fjpg, gammaCorrected, []int{gocv.IMWriteJpegOptimize, 1})
if !ok {
err = fmt.Errorf("error saving %s", fjpg)
return err
}
// gammaCorrected := applyGammaCorrection(img8bit, 1.6) // 伽玛值
// defer gammaCorrected.Close()
// ok := gocv.IMWriteWithParams(fjpg, gammaCorrected, []int{gocv.IMWriteJpegOptimize, 1})
// if !ok {
// err = fmt.Errorf("error saving %s", fjpg)
// return err
// }
return nil
}