我正在尝试将从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格式。
发布于 2011-10-13 05:31:25
您创建的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);
}发布于 2021-04-28 22:36:20
用于从塔式凸轮获取垫框架的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());https://stackoverflow.com/questions/7734469
复制相似问题