imageutil

package
v0.0.0-...-336c678 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RotateAngle90 旋转角度90°
	RotateAngle90 = 90
	// RotateAngle180 旋转角度180°
	RotateAngle180 = 180
	// RotateAngle270 转角度270°
	RotateAngle270 = 270
)
View Source
const (
	// FlipModeHorizontal 水平翻转(左右镜像)
	FlipModeHorizontal = "horizontal"
	// FlipModeVertical 垂直翻转(上下镜像)
	FlipModeVertical = "vertical"
)

Variables

This section is empty.

Functions

func AHash

func AHash(img image.Image) uint64

AHash 平均哈希

func AdjustBrightness

func AdjustBrightness(src image.Image, brightness float64) image.Image

AdjustBrightness 图片亮度调整

Params:

src: 源图片
brightness: 亮度调整值

Example:

AdjustBrightness(img, 50)

func AdjustBrightnessFile

func AdjustBrightnessFile(srcFile, dstFile string, brightness float64) error

AdjustBrightnessFile 图片文件亮度调整

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
brightness: 亮度调整值

func Binarize

func Binarize(src image.Image, threshold uint8) image.Image

Binarize 图片二值化

Params:

src: 源图片
threshold: 阈值,推荐为 128

func BinarizeFile

func BinarizeFile(srcFile, dstFile string, threshold uint8) error

BinarizeFile 图片文件二值化

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
threshold: 阈值,推荐为 128

func Compression

func Compression(srcFile, dstFile string, quality int) error

Compression 图片压缩

Params:

srcFile: 源图片路径
dstFile: 目标图片路径
quality: 压缩质量,范围 1-100(值越低,压缩率越高,质量越低)

func ConvexHull

func ConvexHull(points []image.Point) []image.Point

ConvexHull 计算凸包,基于 Jarvis 步进算法

func Crop

func Crop(src image.Image, cropRect image.Rectangle) (image.Image, error)

Crop 图片裁剪

Params:

src: 源图片
cropRect: 裁剪区域

func CropFile

func CropFile(srcFile, dstFile string, cropRect image.Rectangle) error

CropFile 图片文件裁剪

Params:

srcFile: 源图片路径
dstFile: 目标图片路径
cropRect: 裁剪区域

func DHash

func DHash(img image.Image) uint64

DHash 差异哈希

func Dilate

func Dilate(src image.Image, se StructuringElement) image.Image

Dilate 图片膨胀

Params:

src: 源图片
se: 结构元素

Example:

Dilate(img, NewRectKernel(3, 3))

func DilateFile

func DilateFile(srcFile, dstFile string, se StructuringElement) error

DilateFile 图片文件膨胀

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
se: 结构元素

func DrawFilledCircle

func DrawFilledCircle(dst draw.Image, center image.Point, radius int, c color.Color)

DrawFilledCircle 绘制填充的圆形

Params:

dst: 目标图片
center: 圆心坐标
radius: 圆的半径
c: 填充颜色

func DrawFilledPolygon

func DrawFilledPolygon(dst draw.Image, points []image.Point, c color.Color)

DrawFilledPolygon 多边形填充,使用扫描线算法填充多边形

Params:

dst: 目标图片
points: 顶点集合
c: 颜色

func DrawFilledRect

func DrawFilledRect(dst draw.Image, r image.Rectangle, c color.Color)

DrawFilledRect 矩形填充

Params:

dst: 目标图片
r: 要绘制的矩形区域
c: 颜色

func DrawLine

func DrawLine(dst draw.Image, p1, p2 image.Point, color color.Color)

DrawLine 绘制直线,基于布雷森汉姆(Bresenham)直线算法

Params:

dst: 目标图片
p1, p2: 直线的起点和终点
color: 颜色

func DrawPolygonOutline

func DrawPolygonOutline(dst draw.Image, points []image.Point, c color.Color)

DrawPolygonOutline 绘制多边形边框

Params:

dst: 目标图片
points: 顶点集合
c: 颜色

