[英]Rewriting OpenCV C++ to EmguCV C# - how to work with pointers?
我正在嘗試將編寫 OpenCV 2.x 的 C++ 代碼轉換為 C# 中的 Emgu.CV。
我在 C++ 中有一個 function:
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;
}
如何有效地使用 EmguCV 在 C# 中做同樣的事情?
到目前為止 - 我有這個 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# 代碼是否正確?
有更好/更有效的方法嗎?
您可以嘗試將inMat
轉換為數組inM
,然后在另一個數組Or
中計算您需要的值,最后將后一個數組轉換為 output Mat
outMat
。
注意:我認為NumberOfChannels
為 1,因為我認為情況總是如此。
Mat computeMatXGradient(Mat inMat)
{
int x, y;
byte[] inM, Mr;
double[] Or;
inM = new byte[(int)inMat.Total];
inMat.CopyTo(inM);
Mr = new byte[inMat.Cols];
Or = new double[inMat.Rows * inMat.Cols];
for (y = 0; y < inMat.Rows; y++)
{
Array.Copy(inM, y * inMat.Cols, Mr, 0, inMat.Cols);
Or[y * inMat.Cols] = Mr[1] - Mr[0];
for (x = 1; x < inMat.Cols - 1; x++)
Or[y * inMat.Cols + x] = (Mr[x + 1] - Mr[x - 1]) / 2.0;
Or[y * inMat.Cols + inMat.Cols - 1] = Mr[inMat.Cols - 1] - Mr[inMat.Cols - 2];
}
Mat outMat = new Mat(inMat.Rows, inMat.Cols, DepthType.Cv64F, 1);
Marshal.Copy(Or, 0, outMat.DataPointer, inMat.Rows * inMat.Cols);
return outMat;
}
這是保存代碼以進行等效操作。 沒有測試。 我檢查了 OpenCV 庫以獲取 Mat 的結構。 OpenCV 中的真實結構有一個指向數據的指針。 相反,我制作了一個字節數組。 因此,如果您正在加載/編寫 OpenCV 結構,則需要一個接口來轉換。 output 矩陣是輸入的八倍,因為輸入是一個字節數組,而 output 是一個雙精度數組。
如果有輸入 byte[][] 和 output double[][],代碼會更好。
public class Mat
{
public int flags;
//! the array dimensionality, >= 2
public int dims;
//! the number of rows and columns or (-1, -1) when the array has more than 2 dimensions
public int rows;
public int cols;
//! pointer to the data
public byte[] data;
//! pointer to the reference counter;
// when array points to user-allocated data, the pointer is NULL
public Mat[] refcount;
// other members
public Mat computeMatXGradient(Mat inMat)
{
Mat outMat = new Mat();
outMat.rows = inMat.rows;
outMat.cols = inMat.cols;
outMat.flags = inMat.flags; //include depthType and NumberofChannels
outMat.dims = inMat.dims;
outMat.data = new byte[inMat.rows * inMat.cols * sizeof(Double)];
int outIndex = 0;
byte[] d = new byte[sizeof(double)];
for (int y = 0; y < inMat.rows; ++y)
{
int inRowIndex = y * inMat.cols;
d = BitConverter.GetBytes((double)(inMat.data[inRowIndex + 1] - inMat.data[inRowIndex]));
Array.Copy(d, 0, outMat.data, outIndex, sizeof(double));
outIndex += sizeof(double);
for (int x = 1; x < inMat.cols - 1; ++x)
{
d = BitConverter.GetBytes((double)(inMat.data[inRowIndex + x + 1] - inMat.data[inRowIndex + x - 1]) / 2.0);
Array.Copy(d, 0, outMat.data, outIndex,sizeof(double));
outIndex += sizeof(double);
}
d = BitConverter.GetBytes((double)(inMat.data[inRowIndex + inMat.cols - 1] - inMat.data[inRowIndex - inMat.cols - 2]));
Array.Copy(d, 0, outMat.data, outIndex, sizeof(double));
}
return outMat;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.