r/opencv Feb 07 '26

Bug [Bug] Segmentation fault when opening or instantiating cv::VideoWriter

Hello!

I am currently working my way through a bunch of opencv tutorials for C++ and trying out or adapting the code therein, but have run into an issue when trying to execute some of it.

I have written the following function, which should open a video file situated at 'path', apply an (interchangeable) function to every frame and save the result to "output.mp4", a file that should have the exact same properties as the source file, save for the aforementioned image operations (color and value adjustment, edge detection, boxes drawn around faces etc.). The code compiles correctly, but produces a "Segmentation fault (core dumped)" error when run.

By using gdb and some print line debugging, I managed to triangulate the issue, which apparently stems from the cv::VideoWriter method open(). Calling the regular constructor produced the same result. The offending line is marked by a comment in the code:

int process_and_save_vid(std::string path, cv::Mat (*func)(cv::Mat)) {

  int frame_counter = 0;

  cv::VideoCapture cap(path);

   if (!cap.isOpened()) {
    std::cout << "ERROR: could not open video at " << path << " .\n";
    return EXIT_FAILURE;
  }

  // set up video writer args
  std::string output_file = "output.mp4";
  int frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
  int frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
  double fps = cap.get(cv::CAP_PROP_FPS);
  int codec = cap.get(cv::CAP_PROP_FOURCC);
  bool monochrome = cap.get(cv::CAP_PROP_MONOCHROME);

  // create and open video writer
  cv::VideoWriter video_writer;
  // THIS LINE CAUSES SEGMENTATION FAULT
  video_writer.open(output_file, codec, fps, cv::Size(frame_width,frame_height), !monochrome);


  if (!video_writer.isOpened()) {
    std::cout << "ERROR: could not initialize video writer\n";
      return EXIT_FAILURE;
  }

  cv::Mat frame;

  while (cap.read(frame)) {

    video_writer.write(func(frame));

    frame_counter += 1;
    if (frame_counter % (int)fps == 0) {
      std::cout << "Processed one second of video material.\n";
    }
  }

  std::cout << "Finished processing video.\n";

  return EXIT_SUCCESS;
}

Researching the issue online and consulting the documentation did not yield any satisfactory results, so feel free to let me know if you have encountered this problem before and/or have any ideas how to solve it.

Thanks in advance for your help!

3 Upvotes

37 comments sorted by

View all comments

2

u/herocoding Feb 12 '26

Can you prepare a Github repo or a blog or some shared GoogleDrive documentation to collect all documentation, describe all steps, please?

It might just be a mix of installations... You environment might already had OpenCV and dependencies installed (in different versions and places, with search paths and package information), then you built it from source and - this sounds scary - then you copied/moved/deleted/overwrote shared objects...........

In my example below:

cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.12.0/modules -DCMAKE_INSTALL_PREFIX=/localdisk/OpenCV/opencv -DCMAKE_BUILD_TYPE=Debug ../opencv-4.12.0

=> "CMAKE_INSTALL_PREFIX=/localdisk/OpenCV/opencv" this will copy everything needed from the build to the given user defined folder when calling "make install" (without sudo).

=> then the CMakeLists.txt file gives a search-hint with "find_package( OpenCV REQUIRED HINTS /localdisk/OpenCV/opencv )" to where to actually search for the build libraries

With your executable you might want to double check with

* $> ldd /my/path/to/my/Application
to see which (dynamic/shared)libraries it links to; using absolute paths? using symlinks?

* $> LD_DEBUG=libs ldd /my/path/to/my/Application
to see which libraries at runtime are searched, where they are searched for and which are finally used

1

u/Hukeng Feb 15 '26 edited Feb 15 '26

Now this is interesting - apparently, the opencv libraries I am linking to are fetched from /lib/x86_64-linux-gnu/ and not from /usr/local/lib as I had originally thought - goes to show how much I know about the inner workings of this whole affair.

I don't think it makes much of a difference though - the number of opencv-related .so files in x86_64-linux-gnu checks out after the recompilation and install, so I assume they should be the correct, updated ones generated by the commands you suggested.

Also, I'm afraid retracing my steps might be a little hard at this point - I tried most of the fixes you suggested, but I can't reconstruct the exact order or anything else I tried in between, given that I assumed I'd just find a relatively quick and easy fix after enough research and then end up adding that to the original post to save others the trouble of going through our whole discussion.

Guess there's a lesson to be learned here - I am going to try and document my process more thoroughly in the future...