diA*_*blo 7 c# pinvoke opencv c++-cli bitmap
我有一个基于opencv用C++编写的图像处理功能.在我的wpf应用程序中,我使用AForge库来访问网络摄像头并在UI上更新它.这是处理新帧的功能.
void UI_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
System.Drawing.Bitmap bitmapFrame = (Bitmap)eventArgs.Frame.Clone();
MemoryStream ms = new MemoryStream();
bitmapFrame.Save(ms, ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
BitmapImage bitmapImageFrame = new BitmapImage();
bitmapImageFrame.BeginInit();
bitmapImageFrame.StreamSource = ms;
bitmapImageFrame.EndInit();
bitmapImageFrame.Freeze();
CurrentFrame = bitmapImageFrame;
}
catch (Exception ex)
{
Debug.WriteLine("fatal::" + ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的c ++代码:
void FaceTracker(unsigned char* imageBuffer, int width, int height){
Mat frame(Size(width, height), CV_8UC4, imageBuffer, Mat::AUTO_STEP);
Mat gray;
cvtColor(frame, gray, COLOR_BGR2GRAY);
//do more operations
}
Run Code Online (Sandbox Code Playgroud)
我有两个选择.我已经找到了一个用于将数据复制BitmapImage到IntPtr缓冲区的代码CopyPixels.我测试了它,它工作正常.但我也读过我可以简单地使用句柄传递给Bitmap GetHBitmap.但这不适合我.我读了这个如何将.NET位图传递给本机DLL?但我不明白这一部分GetDIBits.我也尝试锁定位图像素并像这样传递:
bitmapFrame.LockBits(new Rectangle(0, 0, bitmapFrame.Width, bitmapFrame.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
NativeFunctions.FaceTracker(bitmapFrame.GetHbitmap(), bitmapFrame.Width, bitmapFrame.Height);
Run Code Online (Sandbox Code Playgroud)
并简单地传递HBitMap,既不起作用.我总是得到一个说明内存访问冲突的异常.
Yoc*_*mer 10
取决于范围.如果可以保证在其他地方不使用位图,则可以锁定图像缓冲区,然后将指针向下传递给C++代码,然后将其解锁.
的LockBits命令返回的BitmapData具有一个指向它的图像缓冲器类SCAN0属性:
BitmapData bmpData = bitmapFrame.LockBits(new Rectangle(0, 0, bitmapFrame.Width, bitmapFrame.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
NativeFunctions.FaceTracker(bmpData.Scan0 , bitmapFrame.Width, bitmapFrame.Height);
bitmapFrame.UnlockBits(bmpData); //Remember to unlock!!!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5069 次 |
| 最近记录: |