2012-10-17 07:18:30 +08:00
|
|
|
#include <iostream> // for standard I/O
|
|
|
|
#include <string> // for strings
|
|
|
|
|
|
|
|
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat)
|
2014-07-10 22:27:32 +08:00
|
|
|
#include <opencv2/videoio/videoio.hpp> // Video write
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
static void help()
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
cout
|
2012-11-07 22:21:20 +08:00
|
|
|
<< "------------------------------------------------------------------------------" << endl
|
|
|
|
<< "This program shows how to write video files." << endl
|
|
|
|
<< "You can extract the R or G or B color channel of the input video." << endl
|
|
|
|
<< "Usage:" << endl
|
|
|
|
<< "./video-write inputvideoName [ R | G | B] [Y | N]" << endl
|
|
|
|
<< "------------------------------------------------------------------------------" << endl
|
2012-10-17 07:18:30 +08:00
|
|
|
<< endl;
|
|
|
|
}
|
2012-11-07 22:21:20 +08:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
help();
|
2012-11-07 22:21:20 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
if (argc != 4)
|
|
|
|
{
|
|
|
|
cout << "Not enough parameters" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
const string source = argv[1]; // the source file name
|
2012-10-17 07:18:30 +08:00
|
|
|
const bool askOutputType = argv[3][0] =='Y'; // If false it will use the inputs codec type
|
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
VideoCapture inputVideo(source); // Open input
|
|
|
|
if (!inputVideo.isOpened())
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2012-11-07 22:21:20 +08:00
|
|
|
cout << "Could not open the input video: " << source << endl;
|
2012-10-17 07:18:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
string::size_type pAt = source.find_last_of('.'); // Find extension point
|
2012-10-17 07:18:30 +08:00
|
|
|
const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi"; // Form the new name with container
|
2013-04-08 02:45:38 +08:00
|
|
|
int ex = static_cast<int>(inputVideo.get(CAP_PROP_FOURCC)); // Get Codec Type- Int form
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
// Transform from int to char via Bitwise operators
|
2012-11-07 22:21:20 +08:00
|
|
|
char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
Size S = Size((int) inputVideo.get(CAP_PROP_FRAME_WIDTH), // Acquire input size
|
|
|
|
(int) inputVideo.get(CAP_PROP_FRAME_HEIGHT));
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
VideoWriter outputVideo; // Open the output
|
|
|
|
if (askOutputType)
|
2013-04-08 02:45:38 +08:00
|
|
|
outputVideo.open(NAME, ex=-1, inputVideo.get(CAP_PROP_FPS), S, true);
|
2012-10-17 07:18:30 +08:00
|
|
|
else
|
2013-04-08 02:45:38 +08:00
|
|
|
outputVideo.open(NAME, ex, inputVideo.get(CAP_PROP_FPS), S, true);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
if (!outputVideo.isOpened())
|
|
|
|
{
|
|
|
|
cout << "Could not open the output video for write: " << source << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Input frame resolution: Width=" << S.width << " Height=" << S.height
|
2013-04-08 02:45:38 +08:00
|
|
|
<< " of nr#: " << inputVideo.get(CAP_PROP_FRAME_COUNT) << endl;
|
2012-10-17 07:18:30 +08:00
|
|
|
cout << "Input codec type: " << EXT << endl;
|
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
int channel = 2; // Select the channel to save
|
2012-10-17 07:18:30 +08:00
|
|
|
switch(argv[2][0])
|
|
|
|
{
|
2012-11-07 22:21:20 +08:00
|
|
|
case 'R' : channel = 2; break;
|
|
|
|
case 'G' : channel = 1; break;
|
|
|
|
case 'B' : channel = 0; break;
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
2012-11-07 22:21:20 +08:00
|
|
|
Mat src, res;
|
2012-10-17 07:18:30 +08:00
|
|
|
vector<Mat> spl;
|
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
for(;;) //Show the image captured in the window and repeat
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
inputVideo >> src; // read
|
2012-11-07 22:21:20 +08:00
|
|
|
if (src.empty()) break; // check if at end
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
split(src, spl); // process - extract only the correct channel
|
|
|
|
for (int i =0; i < 3; ++i)
|
|
|
|
if (i != channel)
|
|
|
|
spl[i] = Mat::zeros(S, spl[0].type());
|
2012-10-17 07:18:30 +08:00
|
|
|
merge(spl, res);
|
|
|
|
|
|
|
|
//outputVideo.write(res); //save or
|
|
|
|
outputVideo << res;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Finished writing" << endl;
|
|
|
|
return 0;
|
2012-11-07 22:21:20 +08:00
|
|
|
}
|