Home > Enterprise >  OpenCV segmentation fault when converting video to PNG sequence
OpenCV segmentation fault when converting video to PNG sequence

Time:09-22

Converting to webm first causes the frame of segmentation fault failure to move to frame 1308, instead of 1301.

My code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;

void conv_vid_png(string input_path, string output_path){

  VideoCapture cap(input_path);
  int count = 0;
  if(!cap.isOpened()){
       cout << "Error opening video stream or file" << endl;
      }
  while(1) {
    Mat frame;
    //Why the fuck does this use the stream insertion operator?
    // >> appears to both increment and store
    cap >> frame;
    //Break the loop when there are no more frames to capture
    if (frame.empty())
          break;

    imwrite(output_path "frame" to_string(count) ".png", frame);
    cout << "frame "   to_string(count)   " processed" << endl; 
    count  ;
  }
}

int main()
{
  string in_path = "./movie_type_files/vert_beach_short.mp4";
  string out_path = "./tmp_png/";

  conv_vid_png(in_path, out_path); //First, convert the video file to png sequence
  return 0;
}

Which uses the OpenCV VideoCapture object to write each frame of a video to PNGs. I did not have a problem with this same function in OpenCV for python. The segmentation fault happens at the same frame (1301) despite there ostensibly being nothing out of the ordinary about that frame.

Thoughts on a solution? I'm currently using ffmpeg to convert the .mp4 to a webm to see if the issue persists. This is my first C program so I am struggling to self diagnose my problem. I followed a guide on diagnosing segmentation faults and have included the results I gathered below.

enter image description here enter image description here

CodePudding user response:

The segmentation fault actually happened in a following function. I thought I had exactly 1340 frames in this render, but it turns out some got lost in some processing I did, so the code in the original question was 100% correct. The segmentation fault was caused by a failure to read a file, ( I had forgot to pad zeros before the index of the frame). Since the frame was empty, when I used a pointer to access data that did not exist I got a segmentation fault.

  • Related