Search code examples
c++opencvffmpegvideo-capture

Video file stopping in between due to debugging error in openCV C++


I am trying to open a video file using openCV in C++ using visual studio but the video is stopping in between prompting a debugging error not sure what to do, I tried some things taking reference from some solutions but none of them is working i.e. switching the runtime library. Here is my code followed by the error message and screenshot of the error.

[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\videoio_registry.cpp (223) cv::`anonymous-namespace'::VideoBackendRegistry::VideoBackendRegistry VIDEOIO: Enabled backends(8, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930)
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (383) cv::impl::getPluginCandidates Found 3 plugin(s) for FFMPEG
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\opencv\build\bin\opencv_videoio_ffmpeg460_64d.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_videoio_ffmpeg460_64d.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_videoio_ffmpeg460_64.dll => OK
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (50) cv::impl::PluginBackend::initCaptureAPI Found entry: 'opencv_videoio_capture_plugin_init_v1'
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (169) cv::impl::PluginBackend::checkCompatibility Video I/O: initialized 'FFmpeg OpenCV Video I/O Capture plugin': built with OpenCV 4.6 (ABI/API = 1/1), current OpenCV version is '4.6.0' (ABI/API = 1/1)
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (69) cv::impl::PluginBackend::initCaptureAPI Video I/O: plugin is ready to use 'FFmpeg OpenCV Video I/O Capture plugin'
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (84) cv::impl::PluginBackend::initWriterAPI Found entry: 'opencv_videoio_writer_plugin_init_v1'
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (169) cv::impl::PluginBackend::checkCompatibility Video I/O: initialized 'FFmpeg OpenCV Video I/O Writer plugin': built with OpenCV 4.6 (ABI/API = 1/1), current OpenCV version is '4.6.0' (ABI/API = 1/1)
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (103) cv::impl::PluginBackend::initWriterAPI Video I/O: plugin is ready to use 'FFmpeg OpenCV Video I/O Writer plugin'
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\highgui\src\registry.impl.hpp (114) cv::highgui_backend::UIBackendRegistry::UIBackendRegistry UI: Enabled backends(4, sorted by priority): GTK(1000); GTK3(990); GTK2(980); WIN32(970) + BUILTIN(WIN32UI)
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\opencv\build\bin\opencv_highgui_gtk460_64.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_highgui_gtk460_64.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\opencv\build\bin\opencv_highgui_gtk3460_64.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_highgui_gtk3460_64.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\opencv\build\bin\opencv_highgui_gtk2460_64.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_highgui_gtk2460_64.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\highgui\src\backend.cpp (90) cv::highgui_backend::createUIBackend UI: using backend: WIN32 (priority=970)
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\highgui\src\window_w32.cpp (3013) cv::impl::Win32BackendUI::createWindow OpenCV/UI: Creating Win32UI window: Image (1)
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\parallel\registry_parallel.impl.hpp (96) cv::parallel::ParallelBackendRegistry::ParallelBackendRegistry core(parallel): Enabled backends(3, sorted by priority): ONETBB(1000); TBB(990); OPENMP(980)
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\opencv\build\bin\opencv_core_parallel_onetbb460_64d.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_onetbb460_64d.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\opencv\build\bin\opencv_core_parallel_tbb460_64d.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_tbb460_64d.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\opencv\build\bin\opencv_core_parallel_openmp460_64d.dll => FAILED
[ INFO:[email protected]] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_openmp460_64d.dll => FAILED
OpenCV(4.6.0) Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file c:\build\master_winpack-build-win64-vc15\opencv\modules\highgui\src\window.cpp, line 967
OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(4.6.0) Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file c:\build\master_winpack-build-win64-vc15\opencv\modules\highgui\src\window.cpp, line 967

Here is my code:

#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;

//Video
int main() {
    string path = "Resources/test_video.mp4";
    VideoCapture cap(path);
    Mat img;
    while (true) {
        cap.read(img);
        imshow("Image", img);
        waitKey(1);
    }
    return EXIT_SUCCESS;
}

And the photo of the error message: enter image description here


Solution

  • There are 2 issues in your code:

    1. You don't verify that the cv::VideoCapture was initialized properly (i.e. that the file is valid).
    2. You try to extract frames from the video file forever (due to the while (true)), even though you will eventually get out of frames and then attempt to use cv::imshow with an empty image.

    See fixed version below:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>
    
    int main()
    {
        cv::VideoCapture capture;
        capture.open("Resources/test_video.mp4");
        if (!capture.isOpened())
        {
            std::cout << "can not open" << std::endl;
            return -1;
        }
    
        cv::Mat img;
        while (capture.read(img))
        {
            imshow("Image", img);
            cv::waitKey(1);
        }
        return 0;
    }