将Basler图像转换为OpenCV

将Basler图像转换为OpenCV,opencv,Opencv,我正在尝试将从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) pI

我正在尝试将从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;
两者都不起作用。如有任何建议,将不胜感激,谢谢

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

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

for(int i=0;i创建cv图像是为了使用相机的内存,而不是拥有自己内存的图像。问题可能是相机锁定了指针,或者可能希望在每个新图像上重新分配和移动指针

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

//Basler驱动程序可能会在您不知道的情况下更改Danger!Result.Buffer()
const uint8_t*pImageBuffer=(uint8_t*)Result.Buffer();
//这是在结果对象内部使用您无法控制的内存
img=cv::Mat(9641294、cv_8UC1和pImageBuffer);
//而是这样做
img=cv::Mat(9641294,cv_8UC1);//管理它自己的内存
//从Result.Buffer复制到img
memcpy(img.ptr(),Result.Buffer(),1294*964);
//编辑:cvImage在4字节边界上存储对齐的行
//因此,如果源数据未对齐,则必须执行以下操作

对于(int-irow=0;irowC++代码,从塔架凸轮获取垫架

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());
Pylon::设备信息列表设备;
…如果连接了多个摄像头,请获取挂架设备。。。
pylonCam=new CInstantCamera(tlFactory->CreateDevice(devices[selectedCamId]);
Pylon::cGrabResultPtrGrabResult;
Pylon::CImageFormatConverter格式转换器;
formatConverter.OutputPixelFormat=Pylon::PixelType_BGR8packed;
塔柱->MaxNumBuffer=30;
塔柱->开始拉拔(仅限最新估计);
std::cout GetHeight(),ptrGrabResult->GetWidth(),CV_8UC3,(uint8_t*)pylonImage.GetBuffer();

你说它不工作是什么意思?它崩溃了吗?或者你得到了一个有图案的图像(即,你离开了几行或几列,甚至可能是几个频道)?如果有输出,您可以发布吗?看起来您使用的是1294x964的全帧参数。这些是Basler返回的真正有效的行,还是它们是前/后有效行,并且是1280x960?相机提供的是拜耳输出还是单色输出?嗨,代码编译并运行,但是如果我尝试使用
cv::imshow显示图像()
,我会弹出一个错误窗口,显示“basler\u camera.exe已停止工作”.1294*964是相机的最大分辨率,因此这是正确的。我认为问题在于从
pImageBuffer
复制到
img
,但无法正确执行。您好,您能提供一个示例说明您的意思吗?感谢代码。不幸的是,这似乎不起作用,因为图像不是copied.目前我已经通过
for(int I=0;iYou的
memcpy
参数错误地复制了图像像素。它应该是
memcpy(img.ptr(),Result.Buffer(),basler_width*basler_height)
。函数定义是
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);
 }
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());