标签:blog ar io os sp on 数据 2014 问题
最近在研究OpenCv对摄像头的调用。现将代码贴出,供大家批评指正。
1、申明
#include"./opencv2/opencv.hpp" #ifdef _DEBUG #pragma comment(lib,"opencv_core249d.lib") #pragma comment(lib,"opencv_highgui249d.lib") #else #pragma comment(lib,"opencv_core249.lib") #pragma comment(lib,"opencv_highgui249.lib") #endif using namespace cv; class COpencvCameraCtrl : public CCameraBase { public: VideoCapture vc; public: COpencvCameraCtrl(void); ~COpencvCameraCtrl(void); public: BOOL OpenCamera( HWND win, CString strVid, CString strPid ); //打开摄像头 BOOL PhotoPic( CString strSaveFile ); void CloseCamera(); };
//打开摄像头 BOOL COpencvCameraCtrl::OpenCamera(HWND win, CString strVid, CString strPid) { InitCameraWin( win ); vid = strVid; pid = strPid; nIndex = GetCameraIndexInOs( vid, pid ); if( nIndex < 0 ) { return FALSE; } if( !vc.open( nIndex ) ) { return FALSE; } vc.set( CV_CAP_PROP_FRAME_WIDTH, nWidth ); vc.set( CV_CAP_PROP_FRAME_HEIGHT, nHight ); if( !vc.isOpened() ) { bOpen = false; return FALSE; } bOpen = true; return TRUE; }
//关闭摄像头 void COpencvCameraCtrl::CloseCamera() { if( vc.isOpened() ) { vc.release(); } }
//拍摄摄像头 BOOL COpencvCameraCtrl::PhotoPic( CString strSaveFile ) { Mat frame; vc >> frame; vc >> frame; if( !frame.data ) { return FALSE; } //设置图像质量 vector<int> compression_params; compression_params.push_back( IMWRITE_JPEG_QUALITY ); compression_params.push_back( 75 ); imwrite( strSaveFile.GetBuffer(0), frame, compression_params ); strSaveFile.ReleaseBuffer(); return TRUE; }
Mat frame; vc >> frame; vc >> frame; if( !frame.data ) { return FALSE; }
A、如果打开摄像头立即调用拍照保存,第一次会失败,获取不到数据
B、每次拍摄保存的图片是上一次的图片
标签:blog ar io os sp on 数据 2014 问题
原文地址:http://blog.csdn.net/morbi/article/details/41978149