無論什麼品牌的相機,Bayer轉RGB都涉及到插值,因此建議使用官方SDK裏的函數進行轉換。針對Basler相機,代碼如下:
void BaslerCamera::toQImage(CGrabResultPtr ptrGrabResult, QImage &OutImage) { int width = static_cast<int>(ptrGrabResult->GetWidth()); int height = static_cast<int>(ptrGrabResult->GetHeight()); if (ptrGrabResult->GetPixelType() == Pylon::PixelType_Mono8) { uchar* buffer = (uchar*)ptrGrabResult->GetBuffer(); OutImage = QImage(buffer, width, height, QImage::Format_Grayscale8); } else //bayer格式等 { CImageFormatConverter fc; //CPylonImage* img = new CPylonImage(); //注意必須爲指針,否則 GetBuffer()不正確。注意在合適的地方new/delete //CPylonImage img; //通過官方函數先轉爲 RGB8 fc.OutputPixelFormat = PixelType_RGB8packed; fc.Convert(*img, ptrGrabResult); uchar* buffer = (uchar*)img->GetBuffer(); OutImage = QImage(buffer, width, height, QImage::Format_RGB888); } }
【多餘的話】
官方有下圖函數,但是貌似沒用。因此使用 CImageFormatConverter