Using on-board TX2 camera with OpenCV 3.2

I have recently installed and built OpenCV 3.2 to be able to use the camera module on-board the TX2, but am experiencing problems. Are there any Gstreamer settings/configurations that need to be set before using with OpenCV? Here is the code:

#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/core/core.hpp>
#include <stdlib.h>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{

const char* gst =  "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)I420, framerate=(fraction)120/1 ! \
			nvvidconv flip-method=6 ! video/x-raw, format=(string)BGRx ! \
			videoconvert ! video/x-raw, format=(string)BGR ! \
			appsink";

    cv::VideoCapture cap(gst);
if(!cap.isOpened()) { // check if we succeeded
cout << "Failed to open camera " << endl;
return -1;
}
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("original", frame);
waitKey(1);
}
// the camera will be deinitialized automatically in VideoCapture destructor
cap.release();

return 0;

}
g++ -std=c++11 face_test_cpu.cpp -I/usr/include -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect -lopencv_videoio -lopencv_video -o face_test

 /src/Face Detection$ ./face_test 
Failed to open camera

Have you enabled gstreamer-1.0 support when configuring opencv-3.2.0 ? This is mandatory.

Furthermore, where have you installed it (according to CMAKE_INSTALL_PREFIX) ?

Your build command assumes opencv in /usr. If you installed opencv-3.2.0 in /usr, had you removed opencv4tegra before ?

If you’re not sure, post the output of:

ls /usr/lib/libopencv*

I have not enabled gstreamer-1.0 when configuring, how can I do this?

I removed opencv4tegra before, and had OpenCV 3.2 installed in /usr.

The output of the command above gives all of the new OpenCV libraries installed.

Ahh I see, you have to switch -DWITH_GSTREAMER=OFF to ON.
What about DWITH_GSTREAMER_0_10=OFF, does this have to be switched ON as well?

You just need to activate gstreamer-1.0 support.
If you don’t need gstreamer-0.10 support, you can keep it off.

I have enabled gstreamer-1.0 support, but I am still receiving problems. Are there any other settings that need to be enabled?

#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/core/core.hpp>
#include <stdlib.h>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{

const char* src = "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)I420, framerate=(fraction)60/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";


VideoCapture cap;
cap.open(src);

if(!cap.isOpened()) { // check if we succeeded
cout << "Failed to open camera " << endl;
return -1;
}
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("original", frame);
waitKey(1);
}
// the camera will be deinitialized automatically in VideoCapture destructor
cap.release();

return 0;

g++ -std=c++11 face_test_cpu.cpp -I/usr/include -lopencv_core -lopencv_videoio -lopencv_highgui -lopencv_imgproc -lopencv_objdetect -o face_test


}

Is there a gstreamer library I should be linking during compilation and header files that need to be included?

You may install gstreamer dev packages before configuring and building opencv:

sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

Apart from that, if you can configure, build and install opencv-3.2.0 successfully, you should not need more link options for your application.
If gst-launch-1.0 works, then gstreamer libs are available.

It is unclear if you face a problem and if yes what is it, so you may post your commands and the error messages it generates.