海康工业相机MVS抓图图像转HObject格式

使用海康工业彩色面阵相机,实时显示预览视频的时候,抓图到子线程中做图像处理。

public void GrabImage()
        {
            UInt32 nPayloadSize = 0;
            int nRet;
            int temp = m_pOperator.GetIntValue("PayloadSize", ref nPayloadSize);
            if (MyCamera.MV_OK != temp)
            {
                return ;
            }
   
            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();
            uint nDataLen = 0;
            temp = m_pOperator.GetOneFrameTimeout(pData, ref nDataLen,m_nBufSizeForDriver, ref stFrameInfo, 1000);//获取一帧图像,超时时间设置为1000
            if (MyCamera.MV_OK != temp)
            {
                MessageBox.Show("抓图失败!");
                return ;
            }


            IntPtr pImageBuf = IntPtr.Zero;
            int nImageBufSize = 0;

            HObject Hobj = new HObject();
            IntPtr pTemp = IntPtr.Zero;

            int nFrameSize = stFrameInfo.nWidth * stFrameInfo.nHeight * 3;

            Console.WriteLine("image is color pix format...");
            if (IsColorPixelFormat(stFrameInfo.enPixelType))
            {
                if (stFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed)
                {
                    Console.WriteLine("image pixel format is rgb8...");
                    pTemp = pData;
                }
                else
                {
                    if (IntPtr.Zero == pImageBuf || nImageBufSize < nFrameSize)
                    {
                        if (pImageBuf != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(pImageBuf);
                            pImageBuf = IntPtr.Zero;
                        }

                        pImageBuf = Marshal.AllocHGlobal((int)stFrameInfo.nWidth * stFrameInfo.nHeight * 3);
                        if (IntPtr.Zero == pImageBuf)
                        {
                            MessageBox.Show("图像分配内存失败!");
     
                            return;
                        }
                        nImageBufSize = stFrameInfo.nWidth * stFrameInfo.nHeight * 3;
                    }
                    Console.WriteLine("conver image format...");
                    MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();

                    stPixelConvertParam.pSrcData = pData;//源数据
                    stPixelConvertParam.nWidth = stFrameInfo.nWidth;//图像宽度
                    stPixelConvertParam.nHeight = stFrameInfo.nHeight;//图像高度
                    stPixelConvertParam.enSrcPixelType = stFrameInfo.enPixelType;//源数据的格式
                    stPixelConvertParam.nSrcDataLen = stFrameInfo.nFrameLen;

                    stPixelConvertParam.nDstBufferSize = (uint)nImageBufSize;
                    stPixelConvertParam.pDstBuffer = pImageBuf;//转换后的数据
                    stPixelConvertParam.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
                    nRet = m_pOperator.ConvertPixelType_NET(ref stPixelConvertParam);//格式转换
                    if (MyCamera.MV_OK != nRet)
                    {
                        MessageBox.Show("图像格式转换失败!");
 
                        return;
                    }
                    pTemp = pImageBuf;
                }

                try
                {
                    Console.WriteLine("conver image to hobject...");
                    HOperatorSet.GenImageInterleaved(out Hobj, (HTuple)pTemp, (HTuple)"rgb", (HTuple)stFrameInfo.nWidth, (HTuple)stFrameInfo.nHeight, -1, "byte", 0, 0, 0, 0, -1, 0);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());

                    return;
                }
            }
            else if (IsMonoPixelFormat(stFrameInfo.enPixelType))
            {
                if (stFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
                {
                    pTemp = pData;
                }
                else
                {
                    if (IntPtr.Zero == pImageBuf || nImageBufSize < (stFrameInfo.nWidth * stFrameInfo.nHeight))
                    {
                        if (pImageBuf != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(pImageBuf);
                            pImageBuf = IntPtr.Zero;
                        }

                        pImageBuf = Marshal.AllocHGlobal((int)stFrameInfo.nWidth *stFrameInfo.nHeight);
                        if (IntPtr.Zero == pImageBuf)
                        {
                           
                            return;
                        }
                        nImageBufSize = stFrameInfo.nWidth * stFrameInfo.nHeight;
                    }

                    MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();

                    stPixelConvertParam.pSrcData = pData;//源数据
                    stPixelConvertParam.nWidth =stFrameInfo.nWidth;//图像宽度
                    stPixelConvertParam.nHeight = stFrameInfo.nHeight;//图像高度
                    stPixelConvertParam.enSrcPixelType =stFrameInfo.enPixelType;//源数据的格式
                    stPixelConvertParam.nSrcDataLen =stFrameInfo.nFrameLen;

                    stPixelConvertParam.nDstBufferSize = (uint)nImageBufSize;
                    stPixelConvertParam.pDstBuffer = pImageBuf;//转换后的数据
                    stPixelConvertParam.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
                    nRet = m_pOperator.ConvertPixelType_NET(ref stPixelConvertParam);//格式转换
                    if (MyCamera.MV_OK != nRet)
                    {

                        return;
                    }
                    pTemp = pImageBuf;
                }
                try
                {
                    HOperatorSet.GenImage1Extern(out Hobj, "byte", stFrameInfo.nWidth, stFrameInfo.nHeight, pTemp, IntPtr.Zero);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("转换图像格式失败:"+ex);
        
                    return;
                }
            }
            else
            {

                return;
            }
            Console.WriteLine("display image...");
            HalconDisplay(hWindowControlDisplay.HalconWindow, Hobj, stFrameInfo.nHeight, stFrameInfo.nWidth);
            m_ho_SrcImage = Hobj.Clone();//m_ho_SrcImage为HObject类型成员变量
            if (pImageBuf != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(pImageBuf);
                pImageBuf = IntPtr.Zero;
            }


        }

后知后觉、越学越菜
原文地址:https://www.cnblogs.com/chenhuanting/p/14029305.html