4 Replies Latest reply on Jun 9, 2017 1:54 PM by Intel Corporation

    Cannot open cv::VideoWriter() with ISS IoT Edition C++

    yk0207

      I'm developing the software which saves the movie captured by using webcam as video file (output.avi etc) to local of Intel Edison(/home/root/temp etc).
      I'm using ISS IoT Edition C++ as IDE, and OpenCV2.4.9 as Image Processing Library.

       

      When I tried to create video file(/home/root/temp/output.avi) using cv::VideoWriter() method which is prepared by OpenCV2.4.9, I found that video file was not created.

       

      I don't know the cause of this problem...Are there any other packages to install such as codec?

      If someone knows how to deal with this problem , could you please tell me that?

       

      For your information in detail, I show you the code I created below.
      Best regards, thanks.

       

       

      #include <iostream>
      #include <stdio.h>
      #include <opencv/cv.h>
      #include <opencv2/highgui/highgui.hpp>
      #include <opencv2/imgproc/imgproc.hpp>

      const int videolength = 3;
      const int width = 640;
      const int height = 480;
      const double framerate = 20.0;
      const int imgnums = int(videolength*framerate);

      int main()
      {
      cv::Mat frames[imgnums];
      cv::VideoCapture cap(0);
      if(!cap.isOpened())
      {
        std::cerr << "Error: Camera not found" << std::endl;
        return -1;
      }

      std::cout << "Camera found" << std::endl;

      cap.set(CV_CAP_PROP_FRAME_WIDTH,width); //width=640pix
      cap.set(CV_CAP_PROP_FRAME_HEIGHT,height); //height=480pix
      cap.set(CV_CAP_PROP_FPS,framerate); //framerate=20fps

      int i = 0;
      char buf[256];
      while (cap.isOpened())
      {
        if(!cap.read(frames[i]))
        {
         std::cerr << "Error: Blank frame grabbed" << std::endl;
         break;
        }
        sprintf(buf, "/home/root/output_%02d.jpg", i);
        cv::imwrite(std::string(buf), frames[i++]);
        if (i == imgnums)
        {
         i = 0;
        }

        if(cv::waitKey(30) >= 0)
        {
         std::cout << "Key input was occurred" << std::endl;
         break;
        }
      }

      cap.release();

       

      //Video output
      cv::VideoWriter out;
      int codec = CV_FOURCC('M', 'J', 'P', 'G');
      std::string filename = "/home/root/output.avi";

      try
      {
        out.open(filename, codec, framerate, cv::Size(width, height));
      }
      catch (cv::Exception& e)
      {
        const char* err_msg = e.what();
        std::cerr << err_msg << std::endl;
        return -1;
      }

      if(!out.isOpened())
      {
        std::cerr << "Could not open the output video file" << std::endl;                     ⇒⇒⇒ that is why out.isOpened() == false.
        return -1;
      }

      for (i = 0; i < imgnums; i++)
      {
        out.write(frames[i]);
      }

      out.release();
      return 0;
      }