下面介绍了两款常用的工业相机海康机器人,Basler相机,通过QT调用相机SDK,并实现采图相机的功能。
Basler相机
在QT中调用Basler相机并显示抓取的一张图片,可以参考以下步骤:
- 首先,确保已经安装并成功配置了Basler相机SDK。
- 在QT项目中,包括以下头文件:
#include <pylon/PylonIncludes.h>
#include <pylon/BaslerUniversalInstantCamera.h>
#include <pylon/GrabResultPtr.h>
#include <pylon/DisplayImage.h>
- 在需要调用相机的地方,添加以下代码:
using namespace Pylon;
using namespace Basler_UniversalCameraParams;
// 创建一个相机实例
CBaslerUniversalInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
// 打开相机
camera.Open();
// 设置相机参数,如曝光时间、增益等
camera.ExposureTime.SetValue(10000); // 设置曝光时间为10ms
camera.Gain.SetValue(1); // 设置增益为1
// 设置相机图像格式
camera.PixelFormat.SetValue(PixelFormat_BGR8packed);
// 创建一个Pylon的显示窗口
Pylon::PylonAutoInitTerm autoInitTerm;
Pylon::CDisplayImage displayImage;
// 开始实时采集图像
camera.StartGrabbing(1);
// 循环获取图像并显示
while (camera.IsGrabbing())
{
// 等待图像采集完成
camera.RetrieveResult(5000, grabResult, TimeoutHandling_ThrowException);
// 检查图像是否有效
if (grabResult.IsValid())
{
// 将图像转换为Pylon的格式
Pylon::CImageFormatConverter converter;
converter.OutputPixelFormat = PixelType_BGR8packed;
Pylon::CPylonImage pylonImage;
converter.Convert(pylonImage, grabResult);
// 将图像显示在窗口中
displayImage.Attach(0, pylonImage);
displayImage.Show(1);
}
}
// 停止相机采集
camera.StopGrabbing();
// 关闭相机
camera.Close();
上述代码创建了一个相机实例,打开相机并设置曝光时间和增益等参数。然后,创建了一个Pylon的显示窗口,并通过循环不断采集图像并显示在窗口中。最后,停止相机采集和关闭相机。
请注意,以上代码仅供参考,具体实现可能需要根据项目的需求进行适当调整。
海康面阵相机
要使用QT和OpenCV调用海康面阵相机显示图像,可以按照以下步骤进行操作:
- 安装海康相机SDK
下载并安装海康相机SDK,将SDK的安装路径添加到系统环境变量中。 - 创建QT工程
使用QT创建一个新的工程,在.pro文件中添加对OpenCV的引用:
INCLUDEPATH += \
D:\opencv\include \
LIBS += \
D:\opencv\lib\-lopencv_world460
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/./release/ -lMvCameraControl
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/./debug/ -lMvCameraControl
INCLUDEPATH += $$PWD/Includes
DEPENDPATH += $$PWD/Includes
-
创建QT窗口
在mainwindow.h中定义一个Qt窗口,用于显示相机图像:#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #include <QImage> #include <opencv2/opencv.hpp> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: QLabel *imageLabel; // 用于显示图像的标签 cv::Mat frame; // 用于存储图像的Mat对象 QImage qImage; // 显示图像的QImage对象 public slots: void on_pushButton_clicked(); // 显示图像的槽函数 }; #endif // MAINWINDOW_H
-
实现QT窗口
在MainWindow的构造函数中添加代码来获取相机图像并显示:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "MvCameraControl.h"
#include <QMessageBox>
#include <windows.h>
bool g_bExit = false;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 初始化标签
imageLabel = new QLabel(this);
setCentralWidget(imageLabel);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
// 初始化相机
int nRet = MV_OK;
void* handle = NULL;
// 添加海康相机SDK相关代码,打开相机并设置参数
//初始化SDK
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
QMessageBox::information(NULL, "提示", "相机初始化失败。错误码"+QString::number(nRet,16));
return;
}
//选择设备并创建句柄
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[0]);
if (MV_OK != nRet)
{
QMessageBox::information(NULL, "错误", "创建句柄失败。错误码"+QString::number(nRet,16));
return;
}
//打开设备
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
QMessageBox::information(NULL, "错误", "相机打开失败。错误码"+QString::number(nRet,16));
return;
}
//设置触发模式为off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
QMessageBox::information(NULL, "错误", "设置触发模式失败。错误码"+QString::number(nRet,16));
return;
}
//开始取流
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
QMessageBox::information(NULL, "错误", "相机采集图像失败。错误码"+QString::number(nRet,16));
return;
}
Sleep(1000);
unsigned char *pDataForRGB;
pDataForRGB = (unsigned char*)malloc(1280 *960 * 4 + 2048);
MV_FRAME_OUT stOutFrame;
stOutFrame = {0};
nRet = MV_CC_GetImageBuffer(handle, &stOutFrame, 1000);
if (nRet == MV_OK)
{
MV_CC_PIXEL_CONVERT_PARAM CvtParam;
CvtParam.enSrcPixelType=stOutFrame.stFrameInfo.enPixelType;
CvtParam.enDstPixelType=PixelType_Gvsp_RGB8_Packed;
CvtParam.nHeight=stOutFrame.stFrameInfo.nHeight;
CvtParam.nWidth=stOutFrame.stFrameInfo.nWidth;
CvtParam.nDstBufferSize=stOutFrame.stFrameInfo.nWidth * stOutFrame.stFrameInfo.nHeight * 4 + 2048;
CvtParam.pSrcData=stOutFrame.pBufAddr;
CvtParam.pDstBuffer=pDataForRGB;
CvtParam.nSrcDataLen=stOutFrame.stFrameInfo.nFrameLen;
MV_CC_ConvertPixelType(handle,&CvtParam);
cv::Mat srcimg=cv::Mat(stOutFrame.stFrameInfo.nHeight,stOutFrame.stFrameInfo.nWidth,CV_8UC3,pDataForRGB);
cvtColor(srcimg,srcimg,cv::COLOR_RGB2BGR);
if (!srcimg.empty()) {
QImage qImage((const unsigned char*)(srcimg.data),
srcimg.cols,
srcimg.rows,
srcimg.step,
QImage::Format_RGB888);
imageLabel->setPixmap(QPixmap::fromImage(qImage));
imageLabel->resize(imageLabel->pixmap().size());
}
//setCentralWidget(imageLabel);
nRet = MV_CC_FreeImageBuffer(handle, &stOutFrame);
if(nRet != MV_OK)
{
return;
}
}
else
{
QMessageBox::information(NULL, "错误", "相机采集图像失败。错误码"+QString::number(nRet,16));
}
//停止取流
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
QMessageBox::information(NULL, "错误", "停止采集图像失败。错误码"+QString::number(nRet,16));
return;
}
//关闭设备
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
QMessageBox::information(NULL, "错误", "相机关闭失败。错误码"+QString::number(nRet,16));
return;
}
//销毁句柄
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
QMessageBox::information(NULL, "错误", "销毁句柄异常。错误码"+QString::number(nRet,16));
return;
}
handle = NULL;
}
以上是一个简单的示例,可以根据具体的需求和海康相机SDK的调用方式做相应的修改。