4 Replies Latest reply on Jul 17, 2018 5:51 AM by baptiste-mnh

    I want to save 60 frames per second with png.

    jiin

      I refer to this example librealsense/rs-save-to-disk.cpp at master · IntelRealSense/librealsense · GitHub

      but this sample captures 30 frames and writes the last frame to disk.

      How do I fix this example to save 60 frames per second to disk?

       

        • 1. Re: I want to save 60 frames per second with png.
          MartyG

          I would imagine that on line 30 of the script, you would change i < 30 to i < 60.  This tells the 'For Loop' to keep processing until it reaches the 60th frame, and then go no further and return to the start to begin the loop again and check another 60 frames.

           

          From:

           

          for (auto i = 0; i < 30; ++i) pipe.wait_for_frames();

           

          To:

           

          for (auto i = 0; i < 60; ++i) pipe.wait_for_frames();

          1 of 1 people found this helpful
          • 2. Re: I want to save 60 frames per second with png.
            jiin

            I understand

            but this example only saves the last frame, but I want to store all the frames to disk (all 60 frames)

            Is there anything I need to fix in this code ?

            • 3. Re: I want to save 60 frames per second with png.
              MartyG

              Based on what I have heard from other users about saving PNGs, the program is likely to run very slow if you try to save every frame, and will disrupt the playing of the stream because of this processing lag.  The notes in the script indicate that it is storing up a collection of frames before saving the final frame to give the auto-exposure time to settle.

               

              If you really want to try to save every frame though .. if I were programming this script, I would try deleting lines 37,38 and 56, which seem to set up the 'If' check for whether it is the last frame of the sequence.

              • 4. Re: I want to save 60 frames per second with png.
                baptiste-mnh

                Hi,

                In your for loop you can just store the frame in a std::vector or a std::map and at the end of your loop browse all your frames in your list to store thel in PGN.

                 

                Here is an exemple (I might forgot some include) :

                 

                #include <opencv2/core.hpp>
                
                #include <librealsense2/rs.h>
                
                
                typedef map<string, cv::Mat> FrameMap;
                static FrameMap frame_map;
                
                void saveFrame(const cv::Mat &dMat, const string &filename) {
                  frame_map.emplace(filename, dMat.clone());
                }
                
                void save_ram() {
                   auto full_size = frame_map.size();
                   while(!frame_map.empty()){
                   string filename = frame_map.begin()->first;
                   cv::Mat dMat = frame_map.begin()->second;
                
                   cv::imwrite(filename, dMat);
                   confirmeSave(filename);
                
                   frame_map.erase(filename);
                
                   cout << "Saving images : " << 100*(full_size-frame_map.size())/full_size << "%" << endl;
                   }
                  clear_ram();
                }
                
                void clear_ram() {
                  frame_map.clear();
                }
                
                void export_color(rs2::frameset, std::string filename) {
                   auto colored_frame = frames.get_color_frame();
                   int width = colored_frame.get_width();
                   int height = colored_frame.get_height();
                   cv::Mat dMat(cv::Size(width, height), CV_8UC3, (void*) colored_frame.get_data());
                   cv::Mat rgb_dMat;
                   cv::cvtColor(dMat, rgb_dMat,CV_BGR2RGB);
                   frame_map.emplace(rgb_dMat, filename));
                }
                
                int main(int ac, char **av) {
                // Create a Pipeline - this serves as a top-level API for streaming and processing frames
                rs2::pipeline p;
                rs2::config cfg;
                // Configure and start the pipeline
                cfg.enable_stream(RS2_STREAM_COLOR,1280, 720, RS2_FORMAT_RGB8, 60);
                p.start(cfg);
                
                int n = 0
                while (true)
                {
                   // Block program until frames arrive
                  rs2::frameset frames = p.wait_for_frames(); 
                  export_depth (frames, std::to_string(n++))
                }
                }
                
                1 of 1 people found this helpful