使用海康工業彩色面陣相機,實時顯示預覽視頻的時候,抓圖到子線程中做圖像處理。
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;
}
}