opencv_video_example/video_example.cpp

106 lines
2.1 KiB
C++

#include <iostream>
#include <opencv2/opencv.hpp>
#include <sys/stat.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
std::vector<std::string> supportedExt = {
"mp4", "MP4",
"mov", "MOV",
"mkv", "MKV"
};
inline bool in_array(const std::string &value, const std::vector<std::string> &array)
{
return std::find(array.begin(), array.end(), value) != array.end();
}
inline bool file_exists (const std::string& name)
{
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
inline bool is_video (const std::string& name)
{
string ext = name.substr(name.find_last_of(".") + 1);
return in_array(ext, supportedExt);
}
int main(int argc, char** argv)
{
Mat image;
Mat inverted;
string input;
if (argc == 2)
{
input = argv[1];
}
else if (argc < 2)
{
cerr << "Please provide one video." << endl;
return -1;
}
else if (argc > 2)
{
cerr << "Please provide only one video." << endl;
return -2;
}
if (!file_exists(input))
{
cerr << "File " << input << " does not exist." << endl;
return -3;
}
if (!is_video(input))
{
cerr << "File " << input << " is not a valid video." << endl;
return -4;
}
VideoCapture cap(input);
int frameNumbers = (int)cap.get(CAP_PROP_FRAME_COUNT);
double fps = cap.get(CAP_PROP_FPS);
cout << frameNumbers << endl;
cout << fps << endl;
if (cap.isOpened() == false) {
cerr << "Cannot open the video file " << input << "." << endl;
return -5;
}
cout << "Using video " << input << "." << endl;
while(1)
{
Mat frame;
Mat inverted;
cap >> frame;
if (frame.empty())
{
break;
}
bitwise_not(frame, inverted);
imshow( "Video", inverted );
char c= (char) waitKey(25);
if (c == 27)
{
break;
}
}
cap.release();
destroyAllWindows();
return 0;
}