我正在尝试将从Basler相机捕获的帧转换为OpenCV的Mat格式。 Basler API文档中没有很多信息,但这些是Basler示例中的两行,在确定输出格式时应该有用:
// Get the pointer to the image buffer
const uint8_t *pImageBuffer = (uint8_t *) Result.Buffer();
cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;
我知道图像格式是什么(目前设置为单声道8位),并尝试过:
img = cv::Mat(964, 1294, CV_8UC1, &pImageBuffer);
img = cv::Mat(964, 1294, CV_8UC1, Result.Buffer());
两者都不奏效。任何建议/意见将不胜感激,谢谢!
编辑:我可以通过以下方式访问Basler图像中的像素:
for (int i=0; i<1294*964; i++)
(uint8_t) pImageBuffer[i];
如果这有助于将其转换为OpenCV的Mat格式。
答案 0 :(得分:6)
您正在创建cv图像以使用相机的内存 - 而不是拥有自己内存的图像。问题可能是相机锁定了指针 - 或者可能希望在每个新图像上重新分配和移动它
尝试在没有最后一个参数的情况下创建图像,然后使用memcpy()将像素数据从相机复制到图像。
// Danger! Result.Buffer() may be changed by the Basler driver without your knowing
const uint8_t *pImageBuffer = (uint8_t *) Result.Buffer();
// This is using memory that you have no control over - inside the Result object
img = cv::Mat(964, 1294, CV_8UC1, &pImageBuffer);
// Instead do this
img = cv::Mat(964, 1294, CV_8UC1); // manages it's own memory
// copies from Result.Buffer into img
memcpy(img.ptr(),Result.Buffer(),1294*964);
// edit: cvImage stores it's rows aligned on a 4byte boundary
// so if the source data isn't aligned you will have to do
for (int irow=0;irow<964;irow++) {
memcpy(img.ptr(irow),Result.Buffer()+(irow*1294),1294);
}
答案 1 :(得分:0)
从 Pylon cam 获取 Mat 框架的 C++ 代码
Pylon::DeviceInfoList_t devices;
... get pylon devices if you have more than a camera connected ...
pylonCam = new CInstantCamera(tlFactory->CreateDevice(devices[selectedCamId]));
Pylon::CGrabResultPtr ptrGrabResult;
Pylon::CImageFormatConverter formatConverter;
formatConverter.OutputPixelFormat = Pylon::PixelType_BGR8packed;
pylonCam->MaxNumBuffer = 30;
pylonCam->StartGrabbing(GrabStrategy_LatestImageOnly);
std::cout << " trying to get width and height from pylon device " << std::endl;
pylonCam->RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
formatConverter.Convert(pylonImage, ptrGrabResult);
Mat temp = Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t*)pylonImage.GetBuffer());