C++ 大恒水星相机的单帧采集及回调采集 利用OpenCV转成 Mat格式

C++ 大恒水星相机的单帧采集及回调采集 利用OpenCV转成 Mat格式
最近购买一大恒水星MER-310-12UC型彩色相机,其SDK在2019年进行了更新,与之前SDK有很大不同,故现在网上资料较少,将一些简单的采集转换操作过程进行总结,后一篇将介绍与QT结合在软件界面中进行视频播放等操作。 环境是Win10,VS2017, OpenCV3.4

1. 使用手册阅读

  1. 首先我们要在其官网下载大恒相机的SDK,官网地址:http://www.daheng-imaging.com/,点击下载中心中的软件下载选择合适版本进行下载。然后安装
    C++ 大恒水星相机的单帧采集及回调采集 利用OpenCV转成 Mat格式_第1张图片
  2. 安装好了软件以后,在安装目录下找到说明书,路径一般为安装目录下:\Daheng Imaging\GalaxySDK\Doc 按照说明书搭建编译环境
    C++ 大恒水星相机的单帧采集及回调采集 利用OpenCV转成 Mat格式_第2张图片
    C++ 大恒水星相机的单帧采集及回调采集 利用OpenCV转成 Mat格式_第3张图片
    认真阅读里面的一些函数及环境搭建方法,但是其给出的示例不能调试成功,后面进行更改可以正确运行程序。

我们在工程需要把图存成Opencv的Mat形式。大恒C++ SDK采集的图像数据不能直接把图像的数据存进Mat,需要将原始数据(我这里是BayerRG8)经过格式转换之后转变成RGB24的格式,直接调用SDK的ConvertToRGB24函数。然后再向Mat写入数据。

2. 相机回调采集

// An highlighted block
#include 
#include 
//using namespace cv;
using namespace std;
#include 

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);
		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();

	try
	{
		do
		{
			//枚举设备
			gxdeviceinfo_vector vectorDeviceInfo;
			IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
			if (0 == vectorDeviceInfo.size())
			{
				cout << "无可用设备!" << endl;
				break;
			}
			cout << vectorDeviceInfo[0].GetVendorName() << endl;
			cout << vectorDeviceInfo[0].GetSN() << endl;
			//打开第一台设备以及设备下面第一个流
			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();
			
			//设置曝光时间(示例中写死us,只是示例,并不代表真正可工作参数)
			//ObjFeatureControlPtr->GetFloatFeature("ExposureTime")->SetValue(50);

			////注册远端设备事件:曝光结束事件【目前只有千兆网系列相机支持曝光结束事件】
			////选择事件源
			//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();
		} while (0);
	}

	catch (CGalaxyException&e)
	{
		cout << "错误码: " << e.GetErrorCode() << endl;
		cout << "错误描述信息: " << e.what() << endl;
	}
	catch (std::exception&e)
	{
		cout << "错误描述信息: " << e.what() << endl;
	}

	//反初始化库
	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;

}

运行会报CGalaxyEeception 找不到标识符的错误,我们可以在gxsmartptr.h的文件中,在上面添一句#include “GalaxyException.h” 就好了!

使用的是回调采集方式,其运行结果:

图中是以视频帧的方式显示。

3.单帧采集

// An highlighted block
#include 
#include 
//using namespace cv;
using namespace std;
#include 

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;
	}
};

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

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

	//初始化 使用其他接口之前,必须执行初始化
	IGXFactory::GetInstance().Init();

	try
	{
		do
		{
			//枚举设备
			gxdeviceinfo_vector vectorDeviceInfo;
			IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
			if (0 == vectorDeviceInfo.size())
			{
				cout << "无可用设备!" << endl;
				break;
			}
			cout << vectorDeviceInfo[0].GetVendorName() << endl;
			cout << vectorDeviceInfo[0].GetSN() << endl;
			//打开第一台设备以及设备下面第一个流
			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();

			//设置曝光时间(示例中写死us,只是示例,并不代表真正可工作参数)
			//ObjFeatureControlPtr->GetFloatFeature("ExposureTime")->SetValue(50);

			////注册远端设备事件:曝光结束事件【目前只有千兆网系列相机支持曝光结束事件】
			////选择事件源
			//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();

			//采单帧

			CImageDataPointer objImageDataPtr;

			objImageDataPtr = ObjStreamPtr->GetImage(500);//超时时间使用500ms,用户可以自行设定

			if (objImageDataPtr->GetStatus() == GX_FRAME_STATUS_SUCCESS)				//采图成功而且是完整帧,可以进行图像处理...

			{
				cout << "收到一帧图像!" << endl;
				cout << "ImageInfo: " << objImageDataPtr->GetStatus() << endl;
				cout << "ImageInfo: " << objImageDataPtr->GetWidth() << endl;
				cout << "ImageInfo: " << objImageDataPtr->GetHeight() << endl;
				cout << "ImageInfo: " << objImageDataPtr->GetPayloadSize() << endl;

				img.create(objImageDataPtr->GetHeight(), objImageDataPtr->GetWidth(), CV_8UC3);
				void *pRGB24Buffer = NULL;
				//假设原始数据是BayerRG8图像
				pRGB24Buffer = objImageDataPtr->ConvertToRGB24(GX_BIT_0_7, GX_RAW2RGB_NEIGHBOUR, true);
				memcpy(img.data, pRGB24Buffer, (objImageDataPtr->GetHeight()) * (objImageDataPtr->GetWidth()) * 3);
				cv::flip(img, img, 0);
				cv::imshow("sss", img);
				cv::waitKey(1);
				cv::imwrite("01.bmp", img);

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

			}

			//此时开采成功,控制台打印信息,直到输入任意键继续
			//getchar();

			//发送停采命令
			ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
			ObjStreamPtr->StopGrab();

			//注销采集回调
			ObjStreamPtr->UnregisterCaptureCallback();

			////注销远端设备事件
			//ObjFeatureControlPtr->UnregisterFeatureCallback(hFeatureEvent);

			////注销设备掉线事件
			//ObjDevicePtr->UnregisterDeviceOfflineCallback(hDeviceOffline);

			//释放资源
			ObjStreamPtr->Close();
			ObjDevicePtr->Close();
		} while (0);
	}

	catch (CGalaxyException&e)
	{
		cout << "错误码: " << e.GetErrorCode() << endl;
		cout << "错误描述信息: " << e.what() << endl;
	}
	catch (std::exception&e)
	{
		cout << "错误描述信息: " << e.what() << endl;
	}

	//反初始化库 关闭设备之后,不能再调用其他任何库接口
	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;

}

并将其采集图像保存在01.bmp中,结果:

你可能感兴趣的:(大恒工业相机,水星,MER,VS)