1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; int main() { Mat img; VideoCapture video(1); if (!video.isOpened()) { cout << "摄像头打开失败" << endl; return -1; } video >> img; if (img.empty()) { cout << "获取图像失败" << endl; return -1; } bool isColor = (img.type() == CV_8UC3);
VideoWriter w1; int encode_type = VideoWriter::fourcc('M', 'J', 'P', 'G'); double fps = 25.0; string filename = "test_save.avi"; w1.open(filename, encode_type, fps, img.size(), isColor); if (!w1.isOpened()) { cout << "视频打开失败" << endl; } while (1) { Mat frame; video >> frame; if (frame.empty()) { break; } w1.write(frame); namedWindow("video_test", WINDOW_NORMAL); imshow("video_test", frame); char c = waitKey(50); if (c == 27) { break; } } video.release(); w1.release(); return 0; }
|