#fieldimg — 图像场几何(GeoVerse Phase 1)

    层级:四层 · 领域高阶算法层 定位:将图像提升为场实体,标量场(灰度)/矢量场(RGB)/张量场;场是一等几何对象。

    #核心思想

    图像不是像素数组,是二维场实体。本模块将图像处理算子统一为场算子的特例:

    场算子图像处理含义
    field_gradient边缘检测(Sobel/Canny 的数学本质)
    field_laplacian模糊/锐化(LoG)
    field_integrate直方图/总量积分
    field_clamp阈值分割
    field_isosurface等值线提取 → 分割

    #模块结构

    文件内容
    scalar_field.mbt标量场:2D 标量场类型 + 求值/设值/最值
    vector_field.mbt矢量场:2D 矢量场类型 + 求值/幅值
    tensor_field.mbt张量场:结构张量 + 特征值分解
    field_ops.mbt场算子:梯度/散度/旋度/拉普拉斯/积分/阈值/代数/等值/直方图
    field_bind.mbtImage ↔ ScalarField / VectorField 双向转换

    #公共 API

    #类型

    • ScalarField — 标量场(width × height,Float 数据)
    • VectorField — 矢量场(width × height × dim,Float 数据)
    • TensorField — 张量场(width × height × rows × cols,Float 数据)

    #场算子

    • field_gradient(f) → 矢量场 [gx, gy]
    • field_divergence(v) → 标量场 div = du/dx + dv/dy
    • field_curl(v) → 标量场 curl = dv/dx - du/dy
    • field_laplacian(f) → 标量场 Δf
    • field_integrate(f) → Float 全场积分
    • field_clamp(f, lo, hi) → 钳制场值
    • field_mask(f, threshold) → 二值掩码
    • field_add / field_sub / field_mul / field_blend → 场代数
    • field_isosurface(f, level, epsilon) → 等值线提取
    • field_histogram(f, min, max, bins) → 直方图

    #Image 双向转换

    • image_to_scalar_field(img) → 标量场(灰度归一化 [0,1])
    • scalar_field_to_image(field) → 灰度图
    • image_to_vector_field(img) → 矢量场(RGB)
    • vector_field_to_image(field) → RGB 图

    #数学恒等式(属性测试验证)

    • gradient(constant) = 0 — 常量场梯度为零
    • laplacian(constant) = 0 — 常量场拉普拉斯为零
    • curl(gradient(f)) = 0 — 梯度场无旋
    • field_sub(field_add(a, b), b) = a — 加法逆元

    #测试

    64 个测试(5 个测试文件),四目标全绿(native/wasm-gc/js/wasm)。

    ScalarField

    pub(all) struct ScalarField {
    width : Int
    height : Int
    data : Array[Float]
    } derive(Eq,
    Debug
    )

    标量场:2D 标量场,data 为行主序 Float 数组,长度 = width * height。 标量场:2D Scalar field: 2D

    ScalarField::constant

    fn ScalarField::constant(width : Int, height : Int, value : Float) -> ScalarField raise
    core
    /types.LoadError

    创建常量标量场(所有点值相同)。 创建常量标量场(所有点共享同一值) Create a constant scalar field (all points share the same value).

    ScalarField::equal

    fn ScalarField::equal(ScalarField, ScalarField) -> Bool
    automatically derived

    ScalarField::evaluate

    fn ScalarField::evaluate(self : ScalarField, x : Int, y : Int) -> Float

    求值:获取点 (x, y) 处的场值,边界外返回 0。 求值场于 (x, y);越界返回 0 Evaluate the field at (x, y); returns 0 for out-of-bounds.

    ScalarField::evaluate_clamped

    fn ScalarField::evaluate_clamped(self : ScalarField, x : Int, y : Int) -> Float

    求值(边界钳制):边界外取最近边界点的值。 钳位边界求值:越界取最近边缘值 Evaluate with clamped boundary: out-of-bounds takes nearest edge value.

    ScalarField::make

    fn ScalarField::make(width : Int, height : Int, data : Array[Float]) -> ScalarField raise
    core
    /types.LoadError

    创建标量场,数据长度必须等于 width * height。 创建标量场;数据长度须等于 width * height Create a scalar field; data length must equal width * height.

    ScalarField::max_value

    fn ScalarField::max_value(self : ScalarField) -> Float

    场的最大值。 场中最大值 Maximum value in the field.

    ScalarField::min_value

    fn ScalarField::min_value(self : ScalarField) -> Float

    场的最小值。 场中最小值 Minimum value in the field.

    ScalarField::not_equal

    fn ScalarField::not_equal(x : ScalarField, y : ScalarField) -> Bool
    automatically derived

    ScalarField::set

    fn ScalarField::set(self : ScalarField, x : Int, y : Int, value : Float) -> Unit

    设值:设置点 (x, y) 处的场值,越界则忽略。 设置值于 (x, y);越界忽略 Set the value at (x, y); out-of-bounds is ignored.

    ScalarField::size

    fn ScalarField::size(self : ScalarField) -> Int

    场的像素总数。 场中总点数 Total number of points in the field.

    ScalarField::to_repr

    automatically derived

    ScalarField::zeros

    fn ScalarField::zeros(width : Int, height : Int) -> ScalarField raise
    core
    /types.LoadError

    创建全零标量场。 创建全零标量场 Create an all-zero scalar field.

    TensorField

    pub(all) struct TensorField {
    width : Int
    height : Int
    rows : Int
    cols : Int
    data : Array[Float]
    } derive(Eq,
    Debug
    )

    张量场:2D 张量场,data 为行主序,长度 = width * height * rows * cols。 张量场:2D Tensor field: 2D

    TensorField::eigenvalues_2x2

    fn TensorField::eigenvalues_2x2(self : TensorField) -> VectorField raise
    core
    /types.LoadError

    计算结构张量的两个特征值,返回矢量场(dim=2,[λ1, λ2],λ1 ≥ λ2)。 特征值用于 Harris 角点响应:R = det(J) - k * trace(J)² = λ1λ2 - k(λ1+λ2)²。 计算结构张量特征值 Compute eigenvalues of 2×2 structure tensors, returns VectorField (dim=2, [λ1, λ2], λ1 ≥ λ2).

    TensorField::equal

    fn TensorField::equal(TensorField, TensorField) -> Bool
    automatically derived

    TensorField::evaluate

    fn TensorField::evaluate(self : TensorField, x : Int, y : Int, r : Int, c : Int) -> Float

    求值单元素:获取点 (x, y) 处矩阵第 (r, c) 元素,边界外返回 0。 求值元素 (r, c) 于 (x, y);越界返回 0 Evaluate element (r, c) at (x, y); returns 0 for out-of-bounds.

    TensorField::evaluate_clamped

    fn TensorField::evaluate_clamped(self : TensorField, x : Int, y : Int, r : Int, c : Int) -> Float

    求值单元素(边界钳制)。 钳位边界求值元素 Evaluate element with clamped boundary.

    TensorField::make

    fn TensorField::make(width : Int, height : Int, rows : Int, cols : Int, data : Array[Float]) -> TensorField raise
    core
    /types.LoadError

    创建张量场,数据长度必须等于 width * height * rows * cols。 创建张量场;数据长度须等于 width * height * rows * cols Create a tensor field; data length must equal width * height * rows * cols.

    TensorField::not_equal

    fn TensorField::not_equal(x : TensorField, y : TensorField) -> Bool
    automatically derived

    TensorField::set

    fn TensorField::set(self : TensorField, x : Int, y : Int, r : Int, c : Int, value : Float) -> Unit

    设值单元素:设置点 (x, y) 处矩阵第 (r, c) 元素,越界则忽略。 设置元素 (r, c) 于 (x, y);越界忽略 Set element (r, c) at (x, y); out-of-bounds is ignored.

    TensorField::structure_tensor

    fn TensorField::structure_tensor(gradient : VectorField) -> TensorField raise
    core
    /types.LoadError

    由梯度矢量场构建结构张量场(2×2 对称)。 结构张量 J = [[Ix², IxIy], [IxIy, Iy²]],用于角点检测。 从梯度向量场构建结构张量场 Build structure tensor field (2×2 symmetric) from a gradient vector field (dim=2). 结构张量 J,用于角点检测 Structure tensor J = [[Ix², IxIy], [IxIy, Iy²]], used for corner detection.

    TensorField::to_repr

    automatically derived

    TensorField::zeros

    fn TensorField::zeros(width : Int, height : Int, rows : Int, cols : Int) -> TensorField raise
    core
    /types.LoadError

    创建全零张量场。 创建全零张量场 Create an all-zero tensor field.

    VectorField

    pub(all) struct VectorField {
    width : Int
    height : Int
    dim : Int
    data : Array[Float]
    } derive(Eq,
    Debug
    )

    矢量场:2D 矢量场,data 为行主序 Float 数组,长度 = width * height * dim。 向量场:2D 向量场,行主序 Vector field: 2D vector field, data is row-major, length = width * height * dim.

    VectorField::equal

    fn VectorField::equal(VectorField, VectorField) -> Bool
    automatically derived

    VectorField::evaluate

    fn VectorField::evaluate(self : VectorField, x : Int, y : Int) -> Array[Float]

    求值:获取点 (x, y) 处的矢量(返回 dim 维数组副本),边界外返回零矢量。 求值向量于 (x, y);越界返回零向量 Evaluate the vector at (x, y); returns zero vector for out-of-bounds.

    VectorField::evaluate_component

    fn VectorField::evaluate_component(self : VectorField, x : Int, y : Int, k : Int) -> Float

    求值单分量:获取点 (x, y) 处矢量第 k 分量,边界外返回 0。 求值分量 k 于 (x, y);越界返回 0 Evaluate a single component k at (x, y); returns 0 for out-of-bounds.

    VectorField::evaluate_component_clamped

    fn VectorField::evaluate_component_clamped(self : VectorField, x : Int, y : Int, k : Int) -> Float

    求值单分量(边界钳制)。 钳位边界求值单个分量 Evaluate a single component with clamped boundary.

    VectorField::magnitude

    fn VectorField::magnitude(self : VectorField) -> ScalarField raise
    core
    /types.LoadError

    矢量场幅值:返回每个点的矢量模长,形成标量场。 向量幅值:返回各点向量范数的标量场 Vector magnitude: returns a scalar field of vector norms at each point.

    VectorField::make

    fn VectorField::make(width : Int, height : Int, dim : Int, data : Array[Float]) -> VectorField raise
    core
    /types.LoadError

    创建矢量场,数据长度必须等于 width * height * dim。 创建向量场;数据 Create a vector field; data

    VectorField::not_equal

    fn VectorField::not_equal(x : VectorField, y : VectorField) -> Bool
    automatically derived

    VectorField::set_component

    fn VectorField::set_component(self : VectorField, x : Int, y : Int, k : Int, value : Float) -> Unit

    设值单分量:设置点 (x, y) 处矢量第 k 分量,越界则忽略。 设置分量 k 于 (x, y);越界忽略 Set component k at (x, y); out-of-bounds is ignored.

    VectorField::to_repr

    automatically derived

    VectorField::zeros

    fn VectorField::zeros(width : Int, height : Int, dim : Int) -> VectorField raise
    core
    /types.LoadError

    创建全零矢量场。 创建全零向量场 Create an all-zero vector field.

    field_add

    fn field_add(a : ScalarField, b : ScalarField) -> ScalarField raise
    core
    /types.LoadError

    场加法:a + b(逐元素)。两场尺寸必须一致。 场加法:a + b(逐元素)。两场须同尺寸 Field addition: a + b (element-wise). Both fields must have the same size.

    field_blend

    fn field_blend(a : ScalarField, b : ScalarField, alpha : Float) -> ScalarField raise
    core
    /types.LoadError

    场混合:(1 - alpha) * a + alpha * b。两场尺寸必须一致,alpha 通常在 [0, 1]。 场混合:(1 - alpha) * a + alpha * b。须同尺寸 Field blend: (1 - alpha) * a + alpha * b. Same size required; alpha typically in [0, 1].

    field_clamp

    fn field_clamp(field : ScalarField, lo : Float, hi : Float) -> ScalarField raise
    core
    /types.LoadError

    阈值裁剪:将场值限制在 [lo, hi] 范围内。 将场值钳位到 [lo, hi] Clamp field values to [lo, hi].

    field_curl

    fn field_curl(field : VectorField) -> ScalarField raise
    core
    /types.LoadError

    旋度:计算 2D 矢量场(dim=2)的标量旋度,返回标量场。 curl = dv/dx - du/dy(2D 中的标量旋度)。边界使用单侧差分。 二维向量场标量旋度,返回 ScalarField Scalar curl of a 2D vector field (dim=2), returns ScalarField. 旋度 curl = dv/dx - du/dy. Boundary uses one-sided difference.

    field_divergence

    fn field_divergence(field : VectorField) -> ScalarField raise
    core
    /types.LoadError

    散度:计算 2D 矢量场(dim=2)的散度,返回标量场。 div = du/dx + dv/dy。边界使用单侧差分。 二维向量场散度,返回 ScalarField Divergence of a 2D vector field (dim=2), returns ScalarField. 散度 div = du/dx + dv/dy. Boundary uses one-sided difference.

    field_gradient

    fn field_gradient(field : ScalarField) -> VectorField raise
    core
    /types.LoadError

    梯度:中心差分计算标量场梯度,返回矢量场(dim=2,[gx, gy])。 边界使用单侧差分。 梯度:标量场中心差分,返回 VectorField Gradient: central difference of a scalar field, returns VectorField (dim=2, [gx, gy]). 边界使用单侧差分 Boundary uses one-sided difference.

    field_histogram

    fn field_histogram(field : ScalarField, min_val : Float, max_val : Float, bins : Int) -> Array[Int] raise
    core
    /types.LoadError

    直方图:统计标量场值落在 [min_val, max_val) 分成 bins 个桶的计数。 直方图:统计落入 bins 桶的值 Histogram: count values falling into bins buckets over [min_val, max_val).

    field_integrate

    fn field_integrate(field : ScalarField) -> Float

    积分:标量场全场求和(离散积分,相当于总量/面积积分)。 积分:求和所有值(离散积分 = 总和 / 面积积分) Integrate: sum all values (discrete integral = total / area integral).

    field_integrate_region

    fn field_integrate_region(field : ScalarField, x0 : Int, y0 : Int, x1 : Int, y1 : Int) -> Float

    区域积分:对标量场在 [x0,x1)×[y0,y1) 区域求和。 区域积分 Regional integrate: sum values in the region [x0,x1)×[y0,y1).

    field_isosurface

    fn field_isosurface(field : ScalarField, level : Float, epsilon : Float) -> ScalarField raise
    core
    /types.LoadError

    等值面提取:标记 |value - level| < epsilon 的点为 1.0,否则 0.0。 在 2D 中即等值线提取,可用于分割。 等值面 Isosurface: mark points where |value - level| < epsilon as 1.0, else 0.0. 2D 下为 In 2D this is

    field_laplacian

    fn field_laplacian(field : ScalarField) -> ScalarField raise
    core
    /types.LoadError

    拉普拉斯:计算标量场拉普拉斯,返回标量场。 拉普拉斯 lap = f[x+1,y] + f[x-1,y] + f[x,y+1] + f[x,y-1] - 4f[x,y]。 边界使用钳制(复制边界值)。 标量场拉普拉斯,返回 ScalarField Laplacian of a scalar field, returns ScalarField. 拉普拉斯 lap = f[x+1,y] + f[x-1,y] + f[x,y+1] + f[x,y-1] - 4f[x,y]. 边界使用钳位(复制边缘)值 Boundary uses clamped (replicate edge) values.

    field_mask

    fn field_mask(field : ScalarField, threshold : Float) -> ScalarField raise
    core
    /types.LoadError

    掩码:按阈值生成二值掩码场(value >= threshold → 1.0,否则 0.0)。 掩膜:二值掩膜场 Mask: binary mask field (value >= threshold → 1.0, else 0.0).

    field_mul

    fn field_mul(a : ScalarField, b : ScalarField) -> ScalarField raise
    core
    /types.LoadError

    场逐元素乘法:a * b(逐元素)。两场尺寸必须一致。 场逐元素乘法:a * b。两场须同尺寸 Field element-wise multiplication: a * b. Both fields must have the same size.

    field_mul_scalar

    fn field_mul_scalar(field : ScalarField, scalar : Float) -> ScalarField raise
    core
    /types.LoadError

    场标量乘法:field * scalar(逐元素)。 场标量乘法:field * scalar(逐元素) Field scalar multiplication: field * scalar (element-wise).

    field_sub

    fn field_sub(a : ScalarField, b : ScalarField) -> ScalarField raise
    core
    /types.LoadError

    场减法:a - b(逐元素)。两场尺寸必须一致。 场减法:a - b(逐元素)。两场须同尺寸 Field subtraction: a - b (element-wise). Both fields must have the same size.

    image16_to_scalar_field

    fn image16_to_scalar_field(img :
    core
    /types.Image16) -> ScalarField raise
    core
    /types.LoadError

    Image16 转标量场:16-bit 图转标量场,归一化到 [0, 1](除以 65535)。 Image16 转 ScalarField;归一化到 [0, 1](除以 65535) Convert Image16 to ScalarField; normalized to [0, 1] (divided by 65535).

    image_to_scalar_field

    fn image_to_scalar_field(img :
    core
    /types.Image) -> ScalarField raise
    core
    /types.LoadError

    Image 转标量场:将灰度图(channels=1)转为标量场,像素值归一化到 [0, 1]。 若 channels > 1,先转灰度(加权平均:0.299R + 0.587G + 0.114B)。 Image 转 ScalarField;灰度归一化到 [0, 1] Convert Image to ScalarField; grayscale (channels=1) normalized to [0, 1]. channels > 1 时 For channels > 1

    image_to_vector_field

    fn image_to_vector_field(img :
    core
    /types.Image) -> VectorField raise
    core
    /types.LoadError

    Image 转矢量场:将 RGB/RGBA 图转为矢量场(dim=channels),像素值归一化到 [0, 1]。 Image 转 VectorField;像素值归一化到 [0, 1] Convert Image to VectorField (dim=channels); pixel values normalized to [0, 1].

    imagef_to_scalar_field

    fn imagef_to_scalar_field(img :
    core
    /types.ImageF) -> ScalarField raise
    core
    /types.LoadError

    ImageF 转标量场:HDR float 图转标量场,取第一通道,钳制到 [0, 1]。 ImageF 转 ScalarField;取首通道,钳位到 [0, 1] Convert ImageF to ScalarField; takes first channel, clamped to [0, 1].

    scalar_field_to_image

    fn scalar_field_to_image(field : ScalarField) ->
    core
    /types.Image

    标量场转 Image:将标量场转为单通道灰度图,值钳制到 [0, 1] 后映射到 [0, 255]。 ScalarField 转单通道灰度 Image;值钳位到 [0,1] 再映射到 [0,255] Convert ScalarField to single-channel grayscale Image; values clamped to [0, 1] then mapped to [0, 255].

    vector_field_to_image

    fn vector_field_to_image(field : VectorField) ->
    core
    /types.Image

    矢量场转 Image:将矢量场转为多通道图,值钳制到 [0, 1] 后映射到 [0, 255]。 VectorField 转多通道 Image;值钳位到 [0,1] 再映射到 [0,255] Convert VectorField to multi-channel Image; values clamped to [0, 1] then mapped to [0, 255].