Segmentation fault motion detection

Hi,
I am trying to execute the motion detection algorithm on the Jetson TK1 but I get this error : segmentation fault
Any help will be very appreciated

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/opencv.hpp"


#include <opencv2/video.hpp>

#include <iostream>
#include <sstream>

#include <opencv2/core/core.hpp>
#include "opencv2/gpu/gpu.hpp"

#include </usr/include/opencv2/video/background_segm.hpp>
#include <stdio.h>
#include <string>


//using namespace cv;
using namespace std;

int main()
{

cv::VideoCapture input("/home/ubuntu/MyExamples/MD0/highway.avi");
cv::Mat img, img_prev0, img_prev, frame, mask, thresh, gray_img, out_frame;

cv::Ptr<cv::BackgroundSubtractorMOG2> bgsubtractor;
cv::BackgroundSubtractorMOG2( 10,  2.5,true);

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
                bgsubtractor->operator()(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;
			}
		}
}

If compiled with debug symbols you might want to just run this under gdb and get a stack frame to see where it faulted.

If you have no arguments to pass to the program:

gdb /where/ever/it/is/program
r
...faults in here somewhere...
bt
l

(bt gives the “backtrace” or stack frame, “l” lists the current code)

If you have arguments to pass start this way:

gdb /where/ever/it/is/program
set args -what -ever -args -it has
...then "r" and same as above...

Hi,

Could you narrow down the error is caused by which line first?
Thanks.

Hi All,
Thanks for your reply
The code compiles correctly without errors I use cmake … make in the terminal as in YouTube opencv for Jetson tutorials
When I try to execute it this message appears ‘Segmentation fault’ with no more information
I think there is problem in calling the Mog algorithm in opencv4tegra, something missing in my code perhaps parameters of this method
Any help thanks in advance

PS:I will try If I find it I will let you now

Hi,
when I ran the gdb ‘r’ I got 'Program received signal SIGSEGV, segmentation fault. 0x0000994a in main()
without further information any suggestion to solve this.
Aftre reading many threads on forums I think it has a connection with the pointers
Thanks in advance

For debugging with gdb, you have to recompile with flag -ggdb so that debug symbols are included.

Furthermore, after it has shown the segfault, as suggested by @linuxdev you may launch bt (backtrace) it will show the call stack and you should be able to see where it faults.

You may also check that your videoCapture is opened with input.isOpened().

After it seg faults with “r” you then have to find the stack frame of where it was at that moment (and what @Honey_Patouceul said will give you far more information). That is why after it dies you run “bt” (backtrace…a stack frame)…then “l” to list the known code lines surrounding that failure.

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;
    	}
   }
}

Thank you all for your help
I found that this fault is generated when using pointers. like Mr. Honey_Patouceul said I mixed Opencv 2.4 with version 3, thanks a lot for your last edit, I need a lot this tip.
Again thank you all for your help