You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中用Emgu CV实现图像平移:Mat构造函数重载解析失败

在VB.NET中使用Emgu CV构造平移矩阵实现图像平移

要解决WarpAffine所需的平移矩阵构造问题,你可以通过以下两种正确方式实现:

方法1:使用Matrix<Single>转换为Mat

Emgu CV的Matrix<T>类支持直接用二维数组初始化,之后可转换为Mat对象:

' 定义X、Y轴平移量
Dim tx As Single = 100.0F
Dim ty As Single = 100.0F

' 构造2行3列的平移矩阵数组
Dim warpArray(,) As Single = {{1.0F, 0.0F, tx}, {0.0F, 1.0F, ty}}
Dim translationMatrix As New Matrix(Of Single)(warpArray)

' 转换为Mat对象供WarpAffine使用
Dim warpMat As Mat = translationMatrix

方法2:直接创建Mat并填充元素

先初始化指定尺寸和类型的Mat,再通过SetTo方法或索引赋值填充矩阵元素:

Dim tx As Single = 100.0F
Dim ty As Single = 100.0F

' 创建2行3列、32位浮点型的空Mat
Dim warpMat As New Mat(2, 3, CvEnum.DepthType.Cv32F)

' 方式1:用ScalarArray批量赋值
warpMat.SetTo(New ScalarArray(New Single() {1.0F, 0.0F, tx, 0.0F, 1.0F, ty}))

' 方式2:通过索引逐个赋值(可选)
' warpMat(0, 0) = 1.0F
' warpMat(0, 1) = 0.0F
' warpMat(0, 2) = tx
' warpMat(1, 0) = 0.0F
' warpMat(1, 1) = 1.0F
' warpMat(1, 2) = ty

完整图像平移示例

构造好矩阵后,调用WarpAffine完成平移操作:

' 读取原始图像
Dim srcImage As New Mat("input.jpg", ImreadModes.Color)

' 定义输出图像尺寸(与原图一致,可按需调整)
Dim dstSize As New Size(srcImage.Width, srcImage.Height)

' 执行平移变换
Dim dstImage As New Mat()
CvInvoke.WarpAffine(srcImage, dstImage, warpMat, dstSize, Inter.Linear, BorderType.Constant, New Scalar(0, 0, 0))

' 保存或展示结果
dstImage.Save("output.jpg")

你之前的代码报错是因为Emgu CV的Mat构造函数没有直接接受一维Single数组的重载,上述两种方法都能正确构造出符合要求的平移矩阵。

内容的提问来源于stack exchange,提问作者user2959923

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 07:22:47