![](/img/trans.png)
[英]Convert type 'Emgu.CV.Mat' to 'Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>'
[英]Convert a byte[] into an Emgu/OpenCV Image
我有一個表示灰度圖像的字節數組,我希望在C#中使用openCV,使用Emgu包裝器。 我試圖找出如何將其轉換為Emu.CV.Image
而不首先將其轉換為System.Drawing.Bitmap
。
到目前為止, 這個構造函數Image
看起來是有希望。 它看起來像像素行,列,然后數據與我的數據構建一個圖像。 然而,它想要它們以一種奇怪的格式,我正在努力如何正確構建TDepth[,,] data
參數。
這是我到目前為止所擁有的:
// This gets initialized in the constructor and filled in with greyscale image data elsewhere in the code:
byte[] depthPixelData
// Once my depthPixelData is processed, I'm trying to convert it to an Image and this is where I'm having issues
Image<Gray, Byte> depthImage = new Image<Gray, Byte>([depthBitmap.PixelHeight, depthBitmap.pixelWidth, depthPixelData]);
Visual Studio讓我很明顯,只是傳入一個數組不會削減它,但我不知道如何使用我的像素數據構造必需的TDepth[,,]
對象以傳遞給Image
構造函數。
這段代碼需要以~30fps運行,所以我試圖通過對象創建,內存分配等盡可能高效。
另一種解決方案是僅使用圖像的寬度和高度來創建EMGU.CV.Image。 然后你可以做這樣的事情:
byte[] depthPixelData = new byte[640*480]; // your data
Image<Gray, byte> depthImage = new Image<Gray, byte>(640, 480);
depthImage.Bytes = depthPixelData;
只要寬度和高度正確並且寬度可被4整除(如何實現Emgu.CV.Image),就不會有問題。 您甚至可以重用Emgu.CV.Image對象,如果不需要保存對象,只需更改每幀的字節數。
就個人而言,我會沿着這些方向做點什么:
byte[] depthPixelData = ...;
int imageWidth = ...;
int imageHeight = ...;
int channelCount = 1; // grayscale
byte[,,] depthPixelData3d = new byte[imageHeight, imageWidth, channelCount];
for(int line = 0, offset = 0; line < imageHeight; line++)
for(int column = 0; column < imageWidth; column++, offset++)
depthPixelData3d[line, column, 0] = depthPixelData[offset];
出於性能考慮,您可能希望:
Emu.Cv.Image類定義為
public class Image<TColor, TDepth> : CvArray<TDepth>, ...
TColor
此圖像的顏色類型(灰色,Bgr,Bgra,Hsv,Hls,Lab,Luv,Xyz,Ycc,Rgb或Rbga)TDepth
此圖像的深度(字節,SByte,Single,double,UInt16,Int16或Int32)
這個通用參數TDepth具有誤導性。 在你的情況下, TDepth[,,]
表示byte[,,]
要將一個數組復制到另一個數組,可以使用Buffer.BlockCopy:
byte[, ,] imageData = new byte[depthBitmap.PixelHeight , depthBitmap.PixelWidth , colorChannels];
Buffer.BlockCopy(depthPixelData, 0, imageData, 0, imageData.Length);
Image<Gray, Byte> depthImage = new Image<Gray, Byte>(imageData);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.