Furthermore you may be mixing opencv2 and opencv3 code.
If you are using opencv4tegra-2.4.13, this code may work:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/video/background_segm.hpp>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
//using namespace cv;
using namespace std;
int main()
{
cv::VideoCapture input("/home/nvidia/Videos/sample.avi");
if (!input.isOpened()) {
fprintf(stderr, "Failed to open input capture.\n");
exit(-1);
}
cv::Mat img, img_prev0, img_prev, frame, mask, thresh, gray_img, out_frame;
cv::BackgroundSubtractorMOG2 mog2(10, 2.5,true);
//cv::Ptr<cv::BackgroundSubtractorMOG2> bgsubtractor; //This is opencv3
input.read(img);
img.copyTo(img_prev0);
//convert to grayscale and set the first frame
cv::cvtColor(img_prev0, img_prev, CV_BGR2GRAY);
// Apply Gaussian blur filter
cv::GaussianBlur(img_prev, img_prev, cv::Size(7, 7), 0);
cv::VideoWriter output("highwayMD.avi",CV_FOURCC('X','V','I','D'),30,cv::Size(input.get(CV_CAP_PROP_FRAME_WIDTH),input.get(CV_CAP_PROP_FRAME_HEIGHT)),0);
while(input.read(frame))
{
//convert to grayscale
cv::cvtColor(frame, gray_img, CV_BGR2GRAY);
cv::GaussianBlur(gray_img, gray_img, cv::Size(7, 7), 0);
//mog2
mog2(gray_img, mask,-1);
cv::threshold(mask, thresh, 25, 255, cv::THRESH_BINARY);
cv::dilate(thresh, thresh, 0); // 0 is the rectangle structural element
output.write(thresh);
cv::putText(mask, "Motion Detected", cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.75, cv::Scalar(255,255,255),2);
cv::imshow("Camera", mask);
if(cv::waitKey(1) == 27)
{
//exit if ESC is pressed
break;
}
}
}
[EDIT: also check with
ldd <your_app> | grep opencv
that you don’t link against opencv3 libs (such as libopencv_videoio).
ldd should only show opencv-2.4 libs.]
[EDIT2: This code seems to be working (although videoWriter output looks weird so it is commented) with opencv-3.4.0 (gstreamer enabled) with TX2 devkit onboard camera, although detection parameters may be adjusted dependingon scene exposition:
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/video/background_segm.hpp>
#include "opencv2/videoio.hpp"
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
const char* gst = "nvcamerasrc ! video/x-raw(memory:NVMM), format=(string)I420, width=(int)640, height=(int)480, framerate=(fraction)30/1 ! \
nvvidconv ! video/x-raw, format=(string)BGRx ! \
videoconvert ! video/x-raw, format=(string)BGR ! \
appsink";
cv::VideoCapture input(gst);
//cv::VideoCapture input("/home/nvidia/Videos/sample.avi");
if (!input.isOpened()) {
fprintf(stderr, "Failed to open input capture.\n");
exit(-1);
}
cv::Mat img, img_prev0, img_prev, frame, mask, thresh, gray_img, out_frame;
//cv::BackgroundSubtractorMOG2 mog2(10, 2.5, true); // opencv2
cv::Ptr<cv::BackgroundSubtractorMOG2> pMog2; // opencv3
pMog2 = cv::createBackgroundSubtractorMOG2(10, 2.5, true); //MOG2 approach (opencv3)
input.read(img);
img.copyTo(img_prev0);
//convert to grayscale and set the first frame
cv::cvtColor(img_prev0, img_prev, CV_BGR2GRAY);
// Apply Gaussian blur filter
cv::GaussianBlur(img_prev, img_prev, cv::Size(7, 7), 0);
//cv::VideoWriter output("highwayMD.avi",CV_FOURCC('X','V','I','D'),30,cv::Size(input.get(CV_CAP_PROP_FRAME_WIDTH),input.get(CV_CAP_PROP_FRAME_HEIGHT)),0);
while(input.read(frame))
{
//convert to grayscale
cv::cvtColor(frame, gray_img, CV_BGR2GRAY);
cv::GaussianBlur(gray_img, gray_img, cv::Size(7, 7), 0);
//mog2
pMog2->apply(gray_img, mask);
cv::threshold(mask, thresh, 25, 255, cv::THRESH_BINARY);
cv::dilate(thresh, thresh, 0); // 0 is the rectangle structural element
//output.write(thresh);
cv::putText(mask, "Motion Detected", cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.75, cv::Scalar(255,255,255),2);
cv::imshow("Camera", mask);
if(cv::waitKey(1) == 27)
{
//exit if ESC is pressed
break;
}
}
}