Hi everyone, I have a issue with my video capture code where the output of the video is super fast when outputted, I searched some documents and they all suggest that the problem is due to the fps of the camera not in sync to the output video fps, therefore making the output video faster or slower than normal time (capture fps > write fps: slower, capture fps < write fps: faster).
Back to my case, I did manually set the fps of the two webcams to 10 fps and matched 10 fps when using VideoWriter() as well, I also set the waitKey() to 100ms per frame so that it matches with 10 fps, but I'm still getting super fast video output, is it due to the delay processing time of my video_multi_cam_light_detection() function as it does take sometime to process, such that individual frame have longer delay which decrease the overall preset 10 fps? (I'm suspecting this because I tested another simple VideoCapture program with out my detection function and it's only faster for like 3 seconds).
Again, Thank you all for the help, if my logic or code are wrong in any ways please feel free to indicate it as I'm really new to OpenCV.
Here is my code:
int video_light_detection() {
string raw_video_path = "./RAW_VIDEO";
string processed_video_path = "./PROCESSED_VIDEO";
string raw_image_path = "./SAMPLE_CAPTURED";
string processed_image_path = "./SAMPLE_CAPTURED";
VideoCapture cap(0);
cap.set(CV_CAP_PROP_FPS, 10);
VideoCapture cap1(1);
cap1.set(CV_CAP_PROP_FPS, 10);
time_t current_time = time(0);
tm *time_p = localtime(¤t_time);
int year = 1900 + time_p->tm_year;
int month = 1 + time_p->tm_mon;
int day = time_p->tm_mday;
int hour = time_p->tm_hour;
int min = time_p->tm_min;
ostringstream oss;
oss << "_" << hour << "_" << min << "_" << month << "_" << day << "_" << year;
string file_suffix = oss.str();
raw_video_path += file_suffix + string(".mkv");
processed_video_path += file_suffix + string(".mkv");
raw_image_path += file_suffix + string(".jpg");
processed_image_path += file_suffix + string(".jpg");
if (!cap.isOpened() || !cap1.isOpened()) {
cerr << "Camera Open Failure" << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 960);
cap1.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 960);
VideoWriter video_raw;
VideoWriter video_processed;
namedWindow("Capture Window", WINDOW_NORMAL);
resizeWindow("Capture Window", 640, 1280);
namedWindow("Detection Window", WINDOW_NORMAL);
resizeWindow("Detection Window", 640, 1280);
int iterations = 0;
while (true) {
Mat frame, frame1, merged, bgr_image_filter_applied;
cap >> frame;
cap1 >> frame1;
vconcat(frame, frame1, merged);
if (iterations == 0) {
int width = merged.cols;
int height = merged.rows;
video_raw.open(raw_video_path, CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(width, height), true);
video_processed.open(processed_video_path, CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(width, height), true);
}
video_raw << merged;
bgr_image_filter_applied = video_multi_cam_light_detection(merged);
video_processed << bgr_image_filter_applied;
imshow("Capture Window", merged); // Show origional captured image
imshow("Detection Window", bgr_image_filter_applied); // Show processed image
iterations++;
if (waitKey(100) == 27) {
imwrite(raw_image_path, merged);
imwrite(processed_image_path, bgr_image_filter_applied);
video_raw.release();
video_processed.release();
break;
}
}
return 0;
}