private void ImageCallBack_2(IntPtr m_BufForDriver, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser) { MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); int nRet = cameraArr1[0].MV_CC_GetIntValue_NET("PayloadSize", ref stParam); if (MyCamera.MV_OK != nRet) { MessageBox.Show("Get PayloadSize failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } UInt32 nPayloadSize = stParam.nCurValue; if (nPayloadSize > m_nBufSizeForDriver) { if (m_BufForDriver != IntPtr.Zero) { Marshal.Release(m_BufForDriver); } m_nBufSizeForDriver = nPayloadSize; m_BufForDriver = Marshal.AllocHGlobal((Int32)m_nBufSizeForDriver); } if (m_BufForDriver == IntPtr.Zero) { MessageBox.Show("采集失败,请重新连接设备", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 将海康数据类型转为Mat nRet = cameraArr1[0].MV_CC_GetOneFrameTimeout_NET(m_BufForDriver, nPayloadSize, ref pFrameInfo, 1000); // m_BufForDriver为图像数据接收指针 //pTemp = m_BufForDriver; byte[] byteImage = new byte[pFrameInfo.nHeight * pFrameInfo.nWidth]; Marshal.Copy(m_BufForDriver, byteImage, 0, pFrameInfo.nHeight * pFrameInfo.nWidth); Mat matImage = new Mat(pFrameInfo.nHeight, pFrameInfo.nWidth, MatType.CV_8UC1, byteImage); // 单通道图像转为三通道 Mat matImageNew = new Mat(); Cv2.CvtColor(matImage, matImageNew, ColorConversionCodes.GRAY2RGB); Bitmap bitmap = matImageNew.ToBitmap(); // Mat转为Bitmap // 是否进行推理 DeepLearning deepLearning = new DeepLearning(); if (isInference2) { bitmap = deepLearning.Inference(model2, modelType, bitmap); } if (pictureBox2.InvokeRequired) // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 { UpdateUI update = delegate { pictureBox2.Image = bitmap; }; pictureBox2.BeginInvoke(update); } else { pictureBox2.Image = bitmap; } }
private void OnImageGrabbed_2(Object sender, ImageGrabbedEventArgs e) { try { IGrabResult grabResult = e.GrabResult; if (grabResult.IsValid) { // 四通道RGBA Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb); // 锁定位图的位 BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); // 将指针放置到位图的缓冲区 converter.OutputPixelFormat = PixelType.BGRA8packed; IntPtr ptrBmp = bmpData.Scan0; converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult); bitmap.UnlockBits(bmpData); DeepLearning deepLearning = new DeepLearning(); if (isInference2) { bitmap = deepLearning.Inference(model2, modelType, bitmap); } if (pictureBox2.InvokeRequired) // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 { UpdateUI update = delegate { pictureBox2.Image = bitmap; }; pictureBox2.BeginInvoke(update); } else { pictureBox2.Image = bitmap; } } } catch (Exception ex) { MessageBox.Show(string.Format("CAM:{0}!\n{1}", "CAM2", ex.Message)); } finally { e.DisposeGrabResultIfClone(); } }