func DrawRectOutline

func DrawRectOutline(dst draw.Image, r image.Rectangle, c color.Color)

DrawRectOutline 绘制矩形边框

Params:

dst: 目标图片,必须是可变的(例如 *image.RGBA)
r: 要绘制的矩形区域
c: 颜色

func DrawThickLine

func DrawThickLine(dst draw.Image, p1, p2 image.Point, thickness int, c color.Color)

DrawThickLine 绘制粗线

Params:

dst: 目标图片
p1, p2: 直线的起点和终点
thickness: 线宽
c: 颜色

func DrawThickPolygonOutline

func DrawThickPolygonOutline(dst draw.Image, points []image.Point, thickness int, c color.Color)

DrawThickPolygonOutline 绘制粗多边形边框

Params:

dst: 目标图片
points: 顶点集合
thickness: 边框的粗细,像素
c: 颜色

func DrawThickRectOutline

func DrawThickRectOutline(dst draw.Image, r image.Rectangle, c color.Color, thickness int)

DrawThickRectOutline 绘制粗矩形边框

Params:

dst: 目标图片
r: 要绘制的矩形区域
c: 颜色
thickness: 边框的粗细,像素

func EqualizeHist

func EqualizeHist(src image.Image) image.Image

EqualizeHist 直方图均衡化

公式 s_k = round( (cdf(i) - cdf_min) / (Dx × Dy - cdf_min) × 255 )

Params:

src: 源图片

func EqualizeHistFile

func EqualizeHistFile(srcFile, dstFile string) error

EqualizeHistFile 图片文件直方图均衡化

Params:

srcFile: 源图片文件
dstFile: 目标图片文件

func Erode

func Erode(src image.Image, se StructuringElement) image.Image

Erode 图片腐蚀

Params:

src: 源图片
se: 结构元素

Example:

Erode(img, NewRectKernel(3, 3))

func ErodeFile

func ErodeFile(srcFile, dstFile string, se StructuringElement) error

ErodeFile 图片文件腐蚀

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
se: 结构元素

func Flip

func Flip(src image.Image, mode string) (image.Image, error)

Flip 翻转图片(水平或垂直)

Params:

src: 源图片
mode: 翻转模式

Example:

Flip(img, FlipModeHorizontal) // 水平翻转

func FlipFile

func FlipFile(srcFile, dstFile string, mode string) error

FlipFile 翻转图片文件(水平或垂直)

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
mode: 翻转模式

func GaussianBlur

func GaussianBlur(src image.Image, radius int, sigma float64) image.Image

GaussianBlur 图片高斯模糊

  • 高斯核半径和标准差越大,越模糊
  • 建议 radius > 3*sigma
  • 小值(radius=1-3, sigma=0.5-1.0):轻微模糊,适合细微效果
  • 中等值(radius=3-5, sigma=1.0-2.0):明显模糊,适合背景虚化
  • 大值(radius>5, sigma>2.0):强烈模糊,适合艺术效果,但计算慢

Params:

src: 源图片
radius: 高斯核半径
sigma: 高斯核标准差

Example:

GaussianBlur(img, 3, 1.0)

func GaussianBlurFile

func GaussianBlurFile(srcFile, dstFile string, radius int, sigma float64) error

GaussianBlurFile 图片文件高斯模糊

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
radius: 高斯核半径
sigma: 高斯核标准差

Example:

GaussianBlurFile("test.png", "test_gaussian.png", 3, 1.0)

func GenerateCaptcha

func GenerateCaptcha(text string) (image.Image, error)

GenerateCaptcha 验证码图片生成(目前只支持数字)

Params:

text: 验证码文本

Example:

// image.Image to bytes
img, _ := GenerateCaptcha("5679")
var buf bytes.Buffer
err := png.Encode(&buf, img)
if err != nil {
	t.Fatal("Error encoding image:", err)
}
buf.Bytes()

func GenerateSolid

func GenerateSolid(width, height int, c color.Color) image.Image

GenerateSolid 生成指定宽高的纯色背景图片

