Basler相机Bayer转RGB或BGR的黑边问题
针对右侧和下侧黑边问题,对官方转换函数设置如下即可解决:
相机像素格式:BayerRG8
【C++】
CImageFormatConverter fc; fc.OutputPixelFormat = PixelType_BGR8packed; //用于OpenCV //fc.OutputPixelFormat = PixelType_RGB8packed; //用于Halcon //fc.InconvertibleEdgeHandling = InconvertibleEdgeHandling_SetZero; //默认右、下边缘补0 fc.InconvertibleEdgeHandling = InconvertibleEdgeHandling_Extend; //推荐设置,否则右、下有黑边 CPylonImage image; fc.Convert(image,ptrGrabResult); //转换为BGR后,赋给OpenCV Mat cv_img = cv::Mat(ptrGrabResult->GetHeight(),ptrGrabResult->GetWidth(), CV_8UC3,(uint8_t*)image.GetBuffer() ) ;
【C#】参考SDK案例 GUI_SampleMultiCam
其中 PixelDataConverter converter = new PixelDataConverter();
Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format24bppRgb); // Lock the bits of the bitmap. BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); converter.OutputPixelFormat = PixelType.BGR8packed; converter.Parameters[PLPixelDataConverter.InconvertibleEdgeHandling].SetValue("Extend"); //设置成Extend方式 // Place the pointer to the buffer of the bitmap. IntPtr ptrBmp = bmpData.Scan0; converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult); bitmap.UnlockBits(bmpData); //Windows和Basler两种存bitmap方式,效果一样 bitmap.Save("test.bmp", System.Drawing.Imaging.ImageFormat.Bmp); //推荐存为bmp ImagePersistence.Save(ImageFileFormat.Bmp, "sdk_test.bmp", grabResult); bitmap.Save("test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //jpg是压缩后的格式,像素数据损失较大 ImagePersistence.Save(ImageFileFormat.Jpeg, "sdk_test.jpg", grabResult);
【非官方转换函数】
Mat bayerRG 转 Mat BGR
Mat cv_bayer_img = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC1, (uint8_t*)ptrGrabResult->GetBuffer()); //相机的bayer像素赋给Mat bayer
之后直接使用此博客函数即可(虽然是BG2RGB但是也可用于RG转BGR) https://blog.csdn.net/zhoukehu_CSDN/article/details/126482534