Added the How to create videos with OpenCV Tutorial. Made some corrections to the...
authorBernat Gabor <no@email>
Sat, 13 Aug 2011 14:02:18 +0000 (14:02 +0000)
committerBernat Gabor <no@email>
Sat, 13 Aug 2011 14:02:18 +0000 (14:02 +0000)
doc/tutorials/features2d/table_of_content_features2d/table_of_content_features2d.rst
doc/tutorials/features2d/trackingmotion/good_features_to_track/good_features_to_track.rst
doc/tutorials/highgui/table_of_content_highgui/images/video-write.png [new file with mode: 0644]
doc/tutorials/highgui/table_of_content_highgui/table_of_content_highgui.rst
doc/tutorials/highgui/video-input-psnr-ssim/video-input-psnr-ssim.rst
doc/tutorials/highgui/video-write/images/resultOutputWideoWrite.png [new file with mode: 0644]
doc/tutorials/highgui/video-write/images/videoCompressSelect.png [new file with mode: 0644]
doc/tutorials/highgui/video-write/images/videoFileStructure.png [new file with mode: 0644]
doc/tutorials/highgui/video-write/video-write.rst [new file with mode: 0644]
samples/cpp/tutorial_code/HighGUI/video-write/video-write.cpp [new file with mode: 0644]

index e41dee9..bfaf9b0 100644 (file)
@@ -3,7 +3,7 @@
 *feature2d* module. 2D Features framework
 -----------------------------------------------------------
 
-Learn about how to use the feature points  detectors, descriptors and matching framework found inside OpenCV.ddddddd
+Learn about how to use the feature points  detectors, descriptors and matching framework found inside OpenCV.
 
 .. include:: ../../definitions/tocDefinitions.rst 
 
@@ -190,7 +190,11 @@ Learn about how to use the feature points  detectors, descriptors and matching f
 .. toctree::
    :hidden:
 
+   ../feature_description/feature_description
+   ../feature_detection/feature_detection
    ../trackingmotion/harris_detector/harris_detector
+   ../feature_flann_matcher/feature_flann_matcher
+   ../feature_homography/feature_homography
    ../trackingmotion/good_features_to_track/good_features_to_track.rst
    ../trackingmotion/generic_corner_detector/generic_corner_detector
    ../trackingmotion/corner_subpixeles/corner_subpixeles
index 0078236..7fad76e 100644 (file)
@@ -88,14 +88,14 @@ This tutorial code's is shown lines below. You can also download it from `here <
 
      /// Apply corner detection
      goodFeaturesToTrack( src_gray, 
-                         corners,
-                         maxCorners,
-                         qualityLevel,
-                         minDistance,
-                         Mat(),
-                         blockSize,
-                         useHarrisDetector,
-                         k );
+                  corners,
+                  maxCorners,
+                  qualityLevel,
+                  minDistance,
+                  Mat(),
+                  blockSize,
+                  useHarrisDetector,
+                  k );
   
 
      /// Draw corners detected
@@ -116,7 +116,7 @@ Explanation
 Result
 ======
 
-.. image:: images/Shi_Tomasi_Detector_Result.jpg
+.. image:: images/Feature_Detection_Result_a.jpg
               :align: center   
 
 
diff --git a/doc/tutorials/highgui/table_of_content_highgui/images/video-write.png b/doc/tutorials/highgui/table_of_content_highgui/images/video-write.png
new file mode 100644 (file)
index 0000000..9413d80
Binary files /dev/null and b/doc/tutorials/highgui/table_of_content_highgui/images/video-write.png differ
index b03f1a6..753ff34 100644 (file)
@@ -45,6 +45,26 @@ This section contains valuable tutorials about how to read/save your image/video
                    :height: 90pt\r
                    :width:  90pt\r
 \r
++ \r
+  .. tabularcolumns:: m{100pt} m{300pt}\r
+  .. cssclass:: toctableopencv\r
+\r
+  =============== ======================================================\r
+  |hVideoWrite|   *Title:* :ref:`videoWriteHighGui`\r
+\r
+                  *Compatibility:* > OpenCV 2.0\r
+\r
+                  *Author:* |Author_BernatG|\r
+\r
+                  Whenever you work with video feeds you may eventually want to save your image processing result in a form of a new video file. Here's how to do it.\r
+\r
+  =============== ======================================================\r
+\r
+  .. |hVideoWrite| image:: images/video-write.png\r
+                   :height: 90pt\r
+                   :width:  90pt\r
+\r
+\r
 .. raw:: latex\r
 \r
    \pagebreak\r