Params:

width: 图片宽度
height: 图片高度
c: 填充的颜色

Example:

GenerateSolid(800, 600, color.RGBA{255, 255, 255, 255})

func Grayscale

func Grayscale(src image.Image) *image.Gray

Grayscale 图片灰度化

Params:

src: 源图片

func GrayscaleFile

func GrayscaleFile(srcFile, dstFile string) error

GrayscaleFile 图片文件灰度化

Params:

srcFile: 源图片文件
dstFile: 目标图片文件

func Invert

func Invert(src image.Image) image.Image

Invert 图片反转颜色

Params:

src: 源图片

func InvertFile

func InvertFile(srcFile, dstFile string) error

InvertFile 图片文件反转颜色

Params:

srcFile: 源图片文件
dstFile: 目标图片文件

func MedianBlur

func MedianBlur(src image.Image, radius int) image.Image

MedianBlur 图片中值滤波,常用于处理椒盐噪声

Params

src: 源图像
radius: 滤波半径,radius=1 表示 3x3 窗口, radius=2 表示 5x5 窗口

func MedianBlurFile

func MedianBlurFile(srcFile, dstFile string, radius int) error

MedianBlurFile 图片文件中值滤波

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
radius: 滤波半径,radius=1 表示 3x3 窗口, radius=2 表示 5x5 窗口

func MorphologyClose

func MorphologyClose(src image.Image, se StructuringElement) image.Image

MorphologyClose 闭运算,先膨胀,再腐蚀

Params:

src: 源图片
se: 结构元素

Example:

MorphologyClose(img, NewRectKernel(3, 3))

func MorphologyCloseFile

func MorphologyCloseFile(srcFile, dstFile string, se StructuringElement) error

MorphologyCloseFile 对图片文件进行闭运算

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
se: 结构元素

func MorphologyOpen

func MorphologyOpen(src image.Image, se StructuringElement) image.Image

MorphologyOpen 开运算,先腐蚀,再膨胀

Params:

src: 源图片
se: 结构元素

Example:

MorphologyOpen(img, NewRectKernel(3, 3))

func MorphologyOpenFile

func MorphologyOpenFile(srcFile, dstFile string, se StructuringElement) error

MorphologyOpenFile 对图片文件进行开运算

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
se: 结构元素

func OffsetPolygon

func OffsetPolygon(points []image.Point, margin float64) []image.Point

OffsetPolygon 多边形偏移 (内缩/外扩)

  • margin > 0: 外扩
  • margin < 0: 内缩

Params:

points: 输入的顶点列表
margin: 内外缩的距离

func Open

func Open(imagePath string) (image.Image, error)

Open 打开图片

Params:

imagePath: 图片路径

func OtsuThreshold

func OtsuThreshold(gray *image.Gray) uint8

OtsuThreshold 基于大津法计算推荐阈值

通过类间方差:wB × wF × (mB - mF)² 计算出区分度最大的阈值

Params:

gray: 灰度图片

func Overlay

func Overlay(base, overlay image.Image, x, y int) image.Image

Overlay 图片叠加,将 overlay 图片叠加到 base 图片的指定位置 (x, y),支持透明通道 (alpha blending)

Params:

base: 基础图片
overlay: 叠加图片
x: 基础图片的 X 坐标
y: 基础图片的 Y 坐标

Example:

Overlay(base, overlay, 100, 100) // 将 overlay 图片叠加到 base 图片的 (100, 100) 位置

func OverlayFile

func OverlayFile(baseFile, overlayFile, dstFile string, x, y int) error

OverlayFile 图片文件叠加,将 overlay 图片文件叠加到 base 图片文件指定位置 (x, y),支持透明通道 (alpha blending)

Params:

baseFile: 基础图片文件
overlayFile: 叠加图片文件
dstFile: 目标图片文件
x: 基础图片的 X 坐标
y: 基础图片的 Y 坐标

func PHash

func PHash(img image.Image) uint64

PHash 感知哈希

