我目前正在开发一个使用Basler摄像机acA1300-30 am的机器视觉应用程序。为此,我正在使用Basler Pylon 4和OPENCV版本2.4.9,并且出现了一些问题。我正试图使用Pylon捕获一个图像,并将其转换为Mat格式以进行进一步的分析。由于处理时间的限制,我的目标是避免将图像保存到硬盘上,而是进行动态分析。
在下面的代码中,我尝试捕获图像,将其转换为Mat格式,并将其显示在一个新的窗口上,但是我得到的窗口是空的。如果有人能帮我找出我的错误,或者解释我如何以不同的方式达到我的目标,我将非常感激。(我可能应该补充一下,相机工作正常,我已经能够将图像保存到硬盘中)。
谢谢。
这是我的代码:
PylonAutoInitTerm autoInitTerm;
try
{
CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
cout << "Dispositivo usado:"<<camera.GetDeviceInfo().GetModelName()<<endl;
CGrabResultPtr ptrGrabResult;
camera.GrabOne(500,ptrGrabResult,TimeoutHandling_ThrowException);
if(ptrGrabResult->GrabSucceeded())
{
CPylonImage target;
CImageFormatConverter converter;
converter.OutputPixelFormat=PixelType_RGB8packed;
converter.OutputBitAlignment=OutputBitAlignment_MsbAligned;
converter.Convert(target,ptrGrabResult);
Mat image(target.GetWidth(),target.GetHeight(),CV_8UC1,target.GetBuffer(),Mat::AUTO_STEP);
if(image.empty())
{
cout << "No se pudo cargar la imagen Mat" << endl;
return -1;
}
cout << "La imagen se presenta en la ventana *Captura*" << endl;
namedWindow("Captura",WINDOW_AUTOSIZE);
imshow( "Captura", image );
}
else
{
cout<<"Error: "<<ptrGrabResult->GetErrorCode()<<" "<<ptrGrabResult->GetErrorDescription()<<endl;
}
}发布于 2014-06-09 07:35:43
你必须确保像素类型匹配。在您的代码示例中,您使用PixelType_RGB8packed作为相机图像,使用CV_8UC1作为Mat像素类型。您应该使用CV_8UC3代替。另外,我将使用PixelType_BGR8packed而不是PixelType_RGB8packed,因为BGR与Windows位图兼容。我假设你使用Windows。
发布于 2014-06-27 08:22:38
我似乎无法发表评论,但要完整地回答上面的问题:
它对我有用,因为它取代了:
Mat image(target.GetWidth(),target.GetHeight(),CV_16UC3,target.GetBuffer(),Mat::AUTO_STEP);通过
Mat image(target.GetHeight(),target.GetWidth(),CV_16UC3,target.GetBuffer(),Mat::AUTO_STEP);注高宽度
发布于 2014-08-11 12:31:33
为了修复对OpenCV数据(你的形象)的转换,可以以这种方式复制缓冲区
Mat image(target.GetHeight(), target.GetWidth(), CV_8UC3);
memcpy(image.ptr(),target.GetBuffer(),3*target.GetWidth()*target.GetHeight());对我起作用了。
https://stackoverflow.com/questions/24101877
复制相似问题