VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M

1、材料

 Opencv2.4.9已在VS2013中安装,1.1和1.2是已安装opencv计算机的状态:

1.1计算机配置Opencv时环境变量(x86对应32位编译器,X64对应64位编译器,

配置opencv时我两个都加了,这其实是多余的,也可以只选一个,这个对后面安装摄像头的X64还是X86的的lib有用):

C:\Program Files (x86)\openocd\0.6.0\bin;F:\opencv\build\x64\vc12\bin;F:\opencv\build\x86\vc12\bin

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第1张图片


1.2 opencv在VS13的

(1)工程包含(include)目录配置。(2)工程库(lib)目录

(1)中内容:

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第2张图片

(2)中内容(上面1.1中的环境变量配置了X64和X86下的bin,而下图的库目录里面选的是X64的话,则X86作废了,则在接下来的配置中摄像头lib就选X64,就像我的一样,如果你选了X86,则在接下来的配置中摄像头lib就选X86):VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第3张图片

1.3 MER-1070-14UR3的SDK包,大恒图像官网有下载安装,在安装目录下有:

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第4张图片

    上图中1有inc目录,2中也有inc和lib目录。

2、摄像头SDK在VS中的安装

 2.1在包含目录添加1.3图中1和2的inc(也就是说VC SDK和C++ SDK的inc都添加了),然后点确定-应用-确定

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第5张图片

2.2 摄像头lib目录配置,在库目录添加1.3图中1和2的lib\x??(也就是说VC SDK和C++ SDK的lib都添加了),然后点确定-应用-确定

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第6张图片

2.3 链接库的配置

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第7张图片

3 建立C++工程

3.1加入头文件(这个头文件在VC SDK的inc中)

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第8张图片

3.2 加入源.cpp代码

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第9张图片

代码转载来源:点击打开链接

#include "GxIAPI.h"  
#include "DxImageProc.h"  
#include   
#include   
#include   
#include   

using namespace std;
using namespace cv;

GX_DEV_HANDLE       m_hDevice;              ///< 设备句柄  
BYTE                *m_pBufferRaw;          ///< 原始图像数据  
BYTE                *m_pBufferRGB;          ///< RGB图像数据,用于显示和保存bmp图像  
int64_t             m_nImageHeight;         ///< 原始图像高  
int64_t             m_nImageWidth;          ///< 原始图像宽  
int64_t             m_nPayLoadSize;
int64_t             m_nPixelColorFilter;    ///< Bayer格式  
Mat test;

//图像回调处理函数  
static void GX_STDC OnFrameCallbackFun(GX_FRAME_CALLBACK_PARAM* pFrame)
{
	//PrepareForShowImg();  
	if (pFrame->status == 0)
	{
		//对图像进行某些操作  
		/*memcpy(m_pBufferRaw, pFrame->pImgBuf, pFrame->nImgSize);
		// 黑白相机需要翻转数据后显示
		for (int i = 0; i imageData = (char*)m_pImageBuffer;
		cvSaveImage("src.jpg", src);*/
		memcpy(m_pBufferRaw, pFrame->pImgBuf, pFrame->nImgSize);

		// RGB转换  
		DxRaw8toRGB24(m_pBufferRaw
			, m_pBufferRGB
			, (VxUint32)(m_nImageWidth)
			, (VxUint32)(m_nImageHeight)
			, RAW2RGB_NEIGHBOUR
			, DX_PIXEL_COLOR_FILTER(m_nPixelColorFilter)
			, false);
		//  cv_Image->imageData = (char*)m_pBufferRGB;  

		//  cvSaveImage("./test.bmp", cv_Image);  

		//test.data = m_pBufferRaw;  
		memcpy(test.data, m_pBufferRGB, m_nImageWidth*m_nImageHeight * 3);
		imwrite("./test1.bmp", test);
		namedWindow("test");
		imshow("test", test);
		waitKey(15);

	}
	return;
}

