int ret = 0;
int opt;
- std::string *input_file = NULL;
- std::string *output_file = NULL;
+ std::string input_file;
+ std::string output_file;
int trail_lenght = 25;
- std::string *segmentation_method = new std::string("background");
+ std::string segmentation_method("background");
int background_learn_frames = 50;
int threshold_level = 5;
- std::string *drawing_method = new std::string("copy");
+ std::string drawing_method("copy");
bool reverse_trail = false;
bool show_background = false;
bool redraw_current_frame = false;
while ((opt = getopt(argc, argv, "i:o:l:s:b:t:d:rBFh")) != -1) {
switch (opt) {
case 'i':
- input_file = new std::string(optarg);
+ input_file = std::string(optarg);
break;
case 'o':
- output_file = new std::string(optarg);
+ output_file = std::string(optarg);
break;
case 'l':
trail_lenght = atoi(optarg);
break;
case 's':
- delete segmentation_method;
- segmentation_method = new std::string(optarg);
+ segmentation_method = std::string(optarg);
break;
case 'b':
background_learn_frames = atoi(optarg);
threshold_level = atoi(optarg);
break;
case 'd':
- delete drawing_method;
- drawing_method = new std::string(optarg);
+ drawing_method = std::string(optarg);
break;
case 'r':
reverse_trail = true;
cv::Size frame_size;
cv::Mat input_frame;
- if (input_file) {
- inputVideo.open(*input_file);
+ if (!input_file.empty()) {
+ inputVideo.open(input_file);
} else {
+#ifdef __linux__
+ // XXX Hardcode the V4L2 backend on linux for now because the
+ // GStreamer one lacks some functionality, see:
+ // https://github.com/opencv/opencv/issues/18562
+ inputVideo.open(0, cv::CAP_V4L2);
+#else
inputVideo.open(0);
+#endif
}
if (!inputVideo.isOpened()) {
goto out;
}
- frame_size = cv::Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),
- (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));
+ frame_size = cv::Size((int) inputVideo.get(cv::CAP_PROP_FRAME_WIDTH),
+ (int) inputVideo.get(cv::CAP_PROP_FRAME_HEIGHT));
- if (output_file) {
- int fps = inputVideo.get(CV_CAP_PROP_FPS);
+ if (!output_file.empty()) {
+ int fps = inputVideo.get(cv::CAP_PROP_FPS);
if (fps < 0)
fps = 25;
- outputVideo.open(*output_file, CV_FOURCC('M','J','P','G'), fps, frame_size, true);
+ outputVideo.open(output_file, cv::VideoWriter::fourcc('M','J','P','G'), fps, frame_size, true);
if (!outputVideo.isOpened()) {
std::cerr << "Could not open the output video for write." << std::endl;
ret = -1;
trail->setRedrawCurrentFrame(redraw_current_frame);
- if (trail->setDrawingMethod(*drawing_method) < 0) {
+ if (trail->setDrawingMethod(drawing_method) < 0) {
std::cerr << "Invalid drawing method." << std::endl;
ret = -1;
goto out_delete_trail;
}
Segmentation *segmentation;
- if (*segmentation_method == "background") {
+ if (segmentation_method == "background") {
segmentation = new MOG2Segmentation(inputVideo, background_learn_frames);
if (show_background) {
- cv::Mat background;
+ cv::Mat background(frame_size, inputVideo.get(cv::CAP_PROP_FORMAT));
((MOG2Segmentation *)segmentation)->getBackgroundImage(background);
trail->setBackground(background);
}
- } else if (*segmentation_method == "threshold") {
+ } else if (segmentation_method == "threshold") {
segmentation = new ThresholdSegmentation(threshold_level);
- } else if (*segmentation_method == "none") {
+ } else if (segmentation_method == "none") {
segmentation = new DummySegmentation();
} else {
std::cerr << "Invalid segmentation method." << std::endl;
goto out_delete_trail;
}
- cv::namedWindow("Frame", CV_WINDOW_AUTOSIZE);
+ cv::namedWindow("Frame", cv::WINDOW_NORMAL);
+ cv::resizeWindow("Frame", frame_size);
+ cv::setWindowTitle("Frame", "OpenCV Trail Effect");
for (;;) {
inputVideo >> input_frame;
trail->draw(canvas);
cv::imshow("Frame", canvas);
- if (cv::waitKey(30) >= 0)
+ if (cv::waitKeyEx(30) >= 0)
break;
if (outputVideo.isOpened())
out_delete_trail:
delete trail;
out:
- delete drawing_method;
- delete segmentation_method;
- delete output_file;
- delete input_file;
return ret;
}