I am encountering an issue with my project where I am unable to open the ‘test.mp4’ file using OpenCV in a C++ program on an Ubuntu WSL (Windows Subsystem for Linux) environment. I would greatly appreciate your assistance in resolving this matter.
//Code
#include <opencv2/opencv.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include
#include
int main() {
// Open the MP4 file
cv::VideoCapture video_capture(“test.mp4”);
// Check if the video file was opened successfully
if (!video_capture.isOpened()) {
std::cout << "Error: Could not open video file." << std::endl;
return -1;
}
// Get the FPS (frames per second) of the video
int fps = static_cast<int>(video_capture.get(cv::CAP_PROP_FPS));
// Create a background subtractor
cv::Ptr<cv::BackgroundSubtractorMOG2> backSub = cv::createBackgroundSubtractorMOG2();
int count_frame = 0;
// Record the start time
auto start_time = std::chrono::high_resolution_clock::now();
while (true) {
// Read a frame from the video
cv::Mat frame;
bool ret = video_capture.read(frame);
count_frame++;
// Check if the frame was read successfully
if (!ret) {
break; // End of video
}
// Apply background subtraction
cv::Mat fgMask;
backSub->apply(frame, fgMask);
cv::cvtColor(fgMask, fgMask, cv::COLOR_GRAY2BGR);
// Further image processing (erosion, dilation, Gaussian blur, thresholding, Canny edge detection)
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
cv::erode(fgMask, fgMask, kernel, cv::Point(-1, -1), 1);
cv::dilate(fgMask, fgMask, kernel, cv::Point(-1, -1), 1);
cv::GaussianBlur(fgMask, fgMask, cv::Size(3, 3), 0);
cv::morphologyEx(fgMask, fgMask, cv::MORPH_CLOSE, kernel);
cv::threshold(fgMask, fgMask, 130, 255, cv::THRESH_BINARY);
cv::Canny(fgMask, fgMask, 20, 200);
// Find contours in the processed mask
std::vector<std::vector<cv::Point>> contours;
cv::findContours(fgMask, contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
// Iterate through detected contours
for (const auto& contour : contours) {
cv::Rect rect = cv::boundingRect(contour);
double area = cv::contourArea(contour);
// Check if the object meets size criteria (width < 64 or height < 128)
if (rect.width < 64 || rect.height < 128) {
continue; // Skip this object
}
// Draw bounding boxes around objects that pass the criteria
cv::rectangle(frame, rect, cv::Scalar(0, 255, 0), 2);
}
// Display the processed frame with contours and bounding boxes
cv::imshow("Frame", frame);
// Check for user input to exit the program (press 'q' or ESC)
int keyboard = cv::waitKey(30);
if (keyboard == 'q' || keyboard == 27) {
break;
}
}
// Record the end time
auto end_time = std::chrono::high_resolution_clock::now();
// Calculate and print the elapsed time
double elapsed_time = std::chrono::duration<double>(end_time - start_time).count();
std::cout << "Total processing time: " << elapsed_time << " seconds" << std::endl;
// Print the total number of frames processed
std::cout << "Total frames processed: " << count_frame << std::endl;
// Release the video capture object and close any open windows
video_capture.release();
cv::destroyAllWindows();
return 0;
}