dinov2

package
v0.0.0-...-d793979 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MeanR float32 = 0.485
	MeanG float32 = 0.456
	MeanB float32 = 0.406
	StdR  float32 = 0.229
	StdG  float32 = 0.224
	StdB  float32 = 0.225
)

ImageNet 归一化常量 (DINOv2 使用 ImageNet 的 mean/std 进行预处理)

Variables

This section is empty.

Functions

func DrawResult

func DrawResult(img image.Image, result *Result) image.Image

DrawResult 将分割结果以不同颜色半透明叠加到原图上

Params:

img: 原图
result: 分割结果

Types

type Config

type Config struct {
	ModelPath          string // ONNX 模型路径
	OnnxRuntimeLibPath string // ONNX Runtime 动态库路径

	// 推理参数
	InputSize   int // 模型输入尺寸, 默认 224 (DINOv2 常用 224 / 518)
	NumClusters int // 分割区域(聚类)数量, 默认 3
	NumIter     int // k-means 最大迭代次数, 默认 30

	// 模型参数
	PatchSize int // 图像 Patch 尺寸, 默认 14 (dinov2-base)

	// 可选参数
	UseCuda           bool // (可选) 是否启用 CUDA
	NumThreads        int  // (可选) ONNX 线程数, 默认由CPU核心数决定
	EnableCpuMemArena bool // (可选) 是否开启 ONNX 内存池
}

Config 引擎的初始化参数

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 默认配置

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine DINOv2 分割引擎

func NewEngine

func NewEngine(cfg Config) (*Engine, error)

NewEngine 初始化引擎

func (*Engine) Destroy

func (e *Engine) Destroy()

Destroy 释放相关资源

func (*Engine) Predict

func (e *Engine) Predict(img image.Image) (*Result, error)

Predict 执行分割推理

DINOv2 输出的是 Patch 特征 (last_hidden_state), 这里通过无监督聚类 (k-means) 将特征相似的区域划分到同一个分割区域, 从而得到逐像素的分割结果。

type Result

type Result struct {
	Width  int // 原图宽度
	Height int // 原图高度

	// Labels 分割标签图, 每个像素值为对应的聚类编号 (0 ~ NumClusters-1)
	Labels *image.Gray
	// Masks 每个聚类的二值掩码, 白色(255) 表示属于该区域
	Masks []*image.Gray
}

Result 分割结果

Jump to

Keyboard shortcuts

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