大恒工业相机采集的帧数据转为其他8bit图像格式
- C#
- 转为bmp格式
- 转为Halcon的HObject格式
- 转为OpenCVSharp的Mat格式
回调采集图像的数据类型为IFrameData,单帧采集的数据类型为IImageData,两者的区别为IImageData类多了一个**Destroy()**方法
C#
转为bmp格式
//黑白相机
public static Bitmap Tobmp(IImageData)
{int m_width = (int)IImageData.GetWidth();int m_height = (int)IImageData.GetHeight();IntPtr pBufferMono = IImageData.GetBuffer();Bitmap bmp = new Bitmap(m_width, m_height, m_width, System.Drawing.Imaging.PixelFormat.Format8bppIndexed,pBufferMono);//添加调色板ColorPalette palette;palette = bmp.Palette;int i = 0;for (i = 0; i <= 255; i++)palette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);bmp.Palette = palette;return bmp;
}
//彩色相机
public static Bitmap Tobmp(IImageData)
{int m_width = (int)IImageData.GetWidth();int m_height = (int)IImageData.GetHeight();IntPtr pBufferColor = IImageData.ConvertToRGB24(GX_VALID_BIT_LIST.GX_BIT_0_7, GX_BAYER_CONVERT_TYPE_LIST.GX_RAW2RGB_NEIGHBOUR, false);Bitmap bmp = new Bitmap(m_width, m_height, m_width * 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb, pBufferColor);return bmp;
}
转为Halcon的HObject格式
//黑白相机
public static HObject ToHObject(IImageData)
{int m_width = (int)IImageData.GetWidth();int m_height = (int)IImageData.GetHeight();HOperatorSet.GenImage1(out HObject ho_image, "byte", m_width, m_height, IImageData.GetBuffer());return ho_image;
}
//彩色相机
public static HObject ToHObject(IImageData)
{int m_width = (int)IImageData.GetWidth();int m_height = (int)IImageData.GetHeight();IntPtr pBufferColor = IImageData.ConvertToRGB24(GX_VALID_BIT_LIST.GX_BIT_0_7, GX_BAYER_CONVERT_TYPE_LIST.GX_RAW2RGB_NEIGHBOUR, false);HOperatorSet.GenImageInterleaved(out HObject ho_image, pBufferColor, "bgr", m_height, m_height, -1, "byte", m_width, m_height, 0, 0, -1, 0);return ho_image;
}
转为OpenCVSharp的Mat格式
//黑白相机
public static Mat ToMat(IImageData)
{int height = (int)IImageData.GetHeight();int width = (int)IImageData.GetWidth();byte[] m_byMonoBuffer = new byte[width * height];Marshal.Copy(IImageData.GetBuffer(), m_byMonoBuffer, 0, width * height);Mat image = new Mat(height, width, MatType.CV_8UC1, m_byMonoBuffer); return image;
}
//彩色相机
public static Mat ToMat(IImageData)
{int height = (int)IImageData.GetHeight();int width = (int)IImageData.GetWidth();IntPtr pBufferColor = IImageData.ConvertToRGB24(GX_VALID_BIT_LIST.GX_BIT_0_7, GX_BAYER_CONVERT_TYPE_LIST.GX_RAW2RGB_NEIGHBOUR, false);byte[] m_byColorBuffer = new byte[(width * 3) * height];Marshal.Copy(pBufferColor, m_byColorBuffer, 0, (width * 3) * height);Mat image = new Mat(height, width, MatType.CV_8UC3, m_byColorBuffer); return image;
}