video-write.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <iostream> // for standard I/O
  2. #include <string> // for strings
  3. #include <opencv2/core.hpp> // Basic OpenCV structures (cv::Mat)
  4. #include <opencv2/videoio.hpp> // Video write
  5. using namespace std;
  6. using namespace cv;
  7. static void help()
  8. {
  9. cout
  10. << "------------------------------------------------------------------------------" << endl
  11. << "This program shows how to write video files." << endl
  12. << "You can extract the R or G or B color channel of the input video." << endl
  13. << "Usage:" << endl
  14. << "./video-write <input_video_name> [ R | G | B] [Y | N]" << endl
  15. << "------------------------------------------------------------------------------" << endl
  16. << endl;
  17. }
  18. int main(int argc, char *argv[])
  19. {
  20. help();
  21. if (argc != 4)
  22. {
  23. cout << "Not enough parameters" << endl;
  24. return -1;
  25. }
  26. const string source = argv[1]; // the source file name
  27. const bool askOutputType = argv[3][0] =='Y'; // If false it will use the inputs codec type
  28. VideoCapture inputVideo(source); // Open input
  29. if (!inputVideo.isOpened())
  30. {
  31. cout << "Could not open the input video: " << source << endl;
  32. return -1;
  33. }
  34. string::size_type pAt = source.find_last_of('.'); // Find extension point
  35. const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi"; // Form the new name with container
  36. int ex = static_cast<int>(inputVideo.get(CAP_PROP_FOURCC)); // Get Codec Type- Int form
  37. // Transform from int to char via Bitwise operators
  38. char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};
  39. Size S = Size((int) inputVideo.get(CAP_PROP_FRAME_WIDTH), // Acquire input size
  40. (int) inputVideo.get(CAP_PROP_FRAME_HEIGHT));
  41. VideoWriter outputVideo; // Open the output
  42. if (askOutputType)
  43. outputVideo.open(NAME, ex=-1, inputVideo.get(CAP_PROP_FPS), S, true);
  44. else
  45. outputVideo.open(NAME, ex, inputVideo.get(CAP_PROP_FPS), S, true);
  46. if (!outputVideo.isOpened())
  47. {
  48. cout << "Could not open the output video for write: " << source << endl;
  49. return -1;
  50. }
  51. cout << "Input frame resolution: Width=" << S.width << " Height=" << S.height
  52. << " of nr#: " << inputVideo.get(CAP_PROP_FRAME_COUNT) << endl;
  53. cout << "Input codec type: " << EXT << endl;
  54. int channel = 2; // Select the channel to save
  55. switch(argv[2][0])
  56. {
  57. case 'R' : channel = 2; break;
  58. case 'G' : channel = 1; break;
  59. case 'B' : channel = 0; break;
  60. }
  61. Mat src, res;
  62. vector<Mat> spl;
  63. for(;;) //Show the image captured in the window and repeat
  64. {
  65. inputVideo >> src; // read
  66. if (src.empty()) break; // check if at end
  67. split(src, spl); // process - extract only the correct channel
  68. for (int i =0; i < 3; ++i)
  69. if (i != channel)
  70. spl[i] = Mat::zeros(S, spl[0].type());
  71. merge(spl, res);
  72. //outputVideo.write(res); //save or
  73. outputVideo << res;
  74. }
  75. cout << "Finished writing" << endl;
  76. return 0;
  77. }