VS+QT 巴斯勒相机、液态镜头软件二次开发
搞了一个多月,算是终于把这两个软件界面给写好了,期间各类bug和问题真是搞得我脑壳疼,在此记录一下。
一、巴斯勒相机
这个阶段我总结的经验就是在开发相关硬件时,先是阅读官方提供的SDK文档,搞清大部分接口函数的用法,把提供的例程跑一遍看看是什么样的效果,可以减少后面踩坑的概率,不要一上来看了一点东西就开始无脑搞下去,磨刀不误砍柴工,当然是在时间充足情况下。再是查找一些相关的资料代码,看看别人是怎么写的,有什么逻辑与问题,CSDN以及github找几个看看,会有意外的收获。
1、首先还是环境的配置,网上很多资料可以参考,注意版本和平台区别,以及添加包含目录以及库目录时对debug或者release不同的版本区别(第一个坑)
2、配置完环境后是对qt界面的设计。
分为几个部分,一是相机参数的调整,包括触发模式、获取速率、增益自动以及增益值、曝光自动以及曝光值;二是打开相机、保存图像、停止拍照;三是获取相机型号和IP;
要注意的点是获取速率也受图片大小和带宽的影响;qt界面控件的设计布局也是需要自行学习一下,水平垂直策略会影响布局显示时的问题。
3、代码部分
这里着重讲几个踩过的坑:
-
调试程序后,相机一直被占用???
巴斯勒相机有一个参数叫心跳时间,用于检查相机与网卡的连接状态,即每隔一个心跳时间会检查一次,在此期间相机是一直与电脑连接的,即使程序已经调试完终止,默认为5分钟。所以每调试一次,会有5分钟的连接,除非断开相机或者设置心跳时间。
参考:https://blog.csdn.net/kevin_lp/article/details/104002310
-
获取nodemap失败???
也是心跳时间的锅。。。在调试时,若调试时间过长超过心跳时间,系统默认断开相机,则获取nodemap失败。。。 -
获取到的图像转换为QT支持的类型
这个初期写程序时卡bug了,以为是图像信息过大的问题,导致获取图像后界面一直没有反应,按键也按不了,界面卡死。找了一个解决的方法是加了一行代码处理一些事件,这里还不是很懂。
参考:Qt主界面卡死的解决方案-一些具体实现方式 - 张小飞的文章 - 知乎
https://zhuanlan.zhihu.com/p/91143055
-
图像显示线程问题
这个问题从一开始就出现了,显示图像时会跳出弹窗显示Cannot send events to objects owned by a different thread;点击忽略后还是会显示,就先不管他,赶进度。。。后面开始尝试解决它,查资料说是在一些线程中开创的对象在另一线程delete,又在一些参考过的代码中看到线程锁的问题。从以为是图片的问题,到以为是改变了widget大小,结果还是会跳出警告弹窗。后面看了几篇博客说是相机获取图像是在不同线程,终于自己悟了(其实是线程发送图像的问题,虽然之前学习了qt的基本使用,也知道线程的基本概念,没想到在这踩了这么久)。
解决方法先在图像处理线程中发送获取到图像的信号,再由相机类向主窗口发送获取到的图像,即图像处理子线程不能发送图像到主窗口线程中且操控相关控件。至于提到的线程锁问题,好像不加也没问题,再考究。
参考:https://blog.csdn.net/dongfangyu/article/details/5972764
由于代码过多,没办法全部贴上,贴主要部分。
//初始化相机心跳时间、图像大小设置、获取相机信息、设置帧率、增益值、曝光值等
void Camera::Initialize()
{
try
{
PylonInitialize();
m_camera = new CBaslerGigEInstantCamera(CTlFactory::GetInstance().CreateFirstDevice());
CIntegerPtr m_heartbeatTimeout = m_camera->GetTLNodeMap().GetNode("HeartbeatTimeout");
m_heartbeatTimeout->SetValue(10 * 1000); //单位毫秒,设置心跳时间,检查相机与网卡的连接状态,防止占用,在调试时会暂停发送心跳信号,会让相机认为断开了连接
nodemap = &m_camera->GetNodeMap(); //!!!需打开相机才可以设置参数
OpenCamera(); //根据不同采集触发模式配置不同抓取及处理模式
//获取相机信息
m_deviceModelName = m_camera->GetDeviceInfo().GetModelName();
m_deviceIP = m_camera->GetDeviceInfo().GetAddress();
}
catch (GenICam::GenericException &e)
{
qDebug() << "Initialize Error" << QString(e.GetDescription());
}
}
//设置相机参数
void Camera::SetCameraPara(CameraControl_Type index, int tmpValue)
{
switch (index)
{
case Type_Basler_FreeRun:
{
CEnumerationPtr ptrTriggerSel = nodemap->GetNode("TriggerSelector");
ptrTriggerSel->FromString("FrameStart");
CEnumerationPtr ptrTrigger = nodemap->GetNode("TriggerMode"); //打开触发开关
ptrTrigger->SetIntValue(0);
CEnumerationPtr ptrTriggerSource = nodemap->GetNode("TriggerSource");
m_triggerMode = freeRun;
}break;
case Type_Basler_Line:
{
CEnumerationPtr ptrTriggerSel = nodemap->GetNode("TriggerSelector");
ptrTriggerSel->FromString("FrameStart");
CEnumerationPtr ptrTrigger = nodemap->GetNode("TriggerMode");
ptrTrigger->SetIntValue(1);
CEnumerationPtr ptrTriggerSource = nodemap->GetNode("TriggerSource");
ptrTriggerSource->FromString("Line1");
m_triggerMode = lineTrigger;
}break;
case Type_Basler_ExposureAuto:
{
m_camera->AutoFunctionAOISelector.SetValue(AutoFunctionAOISelector_AOI1);
m_camera->AutoFunctionAOIOffsetX.SetValue(m_camera->OffsetX());
m_camera->AutoFunctionAOIOffsetY.SetValue(m_camera->OffsetY());
m_camera->AutoFunctionAOIWidth.SetValue(m_width);
m_camera->AutoFunctionAOIHeight.SetValue(m_height);
m_camera->AutoTargetValue.SetValue(80); //设置亮度控制的目标值???
m_camera->ExposureAuto.SetValue(ExposureAuto_Continuous);
}break;
case Type_Basler_ExposureTimeAbs:
{
m_camera->ExposureAuto.SetValue(ExposureAuto_Off); //关闭自动曝光
CFloatPtr exposureTime = nodemap->GetNode("ExposureTimeAbs");
exposureTime->SetValue(double(tmpValue));
qDebug() << exposureTime->GetValue();
}break;
case Type_Basler_GainAuto:
{
m_camera->AutoFunctionAOISelector.SetValue(AutoFunctionAOISelector_AOI1);
m_camera->AutoFunctionAOIOffsetX.SetValue(m_camera->OffsetX());
m_camera->AutoFunctionAOIOffsetY.SetValue(m_camera->OffsetY());
m_camera->AutoFunctionAOIWidth.SetValue(m_width);
m_camera->AutoFunctionAOIHeight.SetValue(m_height);
m_camera->AutoTargetValue.SetValue(80); //设置亮度控制的目标值???
m_camera->GainAuto.SetValue(GainAuto_Continuous);
}break;
case Type_Basler_GainRaw:
{
m_camera->GainAuto.SetValue(GainAuto_Off); //关闭自动增益
const CIntegerPtr cameraGen = nodemap->GetNode("GainRaw");
cameraGen->SetValue(tmpValue);
}break;
case Type_Basler_AcquisitionFrameRateAbs:
{
const CBooleanPtr frameRate = nodemap->GetNode("AcquisitionFrameRateEnable");
frameRate->SetValue(TRUE);
const CFloatPtr frameRateABS = nodemap->GetNode("AcquisitionFrameRateAbs");
frameRateABS->SetValue(tmpValue);
}break;
case Type_Basler_Width:
{
try
{
const CIntegerPtr Width = nodemap->GetNode("Width");
Width->SetValue(tmpValue);
}
catch (const GenericException& e)
{
qDebug() << "Set Image Width Error:" << QString(e.GetDescription());
}
}break;
case Type_Basler_Height:
{
const CIntegerPtr Height = nodemap->GetNode("Height");
Height->SetValue(tmpValue);
}break;
}
}
//重载图像处理虚函数
void Camera::OnImageGrabbed(Pylon::CInstantCamera &camera, const Pylon::CGrabResultPtr &grabResult)
{
m_mutex.lock();
if (grabResult->GrabSucceeded())
{
uint32_t width = grabResult->GetWidth();
uint32_t height = grabResult->GetHeight();
Mat imageMat(height, width, CV_8UC1, (uint8_t*)(grabResult->GetBuffer()));
//QApplication::processEvents(); //处理事件,防止界面卡死
m_image = imageMat.clone(); //将图像传递出去
if (!imageMat.empty())
{
emit signalGrabAImage();
}
}
else
qWarning() << QString("An exception occurred: ") << grabResult->GetErrorDescription();
m_mutex.unlock();
}
后续再把代码上传到CSDN以及github。