OpenCvSharp海康相机在pictureBox中显示——图像转成Mat,并在pictureBox中显示

        Mat srcImage;
        private Mat CaptureImage()
        {
                int nRet;
                //MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                UInt32 nPayloadSize = 0;
                nRet = m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    return null;
                }
                nPayloadSize = stParam.nCurValue;
                if (nPayloadSize > m_nBufSizeForDriver)
                {
                    m_nBufSizeForDriver = nPayloadSize;
                    m_pBufForDriver = new byte[m_nBufSizeForDriver];
                    m_nBufSizeForSaveImage = m_nBufSizeForDriver * 3 + 2048;
                    m_pBufForSaveImage = new byte[m_nBufSizeForSaveImage];
                }
 
                IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForDriver, 0);
                MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
                nRet = m_pMyCamera.MV_CC_GetOneFrameTimeout_NET(pData, m_nBufSizeForDriver, ref stFrameInfo, 1000);//获取一帧图像,延时时间设置为1000
                if (MyCamera.MV_OK != nRet)
                {
                    return null;
                }
                //转成Mat图像
                srcImage = new Mat(stFrameInfo.nHeight, stFrameInfo.nWidth, MatType.CV_8UC1, pData);
 
                return srcImage;
        }
 
        //点击按钮,在pictureBox上显示图像
        private void btnShow_Click(object sender, EventArgs e)
        {
                Mat srcImage = new Mat();
                //灰度图转BRG(也可以不转)
                Cv2.CvtColor(CaptureImage(), srcImage, ColorConversionCodes.GRAY2BGR);
                //在pictureBox上显示
                pictureBox1.Image = srcImage.ToBitmap();
        }

你可能感兴趣的:(OpenCV)