c# - 如何在 OpenCvSharp 库中打印矩阵值
问题描述
我正在尝试使用Cv2.FindHomography
方法找到两个源的单应矩阵。到目前为止一切正常。问题是我无法获取/打印矩阵值。我真的是 C# 的初学者。我找到了属于它的一种文档。但是,我不明白如何迭代这些值。
this[int startCol, int endCol]
override MatExpr OpenCvSharp.Mat.ColExprIndexer.this[int startCol, int endCol]
getset
Creates a matrix header for the specified column span.
Parameters
startCol An inclusive 0-based start index of the column span.
endCol An exclusive 0-based ending index of the column span.
Returns
我的代码,
Point2d[] points1 = new Point2d[]
{
new Point2d(141, 131), new Point2d(480, 159)
};
Point2d[] points2 = new Point2d[]
{
new Point2d(318, 256),
new Point2d(5, 1)
};
Mat hCv = Cv2.FindHomography(points1, points2);
// I want to print the the resultant matrix
解决方案
第一种方法是使用Data
方法。
byte[] data = new byte[hCv.Width * hCv.Height];
Marshal.Copy(hCv.DataPointer, data, 0, hCv.Width * hCv.Height);
for(int i = 0; i < data.Length; i++)
{
// Print data[i]
}
类型取决于矩阵的类型,如果是CV_8
,则使用byte
,如果是CV_32
,则使用float
等。
据此- 另一种方法是创建图像或矩阵(附加副本),然后访问每个元素
Image<Bgr, Byte> img = hCv.ToImage<Bgr, Byte>();
然后可以使用该Image<,>.Data
属性访问像素数据。
您还可以将 Mat 转换为 Matrix<> 对象。假设 Mat 包含 8 位数据
Matrix<Byte> matrix = new Matrix<Byte>(hCv.Rows, hCv.Cols, mat.NumberOfChannels);
hCv.CopyTo(matrix);
然后可以使用该Matrix<>.Data
属性访问像素数据。
推荐阅读
- gitlab-ci - 如何将多行字符串存储在gitlab yaml中的变量中
- android - Flutter AppsFlyer 为 Android 异常构建
- node.js - 如何在语音频道中奖励用户 XP?
- swift - NSSharingService.perform() 与发送到照片
- kubernetes - Kubernetes HPA 不工作:无法获取指标
- python - 将字符串中的数字转换为英文单词
- python - 如何将类似日期时间的字符串转换为毫秒
- delphi - FireDAC 将 Access 文件中的数据加载到 DBGrid 中,而不使用拖放控件
- python-3.x - 设置 nginx.conf 以根据原始 uri 路由 2 个不同的本地 wsgi
- visual-c++ - 如何将 std::variant 作为 VARIANT* 传递给 ExecWB?