将Basler图像转换为OpenCV [英] Converting Basler image to OpenCV

查看:458
本文介绍了将Basler图像转换为OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将从Basler摄像机捕获的帧转换为OpenCV的Mat格式. Basler API文档中没有很多信息,但是这些是Basler示例中的两行,对于确定输出的格式是有用的:

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;

我知道图像格式是什么(当前设置为8位单声道),并且尝试过:

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!

我可以通过以下方式访问Basler图像中的像素:

I can access the pixels in the Basler image by:

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

如果这有助于将其转换为OpenCV的Mat格式.

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

推荐答案

您正在创建cv图像以使用相机的内存-而不是拥有自己内存的图像.问题可能是相机锁定了该指针-或可能希望重新分配并在每个新图像上移动它

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

尝试创建不带最后一个参数的图像,然后使用memcpy()将像素数据从相机复制到图像.

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);
 }

这篇关于将Basler图像转换为OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