介绍
-
获取PC连接的摄像头下的视频流,个性化设置拍摄时的参数和按键等
-
用于拍摄获取特定场景下的视频
-
windows代码如下:
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#ifdef _WIN32
#include <Windows.h>
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#endif
/****************
// timeStamp convert to stardard time format
*******/
void getStdTimeFormat(time_t nTimeStamp, char* pStdTimeFormat)
{
char pTmpString[128] = { '\0' };
#ifdef _WIN32
struct tm pTmStruct;
localtime_s(&pTmStruct, &nTimeStamp);
sprintf_s(pTmpString, sizeof(pTmpString), "%04d_%02d_%02d-%02d_%02d_%02d", pTmStruct.tm_year + 1900, pTmStruct.tm_mon + 1, pTmStruct.tm_mday, \
pTmStruct.tm_hour, pTmStruct.tm_min, pTmStruct.tm_sec);
memcpy(pStdTimeFormat, pTmpString, strlen(pTmpString));
#else
tm *pTmStruct = localtime(&nTimeStamp);
sprintf(pTmpString, "%04d-%02d-%02d %02d:%02d:%02d", pTmStruct->tm_year + 1900, pTmStruct->tm_mon + 1, pTmStruct->tm_mday, \
pTmStruct->tm_hour, pTmStruct->tm_min, pTmStruct->tm_sec);
//strcpy(pStdTimeFormat, pTmpString);
memcpy(pStdTimeFormat, pTmpString, strlen(pTmpString));
#endif // _WIN32
//timeStream = std::string(pTmpString);
return;
}
using namespace std;
int main(int argc, char** argv){
int frame_width = 640;
int frame_height = 480;
double fps = 25.0;// framerate of the created video stream
if(argc == 2){
printf("Usage: ./Demo.exe fps\n");
int fps_value = atoi(argv[1]);
fps = 1.0f*fps_value;
}
if(argc == 3){
printf("Usage: Demo.exe FrameWidth FrameHeight\n");
frame_width = atoi(argv[1]);
frame_height = atoi(argv[2]);
}
if(argc == 4){
printf("Usage: Demo.exe fps FrameWidth FrameHeight\n");
int fps_value = atoi(argv[1]);
fps = 1.0f*fps_value;
frame_width = atoi(argv[2]);
frame_height = atoi(argv[3]);
}
if(argc >4){
printf("Usage: Demo.exe fps FrameWidth FrameHeight\n");
return -1;
}
cv::Mat src;
// use default camera as video source
cv::VideoCapture cap(0);
// check if we succeeded
if (!cap.isOpened()) {
std::cerr << "ERROR! Unable to open camera\n";
return -1;
}
cap.set(cv::CAP_PROP_FRAME_WIDTH, frame_width);
cap.set(cv::CAP_PROP_FRAME_HEIGHT,frame_height);
cout << "Frame width: " << cap.get(cv::CAP_PROP_FRAME_WIDTH) << endl;
cout << "Frame height: " << cap.get(cv::CAP_PROP_FRAME_HEIGHT) << endl;
// get one frame from camera to know frame size and type
cap >> src;
// check if we succeeded
if (src.empty()) {
cerr << "ERROR! blank frame grabbed\n";
return -1;
}
bool isColor = (src.type() == CV_8UC3);
//--- INITIALIZE VIDEOWRITER
cv::VideoWriter writer;
int codec = cv::VideoWriter::fourcc('M', 'J', 'P', 'G'); // select desired codec (must be available at runtime)
time_t nTimeStamp = time(NULL);
char pStdTimeFormat[64]={0x0};
getStdTimeFormat(nTimeStamp, pStdTimeFormat);
#ifdef _WIN32
const char* cmdpath = _getcwd(NULL, 0);
char saveDir[256] = {0x0};
sprintf_s(saveDir, 255, "%s\\Frames_%s\\", cmdpath, pStdTimeFormat);
if(0 != _access(saveDir,0))
{
_mkdir(saveDir);//create the folder
//printf("dest folder had created. \n");
}
#else
const char* cmdpath = get_current_dir_name();
char saveDir[256] = {0x0};
sprintf(saveDir, "%s/Frames_%s/", cmdpath, pStdTimeFormat);
if ( 0 != access(saveDir.c_str(),0))//if this is not a directory
{
mkdir(saveDir.c_str(),0755);//create the folder
//printf("dest folder had created. \n");
}
#endif
char tmpfilename[256] = {0x0};
sprintf(tmpfilename,"./video_%s.avi", pStdTimeFormat); // name of the output video file
std::string filename(tmpfilename);
writer.open(filename, codec, fps, cv::Size(cap.get(cv::CAP_PROP_FRAME_WIDTH), cap.get(cv::CAP_PROP_FRAME_HEIGHT)), isColor);
// check if we succeeded
if (!writer.isOpened()) {
cerr << "Could not open the output video file for write\n";
return -1;
}
size_t nFrames = 0;
size_t index = 0;
bool enableQuit = false;
cv::namedWindow("capture frame", cv::WINDOW_NORMAL | cv::WINDOW_FULLSCREEN);
for (;;)
{
// check if we succeeded
if (!cap.read(src)) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
nFrames++;
// encode the frame into the video file stream
writer.write(src);
// show live and wait for a key with timeout long enough to show images
cv::imshow("capture frame", src);
int key = cv::waitKey(5);
switch (key) {
case 32: // Spacebar to save a frame mat
{
index++;
char saveString[256];
sprintf(saveString,"%s%05zd.png",saveDir, index);
cv::imwrite(saveString, src);
break;
}
case 27: //ESC to quit
{
enableQuit = true;
break;
}
default:
continue;
}
if(enableQuit){
break;
}
}
cv::destroyAllWindows();
// the video file will be closed and released automatically in VideoWriter destructor
return 0;
}