按照Emgu規範的那些參數的意思是:
/// <param name="type">Mat element type
/// <param name="channels">Number of channels
/// <param name="data">
/// Pointer to the user data. Matrix constructors that take data and step parameters do not
/// allocate matrix data. Instead, they just initialize the matrix header that points to the
/// specified data, which means that no data is copied. This operation is very efficient and
/// can be used to process external data using OpenCV functions. The external data is not
/// automatically deallocated, so you should take care of it.
/// <param name="step">
/// Number of bytes each matrix row occupies.
/// The value should include the padding bytes at the end of each row, if any.
type
是類型CvEnum.DepthType
,這是圖像的深度,可以通過CvEnum.DepthType.Cv32F
其代表32位深度的圖像,其他可能值的形式爲CvEnum.DepthType.Cv{x}{t}
,其中{x}爲集合{8,16,32,64}中的任何值,{t}可以爲S
,對於Single
或F
對於Float
。
channels
,取決於圖像的類型,但我認爲你可以使用ARGB的4
。
對於其他2個參數,如果你不需要優化的一部分,你可以使用這個構造的Mat
類:
public Mat(int rows, int cols, DepthType type, int channels)
如果你真的想使用優化版本,然後(繼續):
UPDATE
其他方式獲得data
和step
是BitmapData類,像這樣(摘自MSDN資源):
Bitmap bmp = new Bitmap(Image.FromStream(httpPostedFileBase.InputStream, true, true));
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// data = scan0 is a pointer to our memory block.
IntPtr data = bmpData.Scan0;
// step = stride = amount of bytes for a single line of the image
int step = bmpData.Stride;
// So you can try to get you Mat instance like this:
Mat mat = new Mat(bmp.Height, bmp.Width, CvEnum.DepthType.Cv32F, 4, data, step);
// Unlock the bits.
bmp.UnlockBits(bmpData);
沒有測試這個解決方案,但你可以試試看。 我的回答是基於Emgu代碼here,位圖的IntPtrhere,也對這個post這有助於我還獲得這個進一步的瞭解。
我看到其他的方法來做到這一點還,除非你真的需要調用完整的構造,我會嘗試這種做法,似乎更清潔:
HttpPostedFileBase file //your file must be available is this context.
if (file.ContentLength > 0)
{
string filename = Path.GetFileName(file.FileName);
// your so wanted Mat!
Mat img = imread(filename, CV_LOAD_IMAGE_COLOR);
}
注意
OpenCV documentation有很棒的教程。只需看看核心模塊的可用教程。特別是這個one。
你做到了嗎?或者你還在嘗試?你需要更多的幫助嗎? –