I want to call the cv::VideoCapture
constructor conditionally. If argc == 2
, then the user has specified a video file and I want to load that. Otherwise use the default camera.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char* argv[])
{
VideoCapture cap(argc == 2 ? argv[1] : 0);
return 0;
}
I compile and run it
$ g++ -g -Wall -o main main.cpp `pkg-config opencv --cflags --libs`
$ ./main
But sadly it doesn't seem to work and I get
OpenCV: Couldn't read movie file ""
The ternary operator returns a string in one case and an integer in another. I reckon this won't work because of some idiosyncrasy of C++ constructors I'm not aware of.
What's the cleanest way to do what I want? Can I do it without using new
so that I don't have to free memory myself at the end of the program?