OpenCvSharp Bitmap 转成 Mat

_captureDeviceL.NewFrame += VideoSource_NewFrameL;
private void VideoSource_NewFrameL(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
_DoubleCapture.ImgL = MKYRobot.utils.TypeConverter.BitmapToMat(eventArgs.Frame);
GC.Collect();
}
   public static Mat BitmapToMat(Bitmap srcbit)
        {
            int iwidth = srcbit.Width;
            int iheight = srcbit.Height;
            int iByte = iwidth * iheight * 3;
            byte[] result = new byte[iByte];
            int step;
            Rectangle rect = new Rectangle(0,0, iwidth, iheight);
            BitmapData bmpData = srcbit.LockBits(rect,ImageLockMode.ReadWrite, srcbit.PixelFormat);
            IntPtr iPtr = bmpData.Scan0;
            Marshal.Copy(iPtr,result,0,iByte);
            step = bmpData.Stride;
            srcbit.UnlockBits(bmpData);
            return  new Mat(srcbit.Height, srcbit.Width,new MatType(MatType.CV_8UC3),result,step);
        }

你可能感兴趣的:(C#)