如何将Pylon图像(来自Basler相机库)转换为C++语言的DIPlib图像?
以下代码说明了如何将Pylon图像转换为OpenCV图像:
// Convert the grabbed buffer to a pylon image.
formatConverter.Convert(pylonImage, ptrGrabResult);
// Create an OpenCV image from a pylon image.
openCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *)pylonImage.GetBuffer());
cvtColor(openCvImage, openCvImage, COLOR_BGR2GRAY);
最佳答案
我在网上搜索了有关Pylon API的文档,但找不到它。尽管如此,使用您提供的OpenCV示例,在DIPlib中很容易做到等效:
dip::Image image(
NonOwnedRefToDataSegment(pylonImage.GetBuffer()), // a random, non-owning pointer
pylonImage.GetBuffer(), // data
dip::DT_UINT8, // pixel type (8-bit unsigned)
{ ptrGrabResult->GetWidth(),
ptrGrabResult->GetHeight() }, // width and heigh
{}, // this one can stay empty
dip::Tensor{3} // 3 channels
);
image.SetColorSpace("RGB");
有关更多详细信息,请参见this constructor和this section of the DIPlib documentation的文档。
将DIPlib更新到当前的master分支(我刚刚将this change推送到了上面)简化为:
dip::Image image(
static_cast<uint8_t*>(pylonImage.GetBuffer()) // data pointer
{ ptrGrabResult->GetWidth(),
ptrGrabResult->GetHeight() }, // width and heigh
3 // 3 channels
);
image.SetColorSpace("RGB");