Camera doesn't work with opencv

Hello everyone,

I would like to inform you that I am newbie to Tegra K1 development kit.
Nowadays I just would like to use opencv and write image processing code with Logitech c310 model web cam.I connected it directly to usb which is one available on tegra k1.

I sucessfully installed Cuda and opencv4tegra with jetpack.

I checked the version of them and respectively the outputs are ;

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2014 NVIDIA Corporation
Built on Tue_Feb_17_22:53:16_CST_2015
Cuda compilation tools, release 6.5, V6.5.45

Also I checked this topic :

According to this topic I tried sample program in c++ like this ;

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
    VideoCapture cap(0); // open the default camera
        return -1;

    Mat frame;
        cap >> frame; // get a new frame from camera
        imshow("Preview", frame);
        if(waitKey(2) >= 0) break;
    return 0;

I downloaded Gtk2.0 and gst1.0

ii  libgtk2.0-0:ar 2.24.23-0ubu armhf        GTK+ graphical user interface lib
gir1.2-gstreamer-1.0  1.2.4-0ubuntu1

Also I installed V4L drivers.After all, I run above code with this command;

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary  main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching

The I tried to run the code for logitech web cam with

Videocapture ("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")

But it did not work either.


The program did not give any errors, but entered the program and immediately closed.It didn’t show any image stream.

Also Videocapture(0) gives this error:


(Preview:7785): Gtk-WARNING **: cannot open display:

The version of the open cv is : 2.4.13

Any help will be apreciated

Two concerns here:

  1. nvcamerasrc cannot use USB camera. Please try v4l2src with /dev/videoX. X is where your camera mounted.
  2. Please “expoer DISPLAY=:0” for X11 identifying your monitor.

Hi Wayne I used this link according to your suggestion.

v4l2src device=/dev/video0 ! video/x-raw-yuv,width=1920,height=1080,framerate=30/1,format=(fourcc)I420 ! ffmpegcolorspace ! appsin

and I built it with :

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary  main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching

Program does not show any error but immediately close itself after running.

My monitor is connected on hdmi port.I didn’t get what does this command mean “expoer DISPLAY=:0”

The program always says

GT-Warning : You can not open display : 0.0

Also I would like to inform you that I connected usb2.0 camera to usb 3.0 port of nvdia.


Please use gstreamer pipeline to confirm that your camera is working. I meant use “gst-launch-1.0” instead of running opencv program.

“export DISPLAY=:0” is to set your display window to X11. As a result, please use “xrandr” after export that variable. If there is nothing shown, which means there would be problem if you want to use imshow()

May I ask one more question, are you using console interface(command line) with your monitor?

Yes, I am using console interface with monitor which is connected to Hdmi port of Tegra kit, I didn’t install eclipse or any other IDE yet.I basically created main.cpp file and run it with opencv libraries.

I checked webcam with other image processing project in my computer(Windows, VS 2015) and it works well.But I would like to integrate it to Nvdia Tegra k1 embedded system and also use Cuda for better performance.

Because we are planning to develope Nvdia based embedded control card to use it in industry.So, I just would like to run image processing program in Tegra k1.

I applied export DISPLAY=:0 and xrandr command but the situation is not changed.İt throws 1you can not open display" if I erase “if(!cap.isOpened()) return -1;” commands.

Also I am wondering how he was able to use simply “VideoCapture cap(0)” with webcam here :

if I can only open the camera i can do the rest


I meant do you have gnome desktop GUI on your monitor or the console only.

If you have only the console (command line) mode on your monitor, which means X11 is not working so that imshow() in your code cannot work. And this indicates the issue is not due to the camera but the display.

Please try to use below and run your program again if you have only console.

sudo xinit &
export DISPLAY=:0
./your program

I applied these commands and related problem is gone.But the error message is changed, it always turns empty image with “imshow” and shows empty image on monitor which is connected to tegra.Camera lights were off during process.

/hdd/buildbot/slave_jetson_tk1_1/32-O4T-L4T/opencv/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow

I use this command in Vide capture:

v4l2src device=/dev/video0 ! video/x-raw-yuv,width=1920,height=1080,framerate=30/1,format=(fourcc)I420 ! ffmpegcolorspace ! appsink

Could you first check the size of this frame before send it to preview?

Wayne, thank you so much.I am able to open taht camera with “Videocapture(0)” command.

But this command unfortunately does not work;

v4l2src device=/dev/video0 ! video/x-raw-yuv,width=640,height=480,framerate=30/1,format=(fourcc)I420 ! ffmpegcolorspace ! appsink

The preview was “640*480”.

Now ı will implement my code and check the performance of given stream commands like here.

May I keep this topic here for asking questions and feedback for trials? or should i open another topics for each process.

I just tried this command and it does not work too.

v4l2src device=\"/dev/video0\" ! video/x-raw-rgb,width=1920,height=1080,framerate=30/1,format=(fourcc)RGB ! appsink

Do you know the reason of it.

I just found it is awkward and really sorry… Are you using opencv4tegra? It does not support gstreamer pipeline.

You need to use openCV 3.x and enable GST when build it from source. I am so sorry for didn’t notice it in the beginning.

How do you run the pipeline in last comment?

I was able to run camera preview with “Videocapture(0)” command.This one is working properly with Opencv4tegra(2.4.13).

I was using other pipelines like this;

#include "opencv2/opencv.hpp"
#include <iostream>
#include <string.h>
using namespace cv;
using namespace std;

int main(int, char**)

string gst = "v4l2src device=\"/dev/video0\" ! video/x-raw-rgb,width=1920,height=1080,framerate=30/1,format=(fourcc)RGB ! appsink";

    VideoCapture cap(gst); // open the default camera

    Mat frame;
        cap >> frame; // get a new frame from camera
       // if(frame.size()>0){
        imshow("Preview", frame);}
        if(waitKey(1) >= 0) break;
    return 0;

So I am going to try 3.x versions with gst today and tomorrow .

I am going to inform here if it works or not.

Thank you so much, I really apreciated for your help.