将 Basler 图像转换为 OpenCV

发布于 2024-12-09 03:03:27 字数 709 浏览 0 评论 0原文

我正在尝试将从 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 格式。

I'm trying to convert frames captured from a Basler camera to OpenCV's Mat format. There isn't a lot of information from the Basler API documentation, but these are the two lines in the Basler example that should be useful in determining what the format of the output is:

// 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;

I know what the image format is (currently set to mono 8-bit), and have tried doing:

img = cv::Mat(964, 1294, CV_8UC1, &pImageBuffer);
img = cv::Mat(964, 1294, CV_8UC1, Result.Buffer());

Neither of which works. Any suggestions/advices would be much appreciated, thanks!

EDIT: I can access the pixels in the Basler image by:

for (int i=0; i<1294*964; i++)
  (uint8_t) pImageBuffer[i];

If that helps with converting it to OpenCV's Mat format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-12-16 03:03:27

您正在创建 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);
 }

You are creating the cv images to use the camera's memory - rather than the images owning their own memory. The problem may be that the camera is locking that pointer - or perhaps expects to reallocate and move it on each new image

Try creating the images without the last parameter and then copy the pixel data from the camera to the image using 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);
 }
最笨的告白 2024-12-16 03:03:27

从 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());

C++ code to get a Mat frame from a Pylon cam

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文