海康相机抓图使用OpencvSharp转换成Mat格式

private int m_nBuffSizeForDriver = 0;
private int m_nBuffSizeForSaveImage = 0;
private IntPtr m_pBuffForDriver = IntPtr.Zero;
private IntPtr m_pBuffForSaveImage = IntPtr.Zero;
private byte[] buffForDiraver = null;
private byte[] buffForSaveImage =null;
public int GrabImage(ref Mat dst)
{

dst = new Mat();
int nRet = CO_FAIL;
int nPayloadSize = 0;
MyCamera.MVCC_INTVALUE_EX stIntValue = new MyCamera.MVCC_INTVALUE_EX();
nRet = m_pCSI.MV_CC_GetIntValueEx_NET("PayloadSize", ref stIntValue);
if (MyCamera.MV_OK != nRet)
{
return CO_FAIL;
}
nPayloadSize = (int)stIntValue.nCurValue;
if (m_pBuffForDriver == IntPtr.Zero)
{
m_nBuffSizeForDriver = nPayloadSize;
buffForDiraver = new byte[m_nBuffSizeForDriver];
m_pBuffForDriver = Marshal.UnsafeAddrOfPinnedArrayElement(buffForDiraver,0);

}
MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
nRet = m_pCSI.MV_CC_GetOneFrameTimeout_NET(m_pBuffForDriver, (uint)m_nBuffSizeForDriver, ref stFrameInfo, 1000);

if (MyCamera.MV_OK != nRet)
{
return CO_FAIL;
}

bool isMono = IsMonoPixelFormat(stFrameInfo.enPixelType);//判断是否为黑白图像

if (isMono)
{
dst = new Mat(stFrameInfo.nHeight, stFrameInfo.nWidth,
MatType.CV_8UC1, m_pBuffForDriver);
}
else
{
Console.WriteLine("彩色");
m_nBuffSizeForSaveImage = stFrameInfo.nWidth * stFrameInfo.nHeight * 3 + 2048;
if (m_pBuffForSaveImage == IntPtr.Zero)
{
buffForSaveImage = new byte[m_nBuffSizeForSaveImage];
m_pBuffForSaveImage = Marshal.UnsafeAddrOfPinnedArrayElement(buffForSaveImage,0);
}

MyCamera.MV_PIXEL_CONVERT_PARAM stConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
stConvertParam.nWidth = stFrameInfo.nWidth; //ch:图像宽 | en:image width
stConvertParam.nHeight = stFrameInfo.nHeight; //ch:图像高 | en:image height
stConvertParam.pSrcData = m_pBuffForDriver; //ch:输入数据缓存 | en:input data buffer
stConvertParam.nSrcDataLen = stFrameInfo.nFrameLen; //ch:输入数据大小 | en:input data size
stConvertParam.enSrcPixelType = stFrameInfo.enPixelType; //ch:输入像素格式 | en:input pixel format
stConvertParam.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed; //ch:输出像素格式 | en:output pixel format 适用于OPENCV的图像格式
//stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed; //ch:输出像素格式 | en:output pixel format
stConvertParam.pDstBuffer = m_pBuffForSaveImage; //ch:输出数据缓存 | en:output data buffer
stConvertParam.nDstBufferSize = (uint)m_nBuffSizeForSaveImage; //ch:输出缓存大小 | en:output buffer size
m_pCSI.MV_CC_ConvertPixelType_NET(ref stConvertParam);
dst = new Mat(stFrameInfo.nHeight, stFrameInfo.nWidth,
MatType.CV_8UC3, m_pBuffForSaveImage);

}

return CO_OK;

}

static bool IsMonoPixelFormat(MyCamera.MvGvspPixelType enType)
{
switch (enType)
{
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12_Packed:
return true;
default:
return false;
}
}

posted @ 2021-08-26 11:41  陈加爵  阅读(2977)  评论(0编辑  收藏  举报