环境介绍
- 安装好QT5.13.1,安装过程看我的QT5.13.1的安装文档
- 安装好MVS,这个百度下去官网下载即可。
- 配置好MSVC的环境,不能是MinGW的环境,不然配置不成功!!!这个的配置也可以参考我的MSVC2017安装文档。
- 配置QT的opencv环境,我这边配置的是opencv3.4.2
MVS需要验证,这个需要有权限
下面开始配置opencv–QT版本
找到下面对应的文件
复制上面的所有文件,然后拷贝到C:\Windows\SysWOW64这个文件夹下
然后创建QT工程,QT工程自己创建一个。然后找到工程文件,添加环境配置。
对着下面改,改成自己的对应的文件路径
win32: LIBS += -L’C:/Program Files(x86)/MVS/Development/Libraries/win64/’ -lMvCameraControl
INCLUDEPATH += ‘C:/Program Files (x86)/MVS/Development/Includes’
DEPENDPATH += ‘C:/Program Files (x86)/MVS/Development/Includes’
INCLUDEPATH+=D:/OpencvWorkspace/opencv_3.4.2_windows/build/include \ D:/OpencvWorkspace/opencv_3.4.2_windows/build/include/opencv \ D:/OpencvWorkspace/opencv_3.4.2_windows/build/include/opencv2
LIBS+=D:/OpencvWorkspace/opencv_3.4.2_windows/build/x64/vc15/lib/opencv_world342.lib \ D:/OpencvWorkspace/opencv_3.4.2_windows/build/x64/vc15/lib/opencv_world342d.lib
然后就可以使用海康摄像头了。
我这边对海康摄像头的代码进行了二次封装,可以通过一个函数即可获取到Mat对象。
上面的getImage_Mat就可以获取到当前摄像头拍摄的画面。
代码如下:
Camera.h
#include "MvCameraControl.h"
#include
#include
#include
#include
#include
#include "string.h"
using namespace cv;
enum CONVERT_TYPE
{
OpenCV_Mat = 0, // Most of the time, we use 'Mat' format to store image data after OpenCV V2.1
OpenCV_IplImage = 1, //we may also use 'IplImage' format to store image data, usually before OpenCV V2.1
};
class Camera
{
public:
Camera(unsigned int nFormat, unsigned int nIndex);
~Camera();
bool setnIndex(unsigned int nIndex);
bool setnFormat(unsigned int nFormat);
void Stop();
Mat getImage_Mat();
protected:
void Start();
void openDevice(void);
void creatHandle(void);
void enumDevice(void);
void WaitForKeyPress(void);
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo);
int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight);
bool Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char* pData, Mat& img);
private:
unsigned int g_nPayloadSize;
int nRet;
void* handle;
unsigned char* pData;
unsigned int nFormat;
unsigned int nIndex;
MV_CC_DEVICE_INFO_LIST stDeviceList; //驱动设备结构体
MV_FRAME_OUT_INFO_EX stImageInfo;
};
Camera.cpp
#include "Camera.h"
#include
using namespace std;
///
///
/// <构造函数>
/// <图像生成的类型 0-OpenCV_Mat 1-OpenCV_IplImage>
/// <设备连接选择>
Camera::Camera(unsigned int nFormat, unsigned int nIndex)
{
// chose the format to convert
this->nFormat = nFormat;
if (nFormat >= 2)
{
printf("Input error!\n");
exit(-1);
}
this->nIndex = nIndex;
this->g_nPayloadSize = 0;
this->nRet = MV_OK;
this->handle = nullptr;
this->pData = nullptr;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
this->Start();
}
Camera::~Camera()
{
this->Stop();
return;
}
///
///
/// <选择驱动的摄像头>
/// <设备号>
/// bool-true设置成功
bool Camera::setnIndex(unsigned int nIndex)
{
this->nIndex = nIndex;
if (this->nIndex >= stDeviceList.nDeviceNum)
{
printf("Input error!\n");
exit(-1);
}
this->Start();
return true;
}
///
/// 设置图像读取的类型
///
/// 0-OpenCV_Mat 1-OpenCV_IplImage
/// bool 是否成功
bool Camera::setnFormat(unsigned int nFormat)
{
this->nFormat = nFormat;
if (this->nFormat >= 2)
{
printf("Input error!\n");
exit(-1);
}
this->Start();
return true;
}
///
///
/// <获取opencv2.0版本以上的Mat对象>
///
Mat Camera::getImage_Mat()
{
Mat img;
// get one frame from camera with timeout=1000ms
//memset(pData, 0, );
nRet = MV_CC_GetOneFrameTimeout(handle, pData, g_nPayloadSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else
{
printf("No data[0x%x]\n", nRet);
free(pData);
pData = NULL;
exit(-1);
}
// 数据去转换
bool bConvertRet = false;
if (0 == nFormat)
{
bConvertRet = Convert2Mat(&stImageInfo, pData, img);
cout<<"img size = "<<img.cols<<" "<<img.rows<<endl;
}
else
{
cout << "error: 数据格式有问题" << endl;
return Mat();
}
// print result
if (bConvertRet)
{
printf("OpenCV format convert success.\n");
}
else
{
printf("OpenCV format convert failed.\n");
free(pData);
pData = NULL;
exit(-1);
}
return img;
}
///
///
/// <打开驱动>
///
void Camera::openDevice(void)
{
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
exit(-1);
}
}
///
///
/// <创建文件头部信息>
///
void Camera::creatHandle(void)
{
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
exit(-1);
}
}
///
///
/// <获取驱动设备>
///
void Camera::enumDevice(void)
{
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
exit(-1);
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
exit(-1);
}
}
// Wait for key pres
void Camera::WaitForKeyPress(void)
{
while (!_kbhit())
{
Sleep(10);
}
_getch();
}
// print the discovered devices information to user
bool Camera::PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// print current ip and user defined name
printf("CurrentIp: %d.%d.%d.%d\n", nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
}
else
{
printf("Not support.\n");
}
return true;
}
int Camera::RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight)
{
if (NULL == pRgbData)
{
return MV_E_PARAMETER;
}
for (unsigned int j = 0; j < nHeight; j++)
{
for (unsigned int i = 0; i < nWidth; i++)
{
unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
}
}
return MV_OK;
}
// convert data stream in Mat format
bool Camera::Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char* pData, Mat& img)
{
if (pstImageInfo->enPixelType == PixelType_Gvsp_Mono8)
{
img = Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC1, pData);
}
else if (pstImageInfo->enPixelType == PixelType_Gvsp_RGB8_Packed)
{
RGB2BGR(pData, pstImageInfo->nWidth, pstImageInfo->nHeight);
img = Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC3, pData);
}
else
{
printf("unsupported pixel format\n");
return false;
}
if (NULL == img.data)
{
return false;
}
return true;
}
///
///
/// <停止>
void Camera::Stop()
{
// Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
exit(-1);
}
// Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
exit(-1);
}
// Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
exit(-1);
}
}
///
///
/// <启动,每次重新数据都要启动>
void Camera::Start()
{
// Enum device
this->enumDevice();
// Select device and create handle
this->creatHandle();
// open device
this->openDevice();
// Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle, "GevSCPSPacketSize", nPacketSize);
if (nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
}
}
// Set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
exit(-1);
}
// Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
exit(-1);
}
g_nPayloadSize = stParam.nCurValue;
// Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
exit(-1);
}
this->pData = (unsigned char*)malloc(sizeof(unsigned char) * (g_nPayloadSize));
if (this->pData == NULL)
{
printf("Allocate memory failed.\n");
exit(-1);
}
}
连接海康摄像头可以采用下面的默认配置
//连接海康摄像头
this->HK = new Camera(OpenCV_Mat, 0);