video capture problem

asked 2019-01-18 07:25:47 -0600

kundo gravatar image

updated 2019-01-20 20:51:06 -0600

Hi,

I built opencv with openni2 using Cmake, and I succeeded to run the example 'openni_capture' which is in OpenCV.sln. It clearly shows the video being captured.

But when I try to make my own project, copy and paste the code, and run it, it says 'can not open a capture object' even if it was successfully built.

The code is like below. The problem is that 'capture.isOpened()' is TRUE in the example project, but it is FALSE in my own project which has exactly same code as the example project.

#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

static void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f, float S=1.f, float V=1.f )
{
.
.
.
}

static float getMaxDisparity( VideoCapture& capture )
{
.
.
.
}

static void printCommandLineParams()
{
    cout << "-cd=       Colorized disparity? (0 or 1; 1 by default) Ignored if disparity map is not selected to show." << endl;
    cout << "-fmd=      Fixed max disparity? (0 or 1; 0 by default) Ignored if disparity map is not colorized (-cd 0)." << endl;
    cout << "-mode=     image mode: resolution and fps, supported three values:  0 - CAP_OPENNI_VGA_30HZ, 1 - CAP_OPENNI_SXGA_15HZ," << endl;
    cout << "          2 - CAP_OPENNI_SXGA_30HZ (0 by default). Ignored if rgb image or gray image are not selected to show." << endl;
    cout << "-m=        Mask to set which output images are need. It is a string of size 5. Each element of this is '0' or '1' and" << endl;
    cout << "          determine: is depth map, disparity map, valid pixels mask, rgb image, gray image need or not (correspondently), ir image" << endl ;
    cout << "          By default -m=010100 i.e. disparity map and rgb image will be shown." << endl ;
    cout << "-r=        Filename of .oni video file. The data will grabbed from it." << endl ;
}

static void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFixedMaxDisp, int& imageMode, bool retrievedImageFlags[],
                       string& filename, bool& isFileReading )
{
    filename.clear();
    cv::CommandLineParser parser(argc, argv, "{h help||}{cd|0|}{fmd|0|}{mode|-1|}{m|000100|}{r||}");
    if (parser.has("h"))
    {
        help();
        printCommandLineParams();
        exit(0);
    }
    isColorizeDisp = (parser.get<int>("cd") != 0);
    isFixedMaxDisp = (parser.get<int>("fmd") != 0);
    imageMode = parser.get<int>("mode");
    int flags = parser.get<int>("m");
    isFileReading = parser.has("r");
    if (isFileReading)
        filename = parser.get<string>("r");
    if (!parser.check())
    {
        parser.printErrors();
        help();
        exit(-1);
    }
    if (flags % 1000000 == 0)
    {
        cout << "No one output image is selected." << endl;
        exit(0);
    }
    for (int i = 0; i < 6; i++)
    {
        retrievedImageFlags[5 - i] = (flags % 10 != 0);
        flags /= 10;
    }
}

int main( int argc, char* argv[] )
{
    bool isColorizeDisp, isFixedMaxDisp;
    int imageMode;
    bool retrievedImageFlags[6];
    string filename;
    bool isVideoReading;
    parseCommandLine( argc, argv, isColorizeDisp, isFixedMaxDisp, imageMode, retrievedImageFlags, filename, isVideoReading );

    cout << "Device opening ..." << endl;
    VideoCapture capture;
    if( isVideoReading )
        capture.open( filename );
    else
    {
        capture.open( CAP_OPENNI2 );
        if (!capture.isOpened())
        {
            capture.open(CAP_OPENNI);
        }
    }

    cout << "done." << endl;

    if( !capture.isOpened() )
    {
        cout << "Can not open a capture object." << endl;
        return -1;
    }
.
.
.

I added to VC++ directory-include ... (more)

edit retag flag offensive close merge delete

Comments

...and what's the not working? Do you have an error message?

Posting your code would also help.

kbarni gravatar imagekbarni ( 2019-01-18 17:48:55 -0600 )edit

Hi, sorry for the unclear question. There is no error message. It was successfully built. I used the exactly same code as the example provided by OpenCV. The problem is, I think, capture.isOpened(). It is TRUE when I run the example project so it succeeds to show the video, but it is FALSE in my own project. Maybe I should change some settings of my project..?

kundo gravatar imagekundo ( 2019-01-20 05:58:40 -0600 )edit

@kundo. You copied and pasted in another folder, but you didn't move filename in your currently folder. Did you?

supra56 gravatar imagesupra56 ( 2019-01-20 07:05:12 -0600 )edit

cross check the name of your input video i think

Allaye gravatar imageAllaye ( 2019-01-20 09:46:51 -0600 )edit

@supra56 What do you mean moving filename in my currently folder? Should I add file names in somewhere?

kundo gravatar imagekundo ( 2019-01-20 19:31:53 -0600 )edit

@supra56@Allaye If you mean 'filename' in capture.open(filename), I think this is for playing a video file. I'm trying to stream my camera in real time.

kundo gravatar imagekundo ( 2019-01-20 20:42:37 -0600 )edit

@kundo. Is your filename in your currently folder? I am not talking about open filename.

supra56 gravatar imagesupra56 ( 2019-01-21 07:42:41 -0600 )edit