【发布时间】:2020-11-03 23:53:04
【问题描述】:
我正在尝试将 OpenCV 2.x 编写的 C++ 代码转换为 C# 中的 Emgu.CV。
我有一个 C++ 函数:
cv::Mat computeMatXGradient(const cv::Mat &mat) {
cv::Mat out(mat.rows, mat.cols, CV_64F);
for (int y = 0; y < mat.rows; ++y) {
const uchar* Mr = mat.ptr<uchar>(y);
double* Or = out.ptr<double>(y);
Or[0] = Mr[1] - Mr[0];
for (int x = 1; x < mat.cols - 1; ++x) {
Or[x] = (Mr[x + 1] - Mr[x - 1]) / 2.0;
}
Or[mat.cols - 1] = Mr[mat.cols - 1] - Mr[mat.cols - 2];
}
return out;
}
如何在 C# 中使用 EmguCV 高效地做同样的事情?
到目前为止 - 我有这个 C# 代码: (我无法测试它,因为缺少很多代码)
Mat computeMatXGradient(Mat inMat)
{
Mat outMat = new Mat(inMat.Rows, inMat.Cols, DepthType.Cv64F, inMat.NumberOfChannels);
for (int y = 0; y < inMat.Rows; ++y)
{
// unsafe is required if I'm using pointers
unsafe {
byte* Mr = (byte*) inMat.DataPointer;
double* Or = (double*) outMat.DataPointer;
Or[0] = Mr[1] - Mr[0];
for (int x = 1; x < inMat.Cols - 1; ++x)
{
Or[x] = (Mr[x + 1] - Mr[x - 1]) / 2.0;
}
Or[inMat.Cols - 1] = Mr[inMat.Cols - 1] - Mr[inMat.Cols - 2];
}
}
return outMat;
}
问题:
-
我的 C# 代码正确吗?
-
有没有更好/更有效的方法?
【问题讨论】: