【OpenCV】MindVision相机Get_Image

时间:2021-01-31 21:18:08


#include "CameraApi.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  

using namespace std;
using namespace cv;

#pragma comment(lib, "MVCAMSDK.lib")


//---------------------------------//
UINT            m_threadID;		//图像抓取线程的ID
HANDLE          m_hDispThread;	//图像抓取线程的句柄
BOOL            m_bExit = FALSE;//用来通知图像抓取线程结束
CameraHandle    m_hCamera;		//相机句柄,多个相机同时使用时,可以用数组代替	
BYTE*           m_pFrameBuffer; //用于将原始图像数据转换为RGB的缓冲区
tSdkFrameHead   m_sFrInfo;		//用于保存当前图像帧的帧头信息

int	            m_iDispFrameNum;	//用于记录当前已经显示的图像帧的数量
float           m_fDispFps;			//显示帧率
float           m_fCapFps;			//捕获帧率
tSdkFrameStatistic  m_sFrameCount;
tSdkFrameStatistic  m_sFrameLast;
int					m_iTimeLast;
char		    g_CameraName[64];


//--------------------------//
tSdkFrameHead 	sFrameInfo;
BYTE*			pbyBuffer;
IplImage *iplImage = NULL;



	tSdkCameraDevInfo sCameraList[10];
	INT iCameraNums;
	CameraSdkStatus status;
	tSdkCameraCapbility sCameraInfo;

	iCameraNums = 10;
	status = CameraEnumerateDevice(sCameraList,&iCameraNums);
	if (CameraEnumerateDevice(sCameraList,&iCameraNums) != CAMERA_STATUS_SUCCESS || iCameraNums == 0)
	{
		return 101;
	}

	status = CameraInit(&sCameraList[0],-1,-1,&m_hCamera);
	if( status != CAMERA_STATUS_SUCCESS )
	{
		char msg[128];
		sprintf_s(msg,"Failed to init the camera! Error code is %d",status);
		printf(msg);
		return 102;
	}

	CameraGetCapability(m_hCamera,&sCameraInfo);//"获得该相机的特性描述"

	m_pFrameBuffer = (BYTE *)CameraAlignMalloc(sCameraInfo.sResolutionRange.iWidthMax*sCameraInfo.sResolutionRange.iWidthMax*3,16);	

	if (sCameraInfo.sIspCapacity.bMonoSensor)
	{
		CameraSetIspOutFormat(m_hCamera,CAMERA_MEDIA_TYPE_MONO8);
	} 
	strcpy_s(g_CameraName,sCameraList[0].acFriendlyName);
	//CameraCreateSettingPage(m_hCamera,NULL, g_CameraName,NULL,NULL,0);//"通知SDK内部建该相机的属性页面";
	CameraPlay(m_hCamera);
	//CameraShowSettingPage(m_hCamera, FALSE);//TRUE显示相机配置界面。FALSE则隐藏。


	CameraSdkStatus status;
	status = CameraGetImageBuffer(m_hCamera, &sFrameInfo, &pbyBuffer,1000);
	if( status == CAMERA_STATUS_SUCCESS)
	{	
		status = CameraImageProcess(m_hCamera, pbyBuffer, m_pFrameBuffer,&sFrameInfo);//连续模式

		//分辨率改变了,则刷新背景
		if (m_sFrInfo.iWidth != sFrameInfo.iWidth || m_sFrInfo.iHeight != sFrameInfo.iHeight)
		{
			m_sFrInfo.iWidth = sFrameInfo.iWidth;
			m_sFrInfo.iHeight = sFrameInfo.iHeight;
		}

		if(status == CAMERA_STATUS_SUCCESS)
		{
			//调用SDK封装好的显示接口来显示图像,您也可以将m_pFrameBuffer中的RGB数据通过其他方式显示,比如directX,OpengGL,等方式。
			CameraImageOverlay(m_hCamera, m_pFrameBuffer, &sFrameInfo);
			if (iplImage)
			{
				cvReleaseImageHeader(&iplImage);
			}
			iplImage = cvCreateImageHeader(cvSize(sFrameInfo.iWidth,sFrameInfo.iHeight),IPL_DEPTH_8U,sFrameInfo.uiMediaType == CAMERA_MEDIA_TYPE_MONO8?1:3);
			cvSetData(iplImage,m_pFrameBuffer,sFrameInfo.iWidth*(sFrameInfo.uiMediaType == CAMERA_MEDIA_TYPE_MONO8?1:3));
			//cvShowImage(g_CameraName, iplImage);
			m_iDispFrameNum++;
		}    

		//在成功调用CameraGetImageBuffer后,必须调用CameraReleaseImageBuffer来释放获得的buffer。
		//否则再次调用CameraGetImageBuffer时,程序将被挂起,知道其他线程中调用CameraReleaseImageBuffer来释放了buffer
		CameraReleaseImageBuffer(m_hCamera, pbyBuffer);

		memcpy(&m_sFrInfo,&sFrameInfo,sizeof(tSdkFrameHead));
	}



if (iplImage)
	{
		cvReleaseImageHeader(&iplImage);
	}
	CameraUnInit(m_hCamera);
	CameraAlignFree(m_pFrameBuffer);
	//destroyWindow(g_CameraName);