index 4bde0be..cb45a5b 100644 (file)
@@ -18,7 +18,7 @@ The source code
 \r
 As a test case where to show off these using OpenCV I've created a small program that reads in two video files and performs a similarity check between them. This is something you could use to check just how well a new video compressing algorithms works. Let there be a reference (original) video like :download:`this small Megamind clip <../../../../samples/cpp/tutorial_code/highgui/video-input-psnr-ssim/video/Megamind.avi>` and :download:`a compressed version of it <../../../../samples/cpp/tutorial_code/highgui/video-input-psnr-ssim/video/Megamind_bugy.avi>`. You may also find the source code and these video file in the :file:`samples/cpp/tutorial_code/highgui/video-input-psnr-ssim/` folder of the OpenCV source library.\r
 \r
-.. literalinclude:: ../../../../samples/cpp/tutorial_code/HighGUI\video-input-psnr-ssim\video-input-psnr-ssim.cpp\r
+.. literalinclude:: ../../../../samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video-input-psnr-ssim.cpp\r
    :language: cpp\r
    :linenos:\r
    :tab-width: 4\r
diff --git a/doc/tutorials/highgui/video-write/images/resultOutputWideoWrite.png b/doc/tutorials/highgui/video-write/images/resultOutputWideoWrite.png
new file mode 100644 (file)
index 0000000..bea15a3
Binary files /dev/null and b/doc/tutorials/highgui/video-write/images/resultOutputWideoWrite.png differ
diff --git a/doc/tutorials/highgui/video-write/images/videoCompressSelect.png b/doc/tutorials/highgui/video-write/images/videoCompressSelect.png
new file mode 100644 (file)
index 0000000..a72fc01
Binary files /dev/null and b/doc/tutorials/highgui/video-write/images/videoCompressSelect.png differ
diff --git a/doc/tutorials/highgui/video-write/images/videoFileStructure.png b/doc/tutorials/highgui/video-write/images/videoFileStructure.png
new file mode 100644 (file)
index 0000000..cef586d
Binary files /dev/null and b/doc/tutorials/highgui/video-write/images/videoFileStructure.png differ
diff --git a/doc/tutorials/highgui/video-write/video-write.rst b/doc/tutorials/highgui/video-write/video-write.rst
new file mode 100644 (file)
index 0000000..0bebf82
--- /dev/null
@@ -0,0 +1 @@
+.. _videoWriteHighGui:\r\rCreating a video with OpenCV\r****************************\r\rGoal\r====\r\rWhenever you work with video feeds you may eventually want to save your image processing result in a form of a new video file. For simple video outputs you can use the OpenCV built-in :huivideo:`VideoWriter <videowriter-videowriter>` class, designed for this. \r\r.. container:: enumeratevisibleitemswithsquare\r\r   + How to create a video file with OpenCV\r   + What type of video files you can create with OpenCV\r   + How to extract a given color channel from a video\r\rAs a simple demonstration I'll just extract one of the RGB color channels of an input video file into a new video. You can control the flow of the application from its console line arguments:\r\r.. container:: enumeratevisibleitemswithsquare\r\r   + The first argument points to the video file to work on\r   + The second argument may be one of the characters: R G B. This will specify which of the channels to extract.\r   + The last argument is the character Y (Yes) or N (No). If this is no, the codec used for the input video file will be the same as for the output. Otherwise, a window will pop up and allow you to select yourself the codec to use.\r\rFor example, a valid command line would look like:\r\r.. code-block:: bash\r\r   video-write.exe video/Megamind.avi R Y\r\rThe source code\r===============\r\r\rYou may also find the source code and these video file in the :file:`samples/cpp/tutorial_code/highgui/video-write/` folder of the OpenCV source library or :download:`download it from here <../../../../samples/cpp/tutorial_code/HighGUI/video-write/video-write.cpp>`.\r\r.. literalinclude:: ../../../../samples/cpp/tutorial_code/HighGUI/video-write/video-write.cpp\r   :language: cpp\r   :linenos:\r   :tab-width: 4\r   :lines: 1-8, 21-22, 24-97\r\rThe structure of a video\r========================\r\rFor start, you should have an idea of just how a video file looks. Every video file in itself is a container. The type of the container is expressed in the files extension (for example *avi*, *mov* or *mkv*). This contains multiple elements like: video feeds, audio feeds or other tracks (like for example subtitles). How these feeds are stored is determined by the codec used for each one of them. In case of the audio tracks commonly used codecs are *mp3* or *aac*. For the video files the list is somehow longer and includes names such as *XVID*, *DIVX*, *H264* or *LAGS* (*Lagarith Lossless Codec*). The full list of codecs you may use on a system depends on just what one you have installed. \r\r.. image:: images/videoFileStructure.png\r   :alt: The Structure of the video\r   :align: center\r\rAs you can see things can get really complicated with videos. However, OpenCV is mainly a computer vision library, not a video stream, codec and write one. Therefore, the developers tried to keep this part as simple as possible. Due to this OpenCV for video containers supports only the *avi* extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you can only create and expand a single video track inside the container. No audio or other track editing support here. Nevertheless, any video codec present on your system might work. If you encounter some of these limitations you will need to look into more specialized video writing libraries such as *FFMpeg* or codecs as *HuffYUV*, *CorePNG* and *LCL*. As an alternative, create the video track with OpenCV and expand it with sound tracks or convert it to other formats by using video manipulation programs such as *VirtualDub* or *AviSynth*.\r\rThe *VideoWriter* class\r=======================\r\rThe content written here builds on the assumption you already read the :ref:`videoInputPSNRMSSIM` tutorial and you know how to read video files.\r\rTo create a video file you just need to create an instance of the :huivideo:`VideoWriter <videowriter-videowriter>` class. You can specify its properties either via parameters in the constructor or later on via the :huivideo:`open <videowriter-open>` function. Either way, the parameters are the same:\r\r1. The name of the output that contains the container type in its extension. At the moment only *avi* is supported. We construct this from the input file, add to this the name of the channel to use, and finish it off with the container extension.\r\r   .. code-block:: cpp\r\r      const string source      = argv[1];            // the source file name\r      string::size_type pAt = source.find_last_of('.');   // Find extension point\r      const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // Form the new name with container\r\r#. The codec to use for the video track. Now all the video codecs have a unique short name of maximum four characters. Hence, the *XVID*, *DIVX* or *H264* names. This is called a four character code. You may also ask this from an input video by using its *get* function. Because the *get* function is a general function it always returns double values. A double value is stored on 64 bits. Four characters are four bytes, meaning 32 bits. These four characters are coded in the lower 32 bits of the *double*. A simple way to throw away the upper 32 bits would be to just convert this value to *int*: \r\r   .. code-block:: cpp\r\r      VideoCapture inputVideo(source);                                   // Open input\r      int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // Get Codec Type- Int form\r\r   OpenCV internally works with this integer type and expect this as its second parameter. Now to convert from the integer form to string we may use two methods: a bitwise operator and a union method. The first one extracting from an int the characters looks like (an "and" operation, some shifting and adding a 0 at the end to close the string):\r\r   .. code-block:: cpp\r\r      char EXT[] = {ex & 0XFF , (ex & 0XFF00) >> 8,(ex & 0XFF0000) >> 16,(ex & 0XFF000000) >> 24, 0};\r\r   You can do the same thing with the *union* as:\r\r   .. code-block:: cpp\r\r      union { int v; char c[5];} uEx ;\r      uEx.v = ex;                              // From Int to char via union\r      uEx.c[4]='\0';\r\r   The advantage of this is that the conversion is done automatically after assigning, while for the bitwise operator you need to do the operations whenever you change the codec type. In case you know the codecs four character code beforehand, you can use the *CV_FOURCC* macro to build the integer:\r\r   .. code-block::cpp\r\r      CV_FOURCC('P','I','M,'1') // this is an MPEG1 codec from the characters to integer\r\r   If you pass for this argument minus one than a window will pop up at runtime that contains all the codec installed on your system and ask you to select the one to use: \r\r   .. image:: images/videoCompressSelect.png\r      :alt: Select the codec type to use\r      :align: center\r\r#. The frame per second for the output video. Again, here I keep the input videos frame per second by using the *get* function.\r\r#. The size of the frames for the output video. Here too I keep the input videos frame size per second by using the *get* function.\r\r#. The final argument is an optional one. By default is true and says that the output will be a colorful one (so for write you will send three channel images). To create a gray scale video pass a false parameter here.\r\rHere it is, how I use it in the sample:\r\r.. code-block:: cpp\r\r   VideoWriter outputVideo;\r   Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),    //Acquire input size\r                 (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));    \r\r   outputVideo.open(NAME , ex, inputVideo.get(CV_CAP_PROP_FPS),S, true);\r\rAfterwards, you use the :huivideo:`isOpened() <videowriter-isopened>` function to find out if the open operation succeeded or not. The video file automatically closes when the *VideoWriter* object is destroyed. After you open the object with success you can send the frames of the video in a sequential order by using the :huivideo:`write<videowriter-write>` function of the class. Alternatively, you can use its overloaded operator << : \r\r.. code-block:: cpp\r\r    outputVideo.write(res);  //or\r    outputVideo << res;\r\rExtracting a color channel from an RGB image means to set to zero the RGB values of the other channels. You can either do this with image scanning operations or by using the split and merge operations. You first split the channels up into different images, set the other channels to zero images of the same size and type and finally merge them back: \r\r.. code-block:: cpp\r\r   split(src, spl);                 // process - extract only the correct channel\r   for( int i =0; i < 3; ++i)       \r      if (i != channel)\r         spl[i] = Mat::zeros(S, spl[0].type());\r   merge(spl, res);\r\rPut all this together and you'll get the upper source code, whose runtime result will show something around the idea: \r\r.. image:: images/resultOutputWideoWrite.png\r   :alt: A sample output\r   :align: center\r\rYou may observe a runtime instance of this on the `YouTube here <https://www.youtube.com/watch?v=jpBwHxsl1_0>`_. \r\r.. raw:: html\r\r  <div align="center">\r  <iframe title="Creating a video with OpenCV" width="560" height="349" src="http://www.youtube.com/embed/jpBwHxsl1_0?rel=0&loop=1" frameborder="0" allowfullscreen align="middle"></iframe>\r  </div>\r
\ No newline at end of file
diff --git a/samples/cpp/tutorial_code/HighGUI/video-write/video-write.cpp b/samples/cpp/tutorial_code/HighGUI/video-write/video-write.cpp
new file mode 100644 (file)
index 0000000..824dd71
--- /dev/null
@@ -0,0 +1,97 @@
+#include <iostream>    // for standard I/O\r
+#include <string>   // for strings\r
+\r
+#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat)\r
+#include <opencv2/highgui/highgui.hpp>  // Video write\r
+\r
+using namespace std;\r
+using namespace cv;\r
+\r
+void help()\r
+{\r
+    cout\r
+        << "\n--------------------------------------------------------------------------" << endl\r
+        << "This program shows how to write video files. You can extract the R or G or B color channel "\r
+        << " of the input video.write "  << endl\r
+        << "Usage:"                                                               << endl\r
+        << "./video-write inputvideoName [ R | G | B] [Y | N]"                   << endl\r
+        << "--------------------------------------------------------------------------"   << endl\r
+        << endl;\r
+}\r
+int main(int argc, char *argv[], char *window_name)\r
+{\r
+    help();\r
+    if (argc != 4)\r
+    {\r
+        cout << "Not enough parameters" << endl;\r
+        return -1;\r
+    }\r
+\r
+    const string source      = argv[1];            // the source file name\r
+    const bool askOutputType = argv[3][0] =='Y';  // If false it will use the inputs codec type\r
+   \r
+    VideoCapture inputVideo(source);        // Open input\r
+    if ( !inputVideo.isOpened())\r
+    {\r
+        cout  << "Could not open the input video." << source << endl;\r
+        return -1;\r
+    }\r
+\r
+    string::size_type pAt = source.find_last_of('.');   // Find extension point\r
+    const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // Form the new name with container\r
+    int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // Get Codec Type- Int form\r
+\r
+    // Transform from int to char via Bitwise operators\r
+    char EXT[] = {ex & 0XFF , (ex & 0XFF00) >> 8,(ex & 0XFF0000) >> 16,(ex & 0XFF000000) >> 24, 0};\r
+\r
+    Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),    //Acquire input size\r
+                  (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));    \r
+\r
+    VideoWriter outputVideo;                                        // Open the output\r
+    if (askOutputType)\r
+            outputVideo.open(NAME  , ex=-1, inputVideo.get(CV_CAP_PROP_FPS),S, true);  \r
+    else\r
+        outputVideo.open(NAME , ex, inputVideo.get(CV_CAP_PROP_FPS),S, true);\r
+    \r
+    if (!outputVideo.isOpened())\r
+    {\r
+        cout  << "Could not open the output video for write: " << source << endl;\r
+        return -1;\r
+    }\r
+\r
+    union { int v; char c[5];} uEx ;\r
+    uEx.v = ex;                              // From Int to char via union\r
+    uEx.c[4]='\0';\r
+\r
+    cout << "Input frame resolution: Width=" << S.width << "  Height=" << S.height\r
+        << " of nr#: " << inputVideo.get(CV_CAP_PROP_FRAME_COUNT) << endl;\r
+    cout << "Input codec type: " << EXT << endl;\r
+\r
+    int channel = 2;    // Select the channel to save\r
+    switch(argv[2][0])\r
+    {\r
+    case 'R' : {channel = 2; break;}\r
+    case 'G' : {channel = 1; break;}\r
+    case 'B' : {channel = 0; break;}\r
+    }\r
+    Mat src,res;\r
+    vector<Mat> spl; \r
+\r
+    while( true) //Show the image captured in the window and repeat\r
+    {\r
+        inputVideo >> src;              // read\r
+        if( src.empty()) break;         // check if at end\r
+\r
+       split(src, spl);                 // process - extract only the correct channel\r
+       for( int i =0; i < 3; ++i)       \r
+        if (i != channel)\r
+           spl[i] = Mat::zeros(S, spl[0].type());\r
+       merge(spl, res);\r
+\r
+       //outputVideo.write(res); //save or\r
+       outputVideo << res;\r
+    }\r
+\r
+    cout << "Finished writing" << endl;\r
+    return 0;\r
+}
\ No newline at end of file