134 lines
3.4 KiB
Go
134 lines
3.4 KiB
Go
package vision
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"math"
|
|
|
|
"github.com/disintegration/imaging"
|
|
"github.com/fogleman/gg"
|
|
)
|
|
|
|
// Resize 缩放画布到指定尺寸
|
|
func (c *Canvas) Resize(w, h int) {
|
|
resized := imaging.Resize(c.dc.Image(), w, h, imaging.Lanczos)
|
|
c.dc = gg.NewContextForImage(resized)
|
|
}
|
|
|
|
// Fit 缩放画布以适应指定尺寸,保持宽高比
|
|
func (c *Canvas) Fit(w, h int) {
|
|
resized := imaging.Fit(c.dc.Image(), w, h, imaging.Lanczos)
|
|
c.dc = gg.NewContextForImage(resized)
|
|
}
|
|
|
|
// Fill 缩放画布以填充指定尺寸,保持宽高比并裁剪
|
|
func (c *Canvas) Fill(w, h int) {
|
|
resized := imaging.Fill(c.dc.Image(), w, h, imaging.Center, imaging.Lanczos)
|
|
c.dc = gg.NewContextForImage(resized)
|
|
}
|
|
|
|
// Rotate 旋转画布
|
|
func (c *Canvas) Rotate(angle float64) {
|
|
rotated := imaging.Rotate(c.dc.Image(), angle, color.Transparent)
|
|
c.dc = gg.NewContextForImage(rotated)
|
|
}
|
|
|
|
// Blur 模糊处理
|
|
func (c *Canvas) Blur(sigma float64) {
|
|
blurred := imaging.Blur(c.dc.Image(), sigma)
|
|
c.dc = gg.NewContextForImage(blurred)
|
|
}
|
|
|
|
// Grayscale 转为灰度图
|
|
func (c *Canvas) Grayscale() {
|
|
gray := imaging.Grayscale(c.dc.Image())
|
|
c.dc = gg.NewContextForImage(gray)
|
|
}
|
|
|
|
// AdjustBrightness 调整亮度
|
|
func (c *Canvas) AdjustBrightness(percent float64) {
|
|
adjusted := imaging.AdjustBrightness(c.dc.Image(), percent)
|
|
c.dc = gg.NewContextForImage(adjusted)
|
|
}
|
|
|
|
// AdjustContrast 调整对比度
|
|
func (c *Canvas) AdjustContrast(percent float64) {
|
|
adjusted := imaging.AdjustContrast(c.dc.Image(), percent)
|
|
c.dc = gg.NewContextForImage(adjusted)
|
|
}
|
|
|
|
// AdjustSaturation 调整饱和度
|
|
func (c *Canvas) AdjustSaturation(percent float64) {
|
|
adjusted := imaging.AdjustSaturation(c.dc.Image(), percent)
|
|
c.dc = gg.NewContextForImage(adjusted)
|
|
}
|
|
|
|
// Sharpen 锐化
|
|
func (c *Canvas) Sharpen(sigma float64) {
|
|
sharpened := imaging.Sharpen(c.dc.Image(), sigma)
|
|
c.dc = gg.NewContextForImage(sharpened)
|
|
}
|
|
|
|
// Invert 反转颜色
|
|
func (c *Canvas) Invert() {
|
|
inverted := imaging.Invert(c.dc.Image())
|
|
c.dc = gg.NewContextForImage(inverted)
|
|
}
|
|
|
|
// FlipH 水平翻转
|
|
func (c *Canvas) FlipH() {
|
|
flipped := imaging.FlipH(c.dc.Image())
|
|
c.dc = gg.NewContextForImage(flipped)
|
|
}
|
|
|
|
// FlipV 垂直翻转
|
|
func (c *Canvas) FlipV() {
|
|
flipped := imaging.FlipV(c.dc.Image())
|
|
c.dc = gg.NewContextForImage(flipped)
|
|
}
|
|
|
|
// Convolve3x3 应用 3x3 卷积核
|
|
func (c *Canvas) Convolve3x3(kernel [9]float64) {
|
|
img := c.dc.Image()
|
|
bounds := img.Bounds()
|
|
w, h := bounds.Dx(), bounds.Dy()
|
|
result := image.NewRGBA(bounds)
|
|
|
|
var kernelSum float64
|
|
for _, v := range kernel {
|
|
kernelSum += v
|
|
}
|
|
if kernelSum == 0 { kernelSum = 1 }
|
|
|
|
for y := 0; y < h; y++ {
|
|
for x := 0; x < w; x++ {
|
|
var r, g, b, a float64
|
|
for ky := -1; ky <= 1; ky++ {
|
|
for kx := -1; kx <= 1; kx++ {
|
|
px := x + kx
|
|
py := y + ky
|
|
if px < 0 { px = 0 }
|
|
if px >= w { px = w - 1 }
|
|
if py < 0 { py = 0 }
|
|
if py >= h { py = h - 1 }
|
|
|
|
col := img.At(px, py)
|
|
cr, cg, cb, ca := col.RGBA()
|
|
k := kernel[(ky+1)*3+(kx+1)]
|
|
r += float64(cr>>8) * k
|
|
g += float64(cg>>8) * k
|
|
b += float64(cb>>8) * k
|
|
a += float64(ca>>8) * k
|
|
}
|
|
}
|
|
result.SetRGBA(x, y, color.RGBA{
|
|
R: uint8(math.Max(0, math.Min(255, r/kernelSum))),
|
|
G: uint8(math.Max(0, math.Min(255, g/kernelSum))),
|
|
B: uint8(math.Max(0, math.Min(255, b/kernelSum))),
|
|
A: uint8(math.Max(0, math.Min(255, a/kernelSum))),
|
|
})
|
|
}
|
|
}
|
|
c.dc = gg.NewContextForImage(result)
|
|
}
|