赞
踩
main() { int iFrameIndex = 0; cv::VideoCapture cap; cap.open("/root/zhangsong/yolo-tensorrt-master/3.mp4"); cv::Mat mat_image; std::string videoname = "/root/zhangsong/yolo-tensorrt-master/build/tracker_result.avi"; int videoFps = cap.get(CV_CAP_PROP_FPS); int myFourCC = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); cv::VideoWriter g_writer = cv::VideoWriter(videoname, myFourCC, videoFps, cv::Size(cap.get(cv::CAP_PROP_FRAME_WIDTH), cap.get(cv::CAP_PROP_FRAME_HEIGHT))); while(true) { cap>>mat_image; cv::Mat mat_temp = mat_image.clone(); iFrameIndex++; //cv::imwrite("000269_.jpg",mat_temp); g_writer << mat_temp; cv::namedWindow("image",0); cv::imshow("image", mat_temp); cv::waitKey(1); } g_writer.release(); }
import os import cv2 if __name__ == "__main__": os.environ['KMP_DUPLICATE_LIB_OK'] = 'True' cap = cv2.VideoCapture('F:/gongye/data/video/IMG_3451.mp4') fps = cap.get(cv2.CAP_PROP_FPS) g_ntotalFrameNumber = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) print(size) # 定义编码格式mpge-4 fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2') # 定义视频文件输入对象 savevideopath = 'F:/gongye/data/video/result01.avi' outVideo = cv2.VideoWriter(savevideopath, fourcc, fps, size) # 获取视频流打开状态 if cap.isOpened(): rval, frame = cap.read() print('ture') else: rval = False print('False') tot = 1 #c = 1 while (rval): rval, frame = cap.read() # 返回两个值,第一个为bool类型,如果读到帧返回True,如果没读到帧返回False,第二个值为帧图像 #cv2.imshow('test', frame) # 每间隔20帧保存一张图像帧 # if tot % 20 ==0 : # cv2.imwrite('cut/'+'cut_'+str(c)+'.jpg',frame) # c+=1 tot += 1 print('tot=', tot) outVideo.write(frame) #cv2.waitKey(1) cap.release() outVideo.release() #cv2.destroyAllWindows()
#include <QCoreApplication> #include <iostream> #include <time.h> #include<sys/io.h> #include<iostream> #include<vector> #include <string> #include <list> #include <dirent.h> #include <sys/stat.h> #include<opencv2/opencv.hpp> using namespace std; int g_ntotalFrameNumber,g_ncurrentframe; double g_currentpercent; #define FRAME_PER_NUME 20 #define FILENAME_SUFFIX_NUM 4 #define INPUT_VIDEO_PATH "/media/t/disk1/HOME/workplace/qt/videotoimage/video/1" #define OUTPUT_IMAGE_PATH "/media/t/disk1/HOME/workplace/qt/videotoimage/output_image/" //vector<string> getFiles(const string &folder,const bool all /* = true */) //{ // vector<string> files; // list<string> subfolders; // subfolders.push_back(folder); // while (!subfolders.empty()) { // string current_folder(subfolders.back()); // if (*(current_folder.end() - 1) != '/') { //确保地址是 “ ..../*” 结尾的 // current_folder.append("/*"); // } // else { // current_folder.append("*"); // } // subfolders.pop_back(); // struct _finddata_t file_info; // long file_handler = _findfirst(current_folder.c_str(), &file_info); // while (file_handler != -1) // { // if (all && // (!strcmp(file_info.name, ".") || !strcmp(file_info.name, ".."))) { // if (_findnext(file_handler, &file_info) != 0) break; // continue; // } //#if 0 // 斜杆/ 和 反斜杠\ 的区别基本上就是这些了,下面再讨论一下相对路径和绝对路径。 // ./SRC/ 这样写表示,当前目录中的SRC文件夹; // ../SRC/ 这样写表示,当前目录的上一层目录中SRC文件夹; // /SRC/ 这样写表示,项目根目录(可以只磁盘根目录,也可以指项目根目录,具体根据实际情况而定) //#endif // if (file_info.attrib & _A_SUBDIR) // { // // it's a sub folder // if (all) // { // // will search sub folder // string folder(current_folder); // folder.pop_back(); // 就是减去最后的* string 的pop_back Delete last character Erases the last character of the string, effectively reducing its length by one. // folder.append(file_info.name); // subfolders.push_back(folder.c_str()); // } // } // else // { // // it's a file // string file_path; // // current_folder.pop_back(); // file_path.assign(current_folder.c_str()).pop_back(); // file_path.append(file_info.name); // files.push_back(file_path); // } // if (_findnext(file_handler, &file_info) != 0) break; // } // while // _findclose(file_handler); // } // return files; //} #if 0 int main(int argc, char *argv[]) { time_t t; struct tm * lt; bool f_stop = false; cv::Mat Input; std::string input_video_path = INPUT_VIDEO_PATH; cv::VideoCapture capture(input_video_path); if (!capture.isOpened()) std::cout << "fail to open!" << std::endl; g_ntotalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT); std::cout << "整个视频共" << g_ntotalFrameNumber << "帧" << std::endl; //获得帧的宽高 int w = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH)); int h = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT)); cv::Size S(w, h); // 视频写入对象 cv::VideoWriter writer("/home/t/result.avi", CV_FOURCC('m', 'p', '4', ' v'),25, cv::Size(1280, 720)); while (!f_stop ) { time (&t); lt = localtime (&t); for (int i = 0;i < 25;i++) { capture >> Input; if(Input.empty()) { break; } } if(Input.empty()) { break; } writer<<Input; g_currentpercent = int(g_ncurrentframe * 100 / g_ntotalFrameNumber); std::stringstream pathROIss; std::string pathROIs; pathROIss<<"/media/t/disk1/DukTo/Videos/20180518_HIKVISION_HDR_1/20190926"<< lt->tm_hour << lt->tm_min << "_" <<g_ncurrentframe<<".jpg"; pathROIss>>pathROIs; imwrite(pathROIs, Input); g_ncurrentframe += 25; } //释放对象 capture.release(); return 0; } #endif #if 1 //int main(int argc, char *argv[]) //{ // //目标文件夹路径 // std::string inPath = "/media/t/disk1/DukTo/Videos/*.mp4";//遍历文件夹下的所有.jpg文件 // //用于查找的句柄 // long handle; // struct _finddata_t fileinfo; // //第一次查找 // handle = _findfirst(inPath.c_str(), &fileinfo); // if (handle == -1) // return -1; // do // { // //找到的文件的文件名 // printf("%s\n", fileinfo.name); // } while (!_findnext(handle, &fileinfo)); // _findclose(handle); // system("pause"); // return 0; //} void videotoimage(std::string input_video_path, std::string output_image_path, std::string filename) { time_t t; struct tm * lt; bool f_stop = false; cv::Mat Input; cv::VideoCapture capture(input_video_path); if (!capture.isOpened()) std::cout << "fail to open!" << std::endl; g_ntotalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT); std::cout << "整个视频共" << g_ntotalFrameNumber << "帧" << std::endl; //获得帧的宽高 int w = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH)); int h = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT)); cv::Size S(w, h); // 视频写入对象 cv::VideoWriter writer("/home/t/result.avi", CV_FOURCC('m', 'p', '4', ' v'),25, cv::Size(1280, 720)); while (!f_stop ) { time (&t); lt = localtime (&t); for (int i = 0;i < FRAME_PER_NUME;i++) { capture >> Input; if(Input.empty()) { break; } } if(Input.empty()) { break; } writer<<Input; g_currentpercent = int(g_ncurrentframe * 100 / g_ntotalFrameNumber); std::stringstream pathROIss; std::string pathROIs; pathROIss<<output_image_path<<"/"<<filename<<"_"<< lt->tm_hour << lt->tm_min << "_" <<g_ncurrentframe<<".jpg"; pathROIss>>pathROIs; imwrite(pathROIs, Input); g_ncurrentframe += FRAME_PER_NUME; } //释放对象 capture.release(); //return 0; } int main() { string src_video_path = INPUT_VIDEO_PATH; DIR* dir = opendir(src_video_path.data());//打开指定目录 dirent* p = NULL;//定义遍历指针 while((p = readdir(dir)) != NULL)//开始逐个遍历 { //这里需要注意,linux平台下一个目录中有"."和".."隐藏文件,需要过滤掉 if(p->d_name[0] != '.')//d_name是一个char数组,存放当前遍历到的文件名 { g_ncurrentframe = 0; string dirpath = OUTPUT_IMAGE_PATH; // std::stringstream pathROIss; // std::string pathROIs; // pathROIss<<"/media/t/disk1/DukTo/Videos"<< lt->tm_hour << lt->tm_min << "_" <<g_ncurrentframe<<".jpg"; // pathROIss>>pathROIs; string str = string(p->d_name); cout<<str<<endl; string videopath = src_video_path + "/"; videopath += str; //str.erase(remove(str.begin() , str.end() , '.') , str.end() ); str.erase( str.size()-FILENAME_SUFFIX_NUM, FILENAME_SUFFIX_NUM); dirpath += str; mkdir(dirpath.data(),S_IRWXU); videotoimage(videopath,dirpath,str); // string name = "/home/xxx/test/" + str; // cout<<name<<endl; } } closedir(dir);//关闭指定目录 } #endif
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。