Golang中如何获取有效的Google图片/颜色搜索关键词

Golang中如何获取有效的Google图片/颜色搜索关键词 大家好! 抱歉又问了个新手问题。 我不仅是Go语言的新手,在图形、图像、颜色等编程方面也是新手。 目前,新信息简直多得让我应接不暇。 有没有一种简单的Go语言方法,可以将我查找表中的颜色滤镜应用到图像上?

目前,我只是逐个像素地循环并进行大量的插值计算。

3 回复

我在 https://pkg.go.dev/ 上搜索了 image filter - Search Results - Go Packages

是否有简单的方法,可以将我查找表中的颜色滤镜应用到图像上?

Gift 可能会有所帮助。

更多关于Golang中如何获取有效的Google图片/颜色搜索关键词的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


谢谢,我会研究一下那个包。 有很多包,例如 bild,它们都很棒,功能齐全。但我只想用我已经有的查找表来实现颜色校正。也许我错了,但难道不能只用标准库,比如 image/colorimage/color/palette 来实现吗?

在Go中处理图像颜色滤镜时,可以使用标准库imagecolor包来优化性能。以下是使用查找表(LUT)应用颜色滤镜的示例:

package main

import (
    "image"
    "image/color"
    "image/jpeg"
    "os"
)

// 创建颜色查找表(示例:增加红色通道)
func createRedBoostLUT() [256]uint32 {
    var lut [256]uint32
    for i := 0; i < 256; i++ {
        // 增强红色,限制在0-255范围内
        r := uint32(min(255, i+50))
        g := uint32(i)
        b := uint32(i)
        lut[i] = (r << 16) | (g << 8) | b
    }
    return lut
}

// 使用LUT应用滤镜
func applyLUT(img image.Image, lut [256]uint32) *image.RGBA {
    bounds := img.Bounds()
    dst := image.NewRGBA(bounds)
    
    for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
        for x := bounds.Min.X; x < bounds.Max.X; x++ {
            c := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
            
            // 使用查找表替换计算
            newColor := lut[c.R]
            
            dst.Set(x, y, color.RGBA{
                R: uint8(newColor >> 16),
                G: uint8(newColor >> 8),
                B: uint8(newColor),
                A: c.A,
            })
        }
    }
    return dst
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}

func main() {
    // 读取图像
    file, _ := os.Open("input.jpg")
    defer file.Close()
    
    img, _ := jpeg.Decode(file)
    
    // 创建和应用LUT
    lut := createRedBoostLUT()
    result := applyLUT(img, lut)
    
    // 保存结果
    out, _ := os.Create("output.jpg")
    defer out.Close()
    
    jpeg.Encode(out, result, &jpeg.Options{Quality: 90})
}

对于更复杂的颜色变换,可以使用三维LUT:

// 3D LUT示例(简化版)
type LUT3D struct {
    Size    int
    Data    []color.RGBA
}

func NewLUT3D(size int) *LUT3D {
    return &LUT3D{
        Size: size,
        Data: make([]color.RGBA, size*size*size),
    }
}

func (l *LUT3D) Apply(img image.Image) *image.RGBA {
    bounds := img.Bounds()
    dst := image.NewRGBA(bounds)
    scale := float64(l.Size-1) / 255.0
    
    for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
        for x := bounds.Min.X; x < bounds.Max.X; x++ {
            c := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
            
            // 计算3D LUT索引
            rIdx := int(float64(c.R) * scale)
            gIdx := int(float64(c.G) * scale)
            bIdx := int(float64(c.B) * scale)
            
            idx := (rIdx*l.Size*l.Size) + (gIdx*l.Size) + bIdx
            dst.Set(x, y, l.Data[idx])
        }
    }
    return dst
}

使用并行处理加速:

import "sync"

func applyLUTParallel(img image.Image, lut [256]uint32) *image.RGBA {
    bounds := img.Bounds()
    dst := image.NewRGBA(bounds)
    
    var wg sync.WaitGroup
    numWorkers := 4
    height := bounds.Dy() / numWorkers
    
    for w := 0; w < numWorkers; w++ {
        wg.Add(1)
        go func(worker int) {
            defer wg.Done()
            
            startY := bounds.Min.Y + height*worker
            endY := startY + height
            if worker == numWorkers-1 {
                endY = bounds.Max.Y
            }
            
            for y := startY; y < endY; y++ {
                for x := bounds.Min.X; x < bounds.Max.X; x++ {
                    c := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
                    newColor := lut[c.R]
                    
                    dst.Set(x, y, color.RGBA{
                        R: uint8(newColor >> 16),
                        G: uint8(newColor >> 8),
                        B: uint8(newColor),
                        A: c.A,
                    })
                }
            }
        }(w)
    }
    
    wg.Wait()
    return dst
}

这些方法通过预计算颜色变换到查找表中,避免了对每个像素进行重复的插值计算,显著提高了处理速度。

回到顶部