1.转Opencv Mat参考代码如下:
//Open cv 2.4.10
//for Mono
Mat Img;
uchar* test = (uchar *)(ptrGrabResult->GetBuffer());
Mat mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC1, test);
//for Color
Mat BayerImg;
uchar* test = (uchar *)(ptrGrabResult->GetBuffer());
Mat mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC1, test);
cvtColor(mat, BayerImg, CV_BayerGB2RGB); //CV_BayerGB2RGB 根据相机Bayer设置
2、转halcon HImage类型代码参考如下:
// 3、转halcon图像并保存
Hobject hImage;
unsigned char *Pointer;
int width,height;
height = ptrGrabResult->GetHeight();
width = ptrGrabResult->GetWidth();
Pylon::PixelType pPixeltype;
pPixeltype = ptrGrabResult->GetPixelType();
Pointer = (uint8_t *) ptrGrabResult->GetBuffer();
if(pPixeltype== PixelType_Mono8) //For Mono
{
gen_image1_extern(&hImage,"byte",(HTuple)width,(HTuple)height,(long)Pointer,NULL); //Mono
write_image(hImage,"bmp",0,"HalconTestMono.bmp");
}
else if(pPixeltype== PixelType_BayerGB8 || pPixeltype== PixelType_BayerBG8)//For Color
{
CImageFormatConverter converter;
CPylonImage targetImage;
converter.OutputPixelFormat = PixelType_RGB8packed ;
converter.Convert(targetImage,ptrGrabResult);
unsigned char *Pointer = (unsigned char*)targetImage.GetBuffer(); //for Color
1)
PointerR = (unsigned char*)targetImage.GetPlane(0).GetBuffer();
PointerG = (unsigned char*)targetImage.GetPlane(1).GetBuffer();
PointerB = (unsigned char*)targetImage.GetPlane(2).GetBuffer();
HalconCpp::GenImage3Extern(&HImage,"byte",(HTuple)width,(HTuple)height,(long)PointerR,(long)PointerG,(long)PointerB,NULL); //Color
2)
gen_image_interleaved (&hImage,(long)Pointer, "rgb", (HTuple)width,(HTuple)height, 0, "byte", 0, 0, 0, 0, 8, 0);
write_image(hImage,"bmp",0,"TestColor.bmp");
}