1、采图UI显示
2、硬件连接和IP配置
2.1 外触发硬件接线
根据相机接线图,连接相机的电源的触发信号输入;
2.2 IP配置
Gige网口相机需要电脑网卡和相机在同一ip段内,支持动态、静态等方式配置IP,打开Pylon IP Configurator来匹配IP,如下
3、代码实现
基本采图功能Balser都有对应的Demo进行参考,这里只截取相机采图和部分参数设置功能演示。
balser_gigeCamera.h
#ifndef BALSER_GIGECAMERA_H
#define BALSER_GIGECAMERA_H
/***************************
* @projectName camera_test
* @brief Basler相机图像采集及参数设置功能;
* @author Alaric
* @date 2018-11-04
**************************/
#include <QImage>
#include "balser_gigecamera_global.h"
#include <QObject>
#include <pylon/PylonIncludes.h>
#include <pylon/BaslerUniversalInstantCamera.h>
#include <pylon/_BaslerUniversalCameraParams.h>
#include <QMutex>
using namespace std;
using namespace Pylon;
using namespace GenApi;
using namespace Basler_UniversalCameraParams;
class BALSER_GIGECAMERASHARED_EXPORT Balser_GigeCamera : public QObject, public CImageEventHandler
{
Q_OBJECT
public:
Balser_GigeCamera(QObject *parent = nullptr);
~Balser_GigeCamera();
enum BaslerCamera_Type{
Type_Basler_Freerun, //设置相机的内触发
Type_Basler_Line1, //设置相机的外触发
Type_Basler_ExposureTimeAbs, //设置相机的曝光时间
Type_Basler_GainRaw, //设置相机的增益
Type_Basler_AcquisitionFrameRateAbs, //设置相机的频率
Type_Basler_Width, //图片的宽度
Type_Basler_Height, //图片的高度
Type_Basler_LineSource, //灯的触发信号
};
void initCamera(); //初始化相机
void OpenCamera(); //打开相机
void CloseCamera(); //关闭相机
void deleteAll(); //删除相机
int getExposureTime(); //获得曝光时间
void setExposureTime(int time); //设置曝光时间
int getGain(); //获得增益
void setGain(int value); //设置增益
void setFrameRate(int value); //设置帧率
int getFrameRate(); //获得帧率
void OneKeyAutoExTime(); //一键自动曝光
void SetCamera(Balser_GigeCamera::BaslerCamera_Type index, int tmpValue = 0); // 设置各种参数
int GetCamera(Balser_GigeCamera::BaslerCamera_Type index); // 获取各种参数
void AutoExposureOnce(CBaslerUniversalInstantCamera& camera); //自动曝光
void StartAcquire(); //开始采集
void StopAcquire(); //结束采集
void StartSnap(); //抓图
bool GrabOnLine_Signal; //实时采图信号
signals:
void canShowImg(QImage img); //发送图像数据
public slots:
void WorkTypeSignal(int work_type); //接收图像处理信号,0为Once,1为Online;
private:
CBaslerUniversalInstantCamera m_basler;
CGrabResultPtr m_ptrGrabResult;
CImageFormatConverter m_formatConverter;//Basler 图片格式转换类
CPylonImage pylonImage; //Basler 图像格式
QMutex m_mutexLock;
int TestImg_WorkType = -1; //图像处理方式,默认-1为不处理,0为Once,1为Online;
QImage CopyImgToQImage(CGrabResultPtr ptrGrabResult);
protected:
virtual void OnImageGrabbed(CInstantCamera &camera, const CGrabResultPtr &grabResult);
};
#endif // BALSER_GIGECAMERA_H
这里balser采集到的BYTE格式的图像数据按照通道数转换成QImage格式,最后发送给UI线程进行显示。
balser_gigecamera.cpp
#include "balser_gigecamera.h"
#include <QDebug>
Balser_GigeCamera::Balser_GigeCamera(QObject *parent) : QObject(parent)
{
}
Balser_GigeCamera::~Balser_GigeCamera()
{
}
void Balser_GigeCamera::initCamera()
{
PylonInitialize();
m_basler.RegisterImageEventHandler(this, RegistrationMode_Append, Cleanup_Delete);
m_basler.Attach(CTlFactory::GetInstance().CreateFirstDevice(),Cleanup_Delete);
qDebug()<<"Using device " << m_basler.GetDeviceInfo().GetModelName()<<endl;
m_basler.Open();
if (!m_basler.IsOpen() || m_basler.IsGrabbing())
{
qDebug()<<"camera open failed"<<endl;
return;
}
}
void Balser_GigeCamera::CloseCamera()
{
if(m_basler.IsOpen()) {
m_basler.DetachDevice();
m_basler.Close();
m_basler.DestroyDevice();
m_ptrGrabResult.Release();
}
}
void Balser_GigeCamera::deleteAll()
{
//停止采集
if(GrabOnLine_Signal) {
StopAcquire();
}
//关闭摄像头
try
{
CloseCamera();
m_basler.DeregisterImageEventHandler(this);
//关闭库
qDebug() << "SBaslerCameraControl deleteAll: PylonTerminate" ;
PylonTerminate();
}
catch (const Pylon::GenericException& e)
{
qDebug() << e.what();
}
}
int Balser_GigeCamera::getExposureTime()
{
return GetCamera(Type_Basler_ExposureTimeAbs);
}
void Balser_GigeCamera::setExposureTime(int time)
{
SetCamera(Type_Basler_ExposureTimeAbs, time);
}
void Balser_GigeCamera::setGain(int value)
{
SetCamera(Type_Basler_GainRaw, value);
}
int Balser_GigeCamera::getGain()
{
return GetCamera(Type_Basler_GainRaw);
}
int Balser_GigeCamera::getFrameRate()
{
return GetCamera(Type_Basler_AcquisitionFrameRateAbs);
}
void Balser_GigeCamera::setFrameRate(int value)
{
SetCamera(Type_Basler_AcquisitionFrameRateAbs, value);
}
void Balser_GigeCamera::OneKeyAutoExTime()
{
AutoExposureOnce(m_basler);
}
void Balser_GigeCamera::SetCamera(Balser_GigeCamera::BaslerCamera_Type index, int tmpValue)
{
INodeMap &cameraNodeMap = m_basler.GetNodeMap();
switch (index) {
case Type_Basler_Freerun: {
CEnumerationPtr ptrTriggerSel = cameraNodeMap.GetNode ("TriggerSelector");
ptrTriggerSel->FromString("FrameStart");
CEnumerationPtr ptrTrigger = cameraNodeMap.GetNode ("TriggerMode");
#ifdef Real_Freerun
ptrTrigger->SetIntValue(0);
#else //Software
ptrTrigger->SetIntValue(1);
CEnumerationPtr ptrTriggerSource = cameraNodeMap.GetNode ("TriggerSource");
ptrTriggerSource->FromString("Software");
#endif
} break;
case Type_Basler_Line1: {
CEnumerationPtr ptrTriggerSel = cameraNodeMap.GetNode ("TriggerSelector");
ptrTriggerSel->FromString("FrameStart");
CEnumerationPtr ptrTrigger = cameraNodeMap.GetNode ("TriggerMode");
ptrTrigger->SetIntValue(1);
CEnumerationPtr ptrTriggerSource = cameraNodeMap.GetNode ("TriggerSource");
ptrTriggerSource->FromString("Line1");
} break;
case Type_Basler_ExposureTimeAbs: {
const CFloatPtr exposureTime = cameraNodeMap.GetNode("ExposureTimeAbs");
exposureTime->SetValue(tmpValue);
} break;
case Type_Basler_GainRaw: {
const CIntegerPtr cameraGen = cameraNodeMap.GetNode("GainRaw");
cameraGen->SetValue(tmpValue);
} break;
case Type_Basler_AcquisitionFrameRateAbs: {
const CBooleanPtr frameRate = cameraNodeMap.GetNode("AcquisitionFrameRateEnable");
frameRate->SetValue(TRUE);
const CFloatPtr frameRateABS = cameraNodeMap.GetNode("AcquisitionFrameRateAbs");
frameRateABS->SetValue(tmpValue);
} break;
case Type_Basler_Width: {
const CIntegerPtr widthPic = cameraNodeMap.GetNode("Width");
widthPic->SetValue(tmpValue);
} break;
case Type_Basler_Height: {
const CIntegerPtr heightPic = cameraNodeMap.GetNode("Height");
heightPic->SetValue(tmpValue);
} break;
case Type_Basler_LineSource: {
CEnumerationPtr ptrLineSource = cameraNodeMap.GetNode ("LineSource");
ptrLineSource->SetIntValue(2);
} break;
default:
break;
}
}
int Balser_GigeCamera::GetCamera(Balser_GigeCamera::BaslerCamera_Type index)
{
INodeMap &cameraNodeMap = m_basler.GetNodeMap();
switch (index) {
case Type_Basler_ExposureTimeAbs: {
const CFloatPtr exposureTime = cameraNodeMap.GetNode("ExposureTimeAbs");
return exposureTime->GetValue();
} break;
case Type_Basler_GainRaw: {
const CIntegerPtr cameraGen = cameraNodeMap.GetNode("GainRaw");
return cameraGen->GetValue();
} break;
case Type_Basler_AcquisitionFrameRateAbs: {
const CBooleanPtr frameRate = cameraNodeMap.GetNode("AcquisitionFrameRateEnable");
frameRate->SetValue(TRUE);
const CFloatPtr frameRateABS = cameraNodeMap.GetNode("AcquisitionFrameRateAbs");
return frameRateABS->GetValue();
} break;
case Type_Basler_Width: {
const CIntegerPtr widthPic = cameraNodeMap.GetNode("Width");
return widthPic->GetValue();
} break;
case Type_Basler_Height: {
const CIntegerPtr heightPic = cameraNodeMap.GetNode("Height");
return heightPic->GetValue();
} break;
default:
return -1;
break;
}
}
void Balser_GigeCamera::OnImageGrabbed(CInstantCamera &camera, const CGrabResultPtr &grabResult)
{
m_mutexLock.lock();
qDebug() <<"Capturest"<<endl;
if (grabResult->GrabSucceeded())
{
m_ptrGrabResult = grabResult;//将捕获到的图像传递出去
qDebug() <<"Captureok"<<endl;
QImage CurrentImg;
CurrentImg = CopyImgToQImage(m_ptrGrabResult);
if(TestImg_WorkType == 1)
{
emit canShowImg(CurrentImg);
// startImgProcess(CurrentImg);
qDebug() <<"Captureok"<<endl;
}
else{
emit canShowImg(CurrentImg);
}
}
m_mutexLock.unlock();
}
void Balser_GigeCamera::StartAcquire()
{
if ( !m_basler.IsGrabbing() ){
qDebug()<<"grabstart"<<endl;
GrabOnLine_Signal = true;
m_basler.StartGrabbing(GrabStrategy_LatestImageOnly,GrabLoop_ProvidedByInstantCamera);
}
else {
qDebug()<<"error"<<endl;
}
}
void Balser_GigeCamera::StartSnap()
{
m_basler.StartGrabbing(1);
CBaslerUniversalGrabResultPtr ptrGrabResult;
m_basler.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded())
{
qDebug()<<"snapok"<<endl;
QImage CurrentImg = CopyImgToQImage(ptrGrabResult);
emit canShowImg(CurrentImg);
if(TestImg_WorkType == 0){
// startImgProcess(CurrentImg);
TestImg_WorkType = -1;
}
}
}
void Balser_GigeCamera::StopAcquire()
{
if ( m_basler.IsGrabbing()){
GrabOnLine_Signal = false;
m_basler.StopGrabbing(); // 停止抓取图像
}
}
void Balser_GigeCamera::AutoExposureOnce(CBaslerUniversalInstantCamera& camera)
{
if ( !camera.ExposureAuto.IsWritable())
{
cout << "The camera does not support Exposure Auto." << endl << endl;
return;
}
camera.ExposureAuto.SetValue(ExposureAuto_Once);
int n = 0;
while (camera.ExposureAuto.GetValue() != ExposureAuto_Off)
{
++n;
//For demonstration purposes only. Wait until the image is shown.
WaitObject::Sleep(100);
//Make sure the loop is exited.
if (n > 100)
{
throw TIMEOUT_EXCEPTION( "The adjustment of auto exposure did not finish.");
}
}
}
void Balser_GigeCamera::WorkTypeSignal(int work_type)
{
TestImg_WorkType = work_type;
}
QImage Balser_GigeCamera::CopyImgToQImage(CGrabResultPtr ptrGrabResult)
{
QImage Qimg;
//格式转换
m_formatConverter.Convert(pylonImage, ptrGrabResult);
uchar * din = (uchar *)(pylonImage.GetBuffer()); //数据指针
//单通道Momo
if(IsMono(pylonImage.GetPixelType()))
{
qDebug()<<"黑白图像";
Qimg = QImage(din, ptrGrabResult->GetWidth(), ptrGrabResult->GetHeight(), QImage::Format_Indexed8);
}
else
{
//此处可以根据相机的格式获取特定格式的彩色图像
if(IsRGB(pylonImage.GetPixelType()))
{
Qimg = QImage(din, ptrGrabResult->GetWidth(), ptrGrabResult->GetHeight(), QImage::Format_RGB888);
}
}
return Qimg;
}
4、源码下载
相机采图及相机设置部分封装成动态库,可以在UI线程调用相机类来实现相机的加载,采图及参数设置操作。
源码下载:https://download.csdn.net/dow...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。