Vs2017+opencv调用大恒图像相机,实现读存操作

一、首先搭建vs环境,这里主要介绍怎么配置大恒图像的水星二代的sdk

a) 新建空项目,选择项目->属性->VC++目录->包含目录,添加下列两个inc文件(来源于官网下载的驱动软件的sample里)

Vs2017+opencv调用大恒图像相机,实现读存操作_第1张图片

b) 选择项目->属性->VC++目录->库目录,添加下列两个lib文件,注意要写\x64,因为lib文件下有两个版本

Vs2017+opencv调用大恒图像相机,实现读存操作_第2张图片

c) 选择项目->链接器->输入->附加依赖项,添加
DxImageProc.lib
GxIAPI.lib
GxIAPICPPEx.lib

Vs2017+opencv调用大恒图像相机,实现读存操作_第3张图片
二、源文件新建一个test.cpp,复制下列代码,注意代码下面有注意事项

// An highlighted block
#include "stdlib.h"
#include <iostream>
#include <opencv2/opencv.hpp>
//using namespace cv;
using namespace std;
#include <GalaxyIncludes.h>
int j = 0;

cv::Mat img;
//用户继承掉线事件处理类
class CSampleDeviceOfflineEventHandler : public IDeviceOfflineEventHandler
{
public:
	void DoOnDeviceOfflineEvent(void* pUserParam)
	{
		cout << "收到设备掉线事件!" << endl;
	}
};

//用户继承属性更新事件处理类

class CSampleFeatureEventHandler : public IFeatureEventHandler
{
public:
	void DoOnFeatureEvent(const GxIAPICPP::gxstring&strFeatureName, void* pUserParam)
	{
		cout << "收到曝光结束事件!" << endl;
	}
};

//用户继承采集事件处理类
class CSampleCaptureEventHandler : public ICaptureEventHandler
{
public:
	void DoOnImageCaptured(CImageDataPointer&objImageDataPointer, void* pUserParam)
	{
		cout << "收到一帧图像!" << endl;
		cout << "ImageInfo: " << objImageDataPointer->GetStatus() << endl;
		cout << "ImageInfo: " << objImageDataPointer->GetWidth() << endl;
		cout << "ImageInfo: " << objImageDataPointer->GetHeight() << endl;
		cout << "ImageInfo: " << objImageDataPointer->GetPayloadSize() << endl;

		img.create(objImageDataPointer->GetHeight(), objImageDataPointer->GetWidth(), CV_8UC3);
		void *pRGB24Buffer = NULL;
		//假设原始数据是BayerRG8图像
		pRGB24Buffer = objImageDataPointer->ConvertToRGB24(GX_BIT_0_7, GX_RAW2RGB_NEIGHBOUR, true);
		memcpy(img.data, pRGB24Buffer, (objImageDataPointer->GetHeight()) * (objImageDataPointer->GetWidth()) * 3);
		cv::flip(img, img, 0);
		j++;
		char st[100];
		sprintf_s(st, 100, "%s%d%s", "D:/桌面/cvt/", j, ".bmp");
		cv::imwrite(st, img);
		cv::imshow("sss", img);
		cv::waitKey(1);

		cout << "帧数:" << objImageDataPointer->GetFrameID() << endl;
	}
};

int main(int argc, char* argv[])
{

	//声明事件回调对象指针
	IDeviceOfflineEventHandler* pDeviceOfflineEventHandler = NULL;///<掉线事件回调对象
	IFeatureEventHandler* pFeatureEventHandler = NULL;///<远端设备事件回调对象
	ICaptureEventHandler* pCaptureEventHandler = NULL;///<采集回调对象

	//初始化
	IGXFactory::GetInstance().Init();

	for (int i = 0; i < 1; i++)
	{
		gxdeviceinfo_vector vectorDeviceInfo;
		IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
		if (0 == vectorDeviceInfo.size())
		{
			cout << "无可用设备!" << endl;
			break;
		}
		//打开第一台设备以及设备下面第一个流
		CGXDevicePointer ObjDevicePtr = IGXFactory::GetInstance().OpenDeviceBySN(
			vectorDeviceInfo[0].GetSN(),
			GX_ACCESS_EXCLUSIVE);
		CGXStreamPointer ObjStreamPtr = ObjDevicePtr->OpenStream(0);
		//注册设备掉线事件【目前只有千兆网系列相机支持此事件通知】
		GX_DEVICE_OFFLINE_CALLBACK_HANDLE hDeviceOffline = NULL;
		pDeviceOfflineEventHandler = new CSampleDeviceOfflineEventHandler();
		hDeviceOffline = ObjDevicePtr->RegisterDeviceOfflineCallback(pDeviceOfflineEventHandler, NULL);
		//获取远端设备属性控制器
		CGXFeatureControlPointer ObjFeatureControlPtr = ObjDevicePtr->GetRemoteFeatureControl();
		//注册远端设备事件:曝光结束事件【目前只有千兆网系列相机支持曝光结束事件】
		//选择事件源
		ObjFeatureControlPtr->GetEnumFeature("EventSelector")->SetValue("ExposureEnd");
		//使能事件
		ObjFeatureControlPtr->GetEnumFeature("EventNotification")->SetValue("On");
		GX_FEATURE_CALLBACK_HANDLE hFeatureEvent = NULL;
		pFeatureEventHandler = new CSampleFeatureEventHandler();
		hFeatureEvent = ObjFeatureControlPtr->RegisterFeatureCallback(
			"EventExposureEnd",
			pFeatureEventHandler,
			NULL);
		//注册回调采集
		pCaptureEventHandler = new CSampleCaptureEventHandler();
		ObjStreamPtr->RegisterCaptureCallback(pCaptureEventHandler, NULL);
		//发送开采命令
		ObjStreamPtr->StartGrab();
		ObjFeatureControlPtr->GetCommandFeature("AcquisitionStart")->Execute();
		//此时开采成功,控制台打印信息,直到输入任意键继续
		getchar();
		//发送停采命令
		ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
		ObjStreamPtr->StopGrab();
		//注销采集回调
		ObjStreamPtr->UnregisterCaptureCallback();
		//注销远端设备事件
		ObjFeatureControlPtr->UnregisterFeatureCallback(hFeatureEvent);
		//注销设备掉线事件
		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;
	}
	return 0;

}


注意:如果报标识符错误,在对应Gxsmartptr.h头文件添加
#include “GalaxyException.h”

你可能感兴趣的:(c++,opencv)