Recently in the study of OpenCV's call to the camera. Now put the code out for everyone to criticize.
1. Affirm
#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 ") # Endifusing namespace Cv;class Copencvcameractrl:p ublic ccamerabase{public:videocapture Vc;public:copencvcameractrl ( void), ~copencvcameractrl (void);p Ublic:bool Opencamera (HWND win, CString Strvid, CString strpid);//Open Camera BOOL photopic ( CString strsavefile); void Closecamera ();};
2. Turn on the camera
Turn on webcam 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;}
3. Turn off the camera
Turn off camera void Copencvcameractrl::closecamera () {if (vc.isopened ()) {Vc.release ()}}
4. Take photos and save to local
camera bool Copencvcameractrl::P hotopic (CString strsavefile) {Mat frame;vc >> frame;vc >> frame;if (! Frame.data) {return FALSE;} Set image quality Vector<int> compression_params;compression_params.push_back (imwrite_jpeg_quality); compression_ Params.push_back (Imwrite); Strsavefile.getbuffer (0), frame, compression_params); Strsavefile.releasebuffer (); return TRUE;}
Note: Every time you get data from the camera to the mat, you need to do it twice.
Mat FRAME;VC >> frame;vc >> frame;if (!frame.data) {return FALSE;}
If executed only once, there are two problems:
A, if you turn on the camera immediately call photo save, the first time will fail, get no data
B, each shot saved picture is the last picture
OpenCV Call Camera Photo code