前言: 近期企业需要用大恒相机的sdk开发项目,我采用VS2017+QT5.10.1+MSVC。
一、环境配置
VS2017和qt的安装不多介绍,主要介绍大恒sdk的配置。 1.https://www.daheng-image.com/list-58-1.html 官网下载大恒相机的SDK包,选择自己对应的相机 2.解压到自己的文件夹 文件目录如下: 3.c++文件的配置主要有头文件、库文件。 头文件在项目中的配置: 右击项目-属性-VC++目录-包含目录 路径为刚才解压的文件夹下路径 库目录路径: 右击项目-属性-VC++目录-库目录 路径为解压文件下路径 附加库依赖 右击项目-属性-链接器-输入 注意事项:有第三方库,将dll文件加入项目文件下即可。这样库文件配置完成开始编写代码部分。我用了opencv。
二、图像采集
1.打开设备
注意事项,必须先进行设备初始化 IGXFactory::GetInstance().Init();
void DH_MER::Open_Device()
{
try
{
do
{
IGXFactory::GetInstance().Init();
gxdeviceinfo_vector vectorDeviceInfo;
IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
if (0 == vectorDeviceInfo.size())
{
cout << "无可用设备!" << endl;
break;
}
cout << vectorDeviceInfo.size() << endl;
ObjDevicePtr = IGXFactory::GetInstance().OpenDeviceBySN(
vectorDeviceInfo[0].GetSN(),
GX_ACCESS_EXCLUSIVE);
ObjStreamPtr = ObjDevicePtr->OpenStream(0);
pDeviceOfflineEventHandler = new CSampleDeviceOfflineEventHandler();
hDeviceOffline = ObjDevicePtr->RegisterDeviceOfflineCallback(pDeviceOfflineEventHandler, NULL);
ObjFeatureControlPtr = ObjDevicePtr->GetRemoteFeatureControl();
#ifdef _DEBUG
ObjFeatureControlPtr->GetIntFeature("GevHeartbeatTimeout")->SetValue(5000);
#endif
txt_factoryName = ObjFeatureControlPtr->GetStringFeature("DeviceVendorName")->GetValue();
txt_basicMode = ObjFeatureControlPtr->GetStringFeature("DeviceModelName")->GetValue();
cout << "成功打开第一个设备" << endl;
} while (0);
}
catch (CGalaxyException& e)
{
cout << "错误码: " << e.GetErrorCode() << endl;
cout << "错误描述信息: " << e.what() << endl;
}
catch (std::exception& e)
{
cout << "错误描述信息: " << e.what() << endl;
}
}
2.采集图像
void DH_MER::Pick_Picture()
{
pCaptureEventHandler = new CSampleCaptureEventHandler();
ObjStreamPtr->RegisterCaptureCallback(pCaptureEventHandler, this);
ObjStreamPtr->StartGrab();
ObjFeatureControlPtr->GetCommandFeature("AcquisitionStart")->Execute();
}
3.停止采集
void DH_MER::Stop_Picking()
{
ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
ObjStreamPtr->StopGrab();
}
4.采集图像事件处理 这里才是对采集的图像进行处理的地方
void CSampleCaptureEventHandler::DoOnImageCaptured(CImageDataPointer& objImageDataPointer, void* pUserParam)
{
DH_MER* camUiPtr = (DH_MER*)pUserParam;
cout << "收到一帧图像!" << endl;
camUiPtr->txt_width = QString::number(objImageDataPointer->GetWidth());
camUiPtr->txt_height = QString::number(objImageDataPointer->GetHeight());
camUiPtr->captureImgMat.create(objImageDataPointer->GetHeight(), objImageDataPointer->GetWidth(), CV_8UC3);
void* pRGB24Buffer = NULL;
pRGB24Buffer = objImageDataPointer->ConvertToRGB24(GX_BIT_0_7, GX_RAW2RGB_NEIGHBOUR, true);
memcpy(camUiPtr->captureImgMat.data, pRGB24Buffer, (objImageDataPointer->GetHeight()) * (objImageDataPointer->GetWidth()) * 3);
cout << "帧数:" << objImageDataPointer->GetFrameID() << endl;
}
5.相机线程调用实时采集
void WorkThread::run()
{
unsigned char ret;
if (1 == ThreadId)
{
cancl->Open_Device();
cancl->Pick_Picture();
}
while (1)
{
if (workcount)
{
cancl->Close_Device();
workcount = false;
break;
}
readFarme();
QThread::usleep(33);
}
}
6.相机采集的图像进行算法处理
因算法不便公开,只写图片传出部分。
只需要用lable展示QPixmap即可ui->label_9->setPixmap(fitpixmap);
QImage image(cancl->captureImgMat.cols, cancl->captureImgMat.rows, QImage::Format_Indexed8);
image.setColorCount(256);
for (int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
uchar *pSrc = cancl->captureImgMat.data;
for (int row = 0; row < cancl->captureImgMat.rows; row++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, cancl->captureImgMat.cols);
pSrc += cancl->captureImgMat.step;
}
QPixmap pixmap = QPixmap::fromImage(image);
emit UpdateImage(pixmap);
三、相机类代码
可以根据需求添加设备组和当前设备对象进行处理
CameraClass.h
#pragma once
#include <opencv2/opencv.hpp>
#include"GalaxyIncludes.h"
#include<QObject>
using namespace cv;
using namespace std;
class CameraClass :public QObject
{
Q_OBJECT
public:
CameraClass();
~CameraClass();
IDeviceOfflineEventHandler* pDeviceOfflineEventHandler = NULL;
GX_DEVICE_OFFLINE_CALLBACK_HANDLE hDeviceOffline = NULL;
IFeatureEventHandler* pFeatureEventHandler = NULL;
ICaptureEventHandler* pCaptureEventHandler = NULL;
CGXDevicePointer ObjDevicePtr;
CGXStreamPointer ObjStreamPtr;
CGXFeatureControlPointer ObjFeatureControlPtr;
Mat captureImgMat;
QString choose_triggerMode;
QString txt_triggerSource;
int txt_exposeTime;
double txt_gain;
QString txt_width;
QString txt_height;
QString txt_factoryName;
QString txt_basicMode;
public:
void Open_Device();
void Close_Device();
void Pick_Picture();
void Stop_Picking();
};
class CSampleCaptureEventHandler : public ICaptureEventHandler
{
public:
void DoOnImageCaptured(CImageDataPointer& objImageDataPointer, void* pUserParam);
};
class CSampleDeviceOfflineEventHandler : public IDeviceOfflineEventHandler
{
public:
void DoOnDeviceOfflineEvent(void* pUserParam)
{
cout << "收到设备掉线事件!" << endl;
}
};
CameraClass.cpp
#include "CameraClass.h"
CameraClass::CameraClass()
{
}
CameraClass::~CameraClass()
{
}
void CameraClass::Open_Device()
{
try
{
do
{
IGXFactory::GetInstance().Init();
gxdeviceinfo_vector vectorDeviceInfo;
IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
if (0 == vectorDeviceInfo.size())
{
cout << "无可用设备!" << endl;
break;
}
cout << vectorDeviceInfo.size() << endl;
ObjDevicePtr = IGXFactory::GetInstance().OpenDeviceBySN(
vectorDeviceInfo[0].GetSN(),
GX_ACCESS_EXCLUSIVE);
ObjStreamPtr = ObjDevicePtr->OpenStream(0);
pDeviceOfflineEventHandler = new CSampleDeviceOfflineEventHandler();
hDeviceOffline = ObjDevicePtr->RegisterDeviceOfflineCallback(pDeviceOfflineEventHandler, NULL);
ObjFeatureControlPtr = ObjDevicePtr->GetRemoteFeatureControl();
#ifdef _DEBUG
ObjFeatureControlPtr->GetIntFeature("GevHeartbeatTimeout")->SetValue(5000);
#endif
txt_factoryName = ObjFeatureControlPtr->GetStringFeature("DeviceVendorName")->GetValue();
txt_basicMode = ObjFeatureControlPtr->GetStringFeature("DeviceModelName")->GetValue();
cout << "成功打开第一个设备" << endl;
} while (0);
}
catch (CGalaxyException& e)
{
cout << "错误码: " << e.GetErrorCode() << endl;
cout << "错误描述信息: " << e.what() << endl;
}
catch (std::exception& e)
{
cout << "错误描述信息: " << e.what() << endl;
}
}
void CameraClass::Close_Device()
{
try
{
Stop_Picking();
ObjStreamPtr->UnregisterCaptureCallback();
ObjDevicePtr->UnregisterDeviceOfflineCallback(hDeviceOffline);
ObjStreamPtr->Close();
ObjDevicePtr->Close();
IGXFactory::GetInstance().Uninit();
if (NULL != pCaptureEventHandler)
{
delete pCaptureEventHandler;
pCaptureEventHandler = NULL;
}
if (NULL != pDeviceOfflineEventHandler)
{
delete pDeviceOfflineEventHandler;
pDeviceOfflineEventHandler = NULL;
}
if (NULL != pFeatureEventHandler)
{
delete pFeatureEventHandler;
pFeatureEventHandler = NULL;
}
cout << "关闭设备" << endl;
}
catch (const std::exception&)
{
}
}
void CameraClass::Pick_Picture()
{
pCaptureEventHandler = new CSampleCaptureEventHandler();
ObjStreamPtr->RegisterCaptureCallback(pCaptureEventHandler, this);
ObjStreamPtr->StartGrab();
ObjFeatureControlPtr->GetCommandFeature("AcquisitionStart")->Execute();
}
void CameraClass::Stop_Picking()
{
ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
ObjStreamPtr->StopGrab();
}
void CSampleCaptureEventHandler::DoOnImageCaptured(CImageDataPointer& objImageDataPointer, void* pUserParam)
{
CameraClass* camUiPtr = (CameraClass*)pUserParam;
cout << "收到一帧图像!" << endl;
camUiPtr->txt_width = QString::number(objImageDataPointer->GetWidth());
camUiPtr->txt_height = QString::number(objImageDataPointer->GetHeight());
camUiPtr->captureImgMat.create(objImageDataPointer->GetHeight(), objImageDataPointer->GetWidth(), CV_8UC3);
void* pRGB24Buffer = NULL;
pRGB24Buffer = objImageDataPointer->ConvertToRGB24(GX_BIT_0_7, GX_RAW2RGB_NEIGHBOUR, true);
memcpy(camUiPtr->captureImgMat.data, pRGB24Buffer, (objImageDataPointer->GetHeight()) * (objImageDataPointer->GetWidth()) * 3);
cout << "帧数:" << objImageDataPointer->GetFrameID() << endl;
}
四、效果展示
实际效果不错,可以看文档更改相机设置。 喜欢点个关注吧
|