int main(int argc, char* argv[])
{
	GX_STATUS emStatus = GX_STATUS_SUCCESS;
	GX_OPEN_PARAM openParam;
	uint32_t      nDeviceNum = 0;
	openParam.accessMode = GX_ACCESS_EXCLUSIVE;
	openParam.openMode = GX_OPEN_INDEX;
	openParam.pszContent = "1";
	// 初始化库   
	emStatus = GXInitLib();
	if (emStatus != GX_STATUS_SUCCESS)
	{
		return 0;
	}
	// 枚举设备列表  
	emStatus = GXUpdateDeviceList(&nDeviceNum, 1000);
	if ((emStatus != GX_STATUS_SUCCESS) || (nDeviceNum <= 0))
	{
		return 0;
	}
	//打开设备  
	emStatus = GXOpenDevice(&openParam, &m_hDevice);


	//设置采集模式连续采集  
	emStatus = GXSetEnum(m_hDevice, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS);
	emStatus = GXSetInt(m_hDevice, GX_INT_ACQUISITION_SPEED_LEVEL, 1);
	emStatus = GXSetEnum(m_hDevice, GX_ENUM_BALANCE_WHITE_AUTO, GX_BALANCE_WHITE_AUTO_CONTINUOUS);

	bool      bColorFliter = false;
	// 获取图像大小  
	emStatus = GXGetInt(m_hDevice, GX_INT_PAYLOAD_SIZE, &m_nPayLoadSize);
	// 获取宽度  
	emStatus = GXGetInt(m_hDevice, GX_INT_WIDTH, &m_nImageWidth);
	// 获取高度  
	emStatus = GXGetInt(m_hDevice, GX_INT_HEIGHT, &m_nImageHeight);
	test.create(m_nImageHeight, m_nImageWidth, CV_8UC3);
	//判断相机是否支持bayer格式  
	bool m_bColorFilter;
	emStatus = GXIsImplemented(m_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_bColorFilter);
	if (m_bColorFilter)
	{
		emStatus = GXGetEnum(m_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_nPixelColorFilter);
	}

	m_pBufferRGB = new BYTE[(size_t)(m_nImageWidth * m_nImageHeight * 3)];
	if (m_pBufferRGB == NULL)
	{
		return false;
	}

	//为存储原始图像数据申请空间  
	m_pBufferRaw = new BYTE[(size_t)m_nPayLoadSize];
	if (m_pBufferRaw == NULL)
	{
		delete[]m_pBufferRGB;
		m_pBufferRGB = NULL;

		return false;
	}

	//注册图像处理回调函数  
	emStatus = GXRegisterCaptureCallback(m_hDevice, NULL, OnFrameCallbackFun);
	//发送开采命令  
	emStatus = GXSendCommand(m_hDevice, GX_COMMAND_ACQUISITION_START);
	//---------------------  
	//  
	//在这个区间图像会通过OnFrameCallbackFun接口返给用户  
	Sleep(100000);
	//  
	//---------------------  
	//发送停采命令  
	emStatus = GXSendCommand(m_hDevice, GX_COMMAND_ACQUISITION_STOP);
	//注销采集回调  
	emStatus = GXUnregisterCaptureCallback(m_hDevice);

	if (m_pBufferRGB != NULL)
	{
		delete[]m_pBufferRGB;
		m_pBufferRGB = NULL;
	}
	if (m_pBufferRaw != NULL)
	{
		delete[]m_pBufferRaw;
		m_pBufferRaw = NULL;
	}
	emStatus = GXCloseDevice(m_hDevice);
	emStatus = GXCloseLib();
	return 0;
}
3.3插上摄像头USB3.0到电脑USB3.0,点击生成解决方案,启动调试即可用。效果:

VS2013+Opencv2.4.9+大恒摄像头MER-1070-14U3M_第10张图片












 

你可能感兴趣的:(机器视觉)