func Resize

func Resize(src image.Image, newWidth, newHeight int) image.Image

Resize 图片缩放

  • 如果 newWidth > 0 && newHeight == 0:按比例基于宽度缩放
  • 如果 newWidth == 0 && newHeight > 0:按比例基于高度缩放
  • 如果 newWidth > 0 && newHeight > 0:固定宽高缩放(可能扭曲)
  • 如果两者均为 0:返回原图

Params:

src: 源图片
newWidth: 新宽度
newHeight: 新高度

func ResizeFile

func ResizeFile(srcFile, dstFile string, newWidth, newHeight int) error

ResizeFile 图片文件缩放

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
newWidth: 新宽度
newHeight: 新高度

func Rotate

func Rotate(src image.Image, angle int) (image.Image, error)

Rotate 旋转图片(顺时针 90°、180°、270°)

Params:

src: 源图片
angle: 旋转角度

Example:

Rotate(img, RotateAngle90) // 旋转90°

func RotateFile

func RotateFile(srcFile, dstFile string, angle int) error

RotateFile 旋转图片文件(顺时针 90°、180°、270°)

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
angle: 旋转角度

func Save

func Save(imagePath string, img image.Image, quality int) error

Save 保存图片

Params:

imagePath: 图片路径
img: 图片
quality: 压缩质量,范围 1-100(值越低,压缩率越高,质量越低)

func SimplifyPath

func SimplifyPath(points []image.Point, epsilon float64) []image.Point

SimplifyPath 简化路径,使用 Ramer-Douglas-Peucker (RDP) 算法进行简化

Params:

points: 输入的顶点列表
epsilon: 阈值 (点到线段的距离),值越大,简化程度越高

func Sobel

func Sobel(src image.Image, threshold float64) *image.Gray

Sobel 索贝尔边缘检测

Sobel 算子说明:

  • 使用 3x3 Gx/Gy 卷积核分别计算水平与垂直方向梯度
  • 边缘强度 edge = sqrt(gx^2 + gy^2)

Params:

src: 源图片
threshold: 阈值 [0, 1442] 推荐值:400

func SobelFile

func SobelFile(srcFile, dstFile string, threshold float64) error

SobelFile 图片文件索贝尔边缘检测

Params:

srcFile: 源图片文件
dstFile: 目标图片文件
threshold: 阈值 [0, 1442] 推荐值:400

Types

type Blob

type Blob struct {
	ID       int
	Points   []image.Point   // 原始像素点集合
	Area     int             // 面积
	Bounds   image.Rectangle // 正外接矩形 (AABB)
	Centroid image.Point     // 质心
}

Blob 存储单个连通区域的特征

type BlobResult

type BlobResult struct {
	Blobs  []Blob
	Width  int
	Height int
}

BlobResult 处理后的所有结果

func FindBlobs

func FindBlobs(img image.Image, threshold ...uint8) *BlobResult

FindBlobs 查找 Mask 图片的连通区域

Params:

img: 输入的图片
threshold: 像素值大于此值被视为前景,默认:127

type SizeReply

type SizeReply struct {
	Width  int // 图片宽
	Height int // 图片高
}

func Size

func Size(imagePath string) (*SizeReply, error)

Size 图片尺寸 说明:当图片类型不是标准库提供的,需要导入扩展库中的image golang.org/x/image

Params:

imagePath: 图片路径

type StructuringElement

type StructuringElement struct {
	// Kernel 形状, true 表示该像素在核的范围内
	Kernel [][]bool
	// Anchor 是核的中心点(锚点)
	Anchor image.Point
}

StructuringElement 结构元素

func NewCrossKernel

func NewCrossKernel(size int) StructuringElement

NewCrossKernel 创建十字形核

Params:

size: 核的尺寸,为保证中心对称,最好使用奇数

func NewRectKernel

func NewRectKernel(width, height int) StructuringElement

NewRectKernel 创建矩形核

Params:

width, height: 核的宽高,为保证中心对称,最好使用奇数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL