Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I've taken example code super_resolution.cpp. For creating good quality image from video source. So here code of super_resolution.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <ctype.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/superres/superres.hpp"
#include "opencv2/superres/optical_flow.hpp"
#include "opencv2/opencv_modules.hpp"
#if defined(HAVE_OPENCV_OCL)
#include "opencv2/ocl/ocl.hpp"
#endif
using namespace std;
using namespace cv;
using namespace cv::superres;
bool useOclChanged;
#define MEASURE_TIME(op) \
        TickMeter tm; \
        tm.start(); \
        op; \
        tm.stop(); \
        cout << tm.getTimeSec() << " sec" << endl; \
static Ptr<DenseOpticalFlowExt> createOptFlow(const string& name, bool useGpu)
    if (name == "farneback")
        if (useGpu)
            return createOptFlow_Farneback_GPU();
            return createOptFlow_Farneback();
    else if (name == "simple")
        return createOptFlow_Simple();
    else if (name == "tvl1")
        if (useGpu)
            return createOptFlow_DualTVL1_GPU();
            return createOptFlow_DualTVL1();
    else if (name == "brox")
        return createOptFlow_Brox_GPU();
    else if (name == "pyrlk")
        return createOptFlow_PyrLK_GPU();
        cerr << "Incorrect Optical Flow algorithm - " << name << endl;
        exit(-1);
#if defined(HAVE_OPENCV_OCL)
static Ptr<DenseOpticalFlowExt> createOptFlow(const string& name)
    if (name == "farneback")
        return createOptFlow_Farneback_OCL();
    else if (name == "simple")
        useOclChanged = true;
        std::cout<<"simple on OpenCL has not been implemented. Use CPU instead!\n";
        return createOptFlow_Simple();
    else if (name == "tvl1")
        return createOptFlow_DualTVL1_OCL();
    else if (name == "brox")
        std::cout<<"brox has not been implemented!\n";
        return NULL;
    else if (name == "pyrlk")
        return createOptFlow_PyrLK_OCL();
        cerr << "Incorrect Optical Flow algorithm - " << name << endl;
    return 0;
#endif
int main(int argc, const char* argv[])
    useOclChanged = false;
    CommandLineParser cmd(argc, argv,
        "{ v   | video      |           | Input video }"
        "{ o   | output     |           | Output video }"
        "{ s   | scale      | 4         | Scale factor }"
        "{ i   | iterations | 180       | Iteration count }"
        "{ t   | temporal   | 4         | Radius of the temporal search area }"
        "{ f   | flow       | farneback | Optical flow algorithm (farneback, simple, tvl1, brox, pyrlk) }"
        "{ g   | gpu        |           | CPU as default device, cuda for CUDA and ocl for OpenCL }"
        "{ h   | help       | false     | Print help message }"
    if (cmd.get<bool>("help"))
        cout << "This sample demonstrates Super Resolution algorithms for video sequence" << endl;
        cmd.printParams();
        return 0;
    const string inputVideoName = cmd.get<string>("video");
    const string outputVideoName = cmd.get<string>("output");
    const int scale = cmd.get<int>("scale");
    const int iterations = cmd.get<int>("iterations");
    const int temporalAreaRadius = cmd.get<int>("temporal");
    const string optFlow = cmd.get<string>("flow");
    string gpuOption = cmd.get<string>("gpu");
    std::transform(gpuOption.begin(), gpuOption.end(), gpuOption.begin(), ::tolower);
    bool useCuda = false;
    bool useOcl = false;
    if(gpuOption.compare("ocl") == 0)
        useOcl = true;
    else if(gpuOption.compare("cuda") == 0)
        useCuda = true;
#ifndef HAVE_OPENCV_OCL
    if(useOcl)
            cout<<"OPENCL is not compiled\n";
            return 0;
#endif
#if defined(HAVE_OPENCV_OCL)
    if(useCuda)
        CV_Assert(!useOcl);
#endif
    Ptr<SuperResolution> superRes;
#if defined(HAVE_OPENCV_OCL)
    if(useOcl)
        Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow);
        if (of.empty())
            exit(-1);
        if(useOclChanged)
            superRes = createSuperResolution_BTVL1();
            useOcl = !useOcl;
        }else
            superRes = createSuperResolution_BTVL1_OCL();
        superRes->set("opticalFlow", of);
#endif
        if (useCuda)
            superRes = createSuperResolution_BTVL1_GPU();
            superRes = createSuperResolution_BTVL1();
        Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);
        if (of.empty())
            exit(-1);
        superRes->set("opticalFlow", of);
    superRes->set("scale", scale);
    superRes->set("iterations", iterations);
    superRes->set("temporalAreaRadius", temporalAreaRadius);
    Ptr<FrameSource> frameSource;
    if (useCuda)
        // Try to use gpu Video Decoding
            frameSource = createFrameSource_Video_GPU(inputVideoName);
            Mat frame;
            frameSource->nextFrame(frame);
        catch (const cv::Exception&)
            frameSource.release();
    if (frameSource.empty())
        frameSource = createFrameSource_Video(inputVideoName);
    // skip first frame, it is usually corrupted
        Mat frame;
        frameSource->nextFrame(frame);
        cout << "Input           : " << inputVideoName << " " << frame.size() << endl;
        cout << "Scale factor    : " << scale << endl;
        cout << "Iterations      : " << iterations << endl;
        cout << "Temporal radius : " << temporalAreaRadius << endl;
        cout << "Optical Flow    : " << optFlow << endl;
#if defined(HAVE_OPENCV_OCL)
        cout << "Mode            : " << (useCuda ? "CUDA" : useOcl? "OpenCL" : "CPU") << endl;
#else
        cout << "Mode            : " << (useCuda ? "CUDA" : "CPU") << endl;
#endif
    superRes->setInput(frameSource);
    VideoWriter writer;
    for (int i = 0;; ++i)
        cout << '[' << setw(3) << i << "] : ";
        Mat result;
#if defined(HAVE_OPENCV_OCL)
        cv::ocl::oclMat result_;
        if(useOcl)
            MEASURE_TIME(
                superRes->nextFrame(result_);
                ocl::finish();
#endif
            MEASURE_TIME(superRes->nextFrame(result));
#ifdef HAVE_OPENCV_OCL
        if(useOcl)
            if(!result_.empty())
                result_.download(result);
#endif
        if (result.empty())
            break;
        imshow("Super Resolution", result);
        if (waitKey(1000) > 0)
            break;
        if (!outputVideoName.empty())
            if (!writer.isOpened())
                writer.open(outputVideoName, CV_FOURCC('X', 'V', 'I', 'D'), 25.0, result.size());
            writer << result;
    return 0;

I've compiled them. But when running in terminal.

./SuperResolution -v /home/raiym/Downloads/bird.avi 

program gives error:

OpenCV Error: Assertion failed (vc_.isOpened()) in reset, file /home/raiym/Downloads/opencv-2.4.9/modules/superres/src/frame_source.cpp, line 161
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/raiym/Downloads/opencv-2.4.9/modules/superres/src/frame_source.cpp:161: error: (-215) vc_.isOpened() in function reset
Aborted (core dumped)

frame_source.cpp is:

/*M///////////////////////////////////////////////////////////////////////////////////////
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//                           License Agreement
//                For Open Source Computer Vision Library
// Third party copyrights are property of their respective owners.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//M*/
#include "precomp.hpp"
using namespace std;
using namespace cv;
using namespace cv::gpu;
using namespace cv::superres;
using namespace cv::superres::detail;
cv::superres::FrameSource::~FrameSource()
//////////////////////////////////////////////////////
// EmptyFrameSource
namespace
    class EmptyFrameSource : public FrameSource
    public:
        void nextFrame(OutputArray frame);
        void reset();
    void EmptyFrameSource::nextFrame(OutputArray frame)
        frame.release();
    void EmptyFrameSource::reset()
Ptr<FrameSource> cv::superres::createFrameSource_Empty()
    return new EmptyFrameSource;
//////////////////////////////////////////////////////
// VideoFrameSource & CameraFrameSource
#ifndef HAVE_OPENCV_HIGHGUI
Ptr<FrameSource> cv::superres::createFrameSource_Video(const string& fileName)
    (void) fileName;
    CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
    return Ptr<FrameSource>();
Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
    (void) deviceId;
    CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
    return Ptr<FrameSource>();
#else // HAVE_OPENCV_HIGHGUI
namespace
    class CaptureFrameSource : public FrameSource
    public:
        void nextFrame(OutputArray frame);
    protected:
        VideoCapture vc_;
    private:
        Mat frame_;
    void CaptureFrameSource::nextFrame(OutputArray _frame)
        if (_frame.kind() == _InputArray::MAT)
            vc_ >> _frame.getMatRef();
        else if(_frame.kind() == _InputArray::GPU_MAT)
            vc_ >> frame_;
            arrCopy(frame_, _frame);
        else if(_frame.kind() == _InputArray::OCL_MAT)
            vc_ >> frame_;
            if(!frame_.empty())
                arrCopy(frame_, _frame);
            //should never get here
    class VideoFrameSource : public CaptureFrameSource
    public:
        VideoFrameSource(const string& fileName);
        void reset();
    private:
        string fileName_;
    VideoFrameSource::VideoFrameSource(const string& fileName) : fileName_(fileName)
        reset();
    void VideoFrameSource::reset()
        vc_.release();
        vc_.open(fileName_);
        CV_Assert( vc_.isOpened() );
    class CameraFrameSource : public CaptureFrameSource
    public:
        CameraFrameSource(int deviceId);
        void reset();
    private:
        int deviceId_;
    CameraFrameSource::CameraFrameSource(int deviceId) : deviceId_(deviceId)
        reset();
    void CameraFrameSource::reset()
        vc_.release();
        vc_.open(deviceId_);
        CV_Assert( vc_.isOpened() );
Ptr<FrameSource> cv::superres::createFrameSource_Video(const string& fileName)
    return new VideoFrameSource(fileName);
Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
    return new CameraFrameSource(deviceId);
#endif // HAVE_OPENCV_HIGHGUI
//////////////////////////////////////////////////////
// VideoFrameSource_GPU
#if !defined(HAVE_OPENCV_GPU) || defined(DYNAMIC_CUDA_SUPPORT)
Ptr<FrameSource> cv::superres::createFrameSource_Video_GPU(const string& fileName)
    (void) fileName;
    CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
    return Ptr<FrameSource>();
#else // HAVE_OPENCV_GPU
namespace
    class VideoFrameSource_GPU : public FrameSource
    public:
        VideoFrameSource_GPU(const string& fileName);
        void nextFrame(OutputArray frame);
        void reset();
    private:
        string fileName_;
        VideoReader_GPU reader_;
        GpuMat frame_;
    VideoFrameSource_GPU::VideoFrameSource_GPU(const string& fileName) : fileName_(fileName)
        reset();
    void VideoFrameSource_GPU::nextFrame(OutputArray _frame)
        if (_frame.kind() == _InputArray::GPU_MAT)
            bool res = reader_.read(_frame.getGpuMatRef());
            if (!res)
                _frame.release();
            bool res = reader_.read(frame_);
            if (!res)
                _frame.release();
                arrCopy(frame_, _frame);
    void VideoFrameSource_GPU::reset()
        reader_.close();
        reader_.open(fileName_);
        CV_Assert( reader_.isOpened() );
Ptr<FrameSource> cv::superres::createFrameSource_Video_GPU(const string& fileName)
    return new VideoFrameSource(fileName);
#endif // HAVE_OPENCV_GPU

And this code is where error found in frame_source.cpp (line 161):

void VideoFrameSource::reset()
    vc_.release();
    vc_.open(fileName_);
    CV_Assert( vc_.isOpened() );

Can anybody help me?

Looks like the input video you specify in the arguments does not exist. But to know for sure you should check the code of frame_source.cpp and see what the assertion is about... – diip_thomas Jul 29, 2014 at 11:28 @diip_thomas ./SuperResolution -v /path/to/video.3gp that is what I write in terminal. I've edited my question – raiym Jul 29, 2014 at 11:31

It could be that you have a codec problem. The assertion checks if the source can be opened or not. When it fails it gives you the error you describe. It can fail because the source does not exist or because the it is unable to open the file format or read the codec. You can also try another .avi video to see if that does work. If it does, you can be pretty sure it is a codec problem.

See this SO post for some ways to check the codecs: Why can't I open avi video in openCV?

You can check the codec of the file by opening a Linux terminal and typing:

ffmpeg -i video.3gp

Then you can see which libs are installed and what the video encoding is. For instance:

Stream: #0.0: Video mjpeg (MJPG / 0x47504A4D), yuvj422p, 1280x720, 30.02 tbr

Shows that both FFMPEG is working correctly (otherwise you'd get an error message) and that the file is MJPG encoded.

Another option is that your openCV installation did not build with FFMPEG. You can check that by running cmake and check if cmake is detected. It should look like:

-- FFMPEG: YES

Apparently sometimes your FFMPEG can be installed and detected, but not configured properly. This SO post shows a solution: VideoCapture is not working in OpenCV 2.4.2

Yes, I ve checked it when you first answer this question. I followed link. ffmpeg works well – raiym Jul 29, 2014 at 12:18

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.