Documentation
¶
Index ¶
- Constants
- func AHash(img image.Image) uint64
- func AdjustBrightness(src image.Image, brightness float64) image.Image
- func AdjustBrightnessFile(srcFile, dstFile string, brightness float64) error
- func Binarize(src image.Image, threshold uint8) image.Image
- func BinarizeFile(srcFile, dstFile string, threshold uint8) error
- func Compression(srcFile, dstFile string, quality int) error
- func ConvexHull(points []image.Point) []image.Point
- func Crop(src image.Image, cropRect image.Rectangle) (image.Image, error)
- func CropFile(srcFile, dstFile string, cropRect image.Rectangle) error
- func DHash(img image.Image) uint64
- func Dilate(src image.Image, se StructuringElement) image.Image
- func DilateFile(srcFile, dstFile string, se StructuringElement) error
- func DrawFilledCircle(dst draw.Image, center image.Point, radius int, c color.Color)
- func DrawFilledPolygon(dst draw.Image, points []image.Point, c color.Color)
- func DrawFilledRect(dst draw.Image, r image.Rectangle, c color.Color)
- func DrawLine(dst draw.Image, p1, p2 image.Point, color color.Color)
- func DrawPolygonOutline(dst draw.Image, points []image.Point, c color.Color)
- func DrawRectOutline(dst draw.Image, r image.Rectangle, c color.Color)
- func DrawThickLine(dst draw.Image, p1, p2 image.Point, thickness int, c color.Color)
- func DrawThickPolygonOutline(dst draw.Image, points []image.Point, thickness int, c color.Color)
- func DrawThickRectOutline(dst draw.Image, r image.Rectangle, c color.Color, thickness int)
- func EqualizeHist(src image.Image) image.Image
- func EqualizeHistFile(srcFile, dstFile string) error
- func Erode(src image.Image, se StructuringElement) image.Image
- func ErodeFile(srcFile, dstFile string, se StructuringElement) error
- func Flip(src image.Image, mode string) (image.Image, error)
- func FlipFile(srcFile, dstFile string, mode string) error
- func GaussianBlur(src image.Image, radius int, sigma float64) image.Image
- func GaussianBlurFile(srcFile, dstFile string, radius int, sigma float64) error
- func GenerateCaptcha(text string) (image.Image, error)
- func GenerateSolid(width, height int, c color.Color) image.Image
- func Grayscale(src image.Image) *image.Gray
- func GrayscaleFile(srcFile, dstFile string) error
- func Invert(src image.Image) image.Image
- func InvertFile(srcFile, dstFile string) error
- func MedianBlur(src image.Image, radius int) image.Image
- func MedianBlurFile(srcFile, dstFile string, radius int) error
- func MorphologyClose(src image.Image, se StructuringElement) image.Image
- func MorphologyCloseFile(srcFile, dstFile string, se StructuringElement) error
- func MorphologyOpen(src image.Image, se StructuringElement) image.Image
- func MorphologyOpenFile(srcFile, dstFile string, se StructuringElement) error
- func OffsetPolygon(points []image.Point, margin float64) []image.Point
- func Open(imagePath string) (image.Image, error)
- func OtsuThreshold(gray *image.Gray) uint8
- func Overlay(base, overlay image.Image, x, y int) image.Image
- func OverlayFile(baseFile, overlayFile, dstFile string, x, y int) error
- func PHash(img image.Image) uint64
- func Resize(src image.Image, newWidth, newHeight int) image.Image
- func ResizeFile(srcFile, dstFile string, newWidth, newHeight int) error
- func Rotate(src image.Image, angle int) (image.Image, error)
- func RotateFile(srcFile, dstFile string, angle int) error
- func Save(imagePath string, img image.Image, quality int) error
- func SimplifyPath(points []image.Point, epsilon float64) []image.Point
- func Sobel(src image.Image, threshold float64) *image.Gray
- func SobelFile(srcFile, dstFile string, threshold float64) error
- type Blob
- type BlobResult
- type SizeReply
- type StructuringElement
Constants ¶
const ( // RotateAngle90 旋转角度90° RotateAngle90 = 90 // RotateAngle180 旋转角度180° RotateAngle180 = 180 // RotateAngle270 转角度270° RotateAngle270 = 270 )
const ( // FlipModeHorizontal 水平翻转(左右镜像) FlipModeHorizontal = "horizontal" // FlipModeVertical 垂直翻转(上下镜像) FlipModeVertical = "vertical" )
Variables ¶
This section is empty.
Functions ¶
func AdjustBrightness ¶
AdjustBrightness 图片亮度调整
Params: ¶
src: 源图片 brightness: 亮度调整值
Example: ¶
AdjustBrightness(img, 50)
func AdjustBrightnessFile ¶
AdjustBrightnessFile 图片文件亮度调整
Params: ¶
srcFile: 源图片文件 dstFile: 目标图片文件 brightness: 亮度调整值
func BinarizeFile ¶
BinarizeFile 图片文件二值化
Params: ¶
srcFile: 源图片文件 dstFile: 目标图片文件 threshold: 阈值,推荐为 128
func Compression ¶
Compression 图片压缩
Params: ¶
srcFile: 源图片路径 dstFile: 目标图片路径 quality: 压缩质量,范围 1-100(值越低,压缩率越高,质量越低)
func ConvexHull ¶
ConvexHull 计算凸包,基于 Jarvis 步进算法
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 ¶
DrawFilledCircle 绘制填充的圆形
Params: ¶
dst: 目标图片 center: 圆心坐标 radius: 圆的半径 c: 填充颜色
func DrawRectOutline ¶
DrawRectOutline 绘制矩形边框
Params: ¶
dst: 目标图片,必须是可变的(例如 *image.RGBA) r: 要绘制的矩形区域 c: 颜色
func DrawThickPolygonOutline ¶
DrawThickPolygonOutline 绘制粗多边形边框
Params: ¶
dst: 目标图片 points: 顶点集合 thickness: 边框的粗细,像素 c: 颜色
func DrawThickRectOutline ¶
DrawThickRectOutline 绘制粗矩形边框
Params: ¶
dst: 目标图片 r: 要绘制的矩形区域 c: 颜色 thickness: 边框的粗细,像素
func EqualizeHist ¶
EqualizeHist 直方图均衡化
公式 s_k = round( (cdf(i) - cdf_min) / (Dx × Dy - cdf_min) × 255 )
Params: ¶
src: 源图片
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 ¶
Flip 翻转图片(水平或垂直)
Params: ¶
src: 源图片 mode: 翻转模式
Example: ¶
Flip(img, FlipModeHorizontal) // 水平翻转
func GaussianBlur ¶
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 ¶
GaussianBlurFile 图片文件高斯模糊
Params: ¶
srcFile: 源图片文件 dstFile: 目标图片文件 radius: 高斯核半径 sigma: 高斯核标准差
Example: ¶
GaussianBlurFile("test.png", "test_gaussian.png", 3, 1.0)
func GenerateCaptcha ¶
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 ¶
GenerateSolid 生成指定宽高的纯色背景图片
Params: ¶
width: 图片宽度 height: 图片高度 c: 填充的颜色
Example: ¶
GenerateSolid(800, 600, color.RGBA{255, 255, 255, 255})
func MedianBlur ¶
MedianBlur 图片中值滤波,常用于处理椒盐噪声
Params ¶
src: 源图像 radius: 滤波半径,radius=1 表示 3x3 窗口, radius=2 表示 5x5 窗口
func MedianBlurFile ¶
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 ¶
OffsetPolygon 多边形偏移 (内缩/外扩)
- margin > 0: 外扩
- margin < 0: 内缩
Params: ¶
points: 输入的顶点列表 margin: 内外缩的距离
func OtsuThreshold ¶
OtsuThreshold 基于大津法计算推荐阈值
通过类间方差:wB × wF × (mB - mF)² 计算出区分度最大的阈值
Params: ¶
gray: 灰度图片
func Overlay ¶
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 ¶
OverlayFile 图片文件叠加,将 overlay 图片文件叠加到 base 图片文件指定位置 (x, y),支持透明通道 (alpha blending)
Params: ¶
baseFile: 基础图片文件 overlayFile: 叠加图片文件 dstFile: 目标图片文件 x: 基础图片的 X 坐标 y: 基础图片的 Y 坐标
func Resize ¶
Resize 图片缩放
- 如果 newWidth > 0 && newHeight == 0:按比例基于宽度缩放
- 如果 newWidth == 0 && newHeight > 0:按比例基于高度缩放
- 如果 newWidth > 0 && newHeight > 0:固定宽高缩放(可能扭曲)
- 如果两者均为 0:返回原图
Params: ¶
src: 源图片 newWidth: 新宽度 newHeight: 新高度
func ResizeFile ¶
ResizeFile 图片文件缩放
Params: ¶
srcFile: 源图片文件 dstFile: 目标图片文件 newWidth: 新宽度 newHeight: 新高度
func Rotate ¶
Rotate 旋转图片(顺时针 90°、180°、270°)
Params: ¶
src: 源图片 angle: 旋转角度
Example: ¶
Rotate(img, RotateAngle90) // 旋转90°
func RotateFile ¶
RotateFile 旋转图片文件(顺时针 90°、180°、270°)
Params: ¶
srcFile: 源图片文件 dstFile: 目标图片文件 angle: 旋转角度
func SimplifyPath ¶
SimplifyPath 简化路径,使用 Ramer-Douglas-Peucker (RDP) 算法进行简化
Params: ¶
points: 输入的顶点列表 epsilon: 阈值 (点到线段的距离),值越大,简化程度越高
Types ¶
type Blob ¶
type Blob struct {
ID int
Points []image.Point // 原始像素点集合
Area int // 面积
Bounds image.Rectangle // 正外接矩形 (AABB)
Centroid image.Point // 质心
}
Blob 存储单个连通区域的特征
type BlobResult ¶
BlobResult 处理后的所有结果
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: 核的宽高,为保证中心对称,最好使用奇数