//Build using g++ opencv.cpp `pkg-config --cflags --libs opencv`
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened())
return -1;
namedWindow("Preview",1);
Mat frame;
for(;;)
{
cap >> frame; // get a new frame from camera
imshow("Preview", frame);
if(waitKey(2) >= 0) break;
}
return 0;
}
I tried reading various other posts here but couldn’t get it to work.
What could be the problem here?
I’m not an OpenCV developer, but you could compile with debug symbols (-g) and run it in gdb…when it fails get a stack frame with backtrace command (bt). Then “l” to list the lines of code surrounding the seg fault. You may want to manually find out what the output is from the command “pkg-config --cflags --libs opencv” and substitute this directly for the backquoted “pkg-config --cflags --libs opencv”…if there is any optimizing (like -O#), remove that, add the “-g”.
We did the test with OpenCV 3.1.0 with onboard camera, we can see it work successfully.
Here are some tips to open the default camera
Please try openCV version be 3.0.0 or higher. we’re using 3.1.0
GST 1.0 and related plugins must be installed.
test program must be linked against built openCV 3.1.0 libs
test code looks like:
VideoCapture cap("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)I420, framerate=(fraction)24/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"); //open the default camera
if(!cap.isOpened()) { // check if we succeeded
cerr << "Fail 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();
Regarding OpenCV4Tegra, it’s not well optimized for onboard camera due to pixel format issue, but USB webcam can work.
@kayccc
hi kayccc,
as u said above, since there’s no articles about how to install opencv3.1 on TX1 board , could u please tell us how to install opencv3.1 (can use NVIDIA’s CPU optimizations )
After built your own version of OpenCV (using the public open-source code), you will still have the full CUDA-optimized “gpu” module but not the CPU optimizations of OpenCV4Tegra.
Regarding your OpenCV 3.1.0 install issue, that might be the known issue, OpenCV compiling frequently crash in R24.1 64-bit environment. Please repeat compiling till it’s done, or to wait for next release.
About the GST issue, please make sure that GST is installed correctly. Run a simple nvcamerasrc pipeline to make sure that on-board camera runs well.
For on-board camera, please paste the test code, and the crash log with GST debug information.
I have a camera pluged in TX1,how can I get the camera worked successfully with opencv3.1 (not the default camera)?could you please show me the codes then?
thank you very much!
Thanks kayccc, you were right, it was that same install issue. After about 100 times it finally made it through (the last one requiring a sudo). I wonder if it could have been caused by me already having the default OpenCV 2.4.9 in there?
I didn’t format my post well on the GST issue, that part was actually working fine and I’ve now gotten the code snippet to work with OpenCV.
I’ve seen and tested that code, but due to different types of cam, the code there seems no help. My camera is Logitech, a USB camera, it’s not the camera (0-1) in opencv ,so I want to know how to make the camera work well , I’ll appreciate it if you show us the code about how to make the Logitech camera work successfully.
thx very much!
USB webcam on Jetson TX1/TX2 could be accessed via OpenCV’s GStreamer-pipeline-based VideoCapture(). I shared my code in the blog post below. You’d have to build OpenCV 3.x with gstreamer supported enabled to get it to work though.