# Camera doesn't work with opencv 

**URL:** <https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958>\
**Category:** Jetson TK1\
**Created:** [February 7, 2018, 10:34am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958 "2018-02-07T10:34:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pm\_brk\_pm](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pm\_brk\_pm](https://forums.developer.nvidia.com/u/pm_brk_pm)\
**Post date:** [February 7, 2018, 10:34am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/1 "2018-02-07T10:34:35Z")

</div>

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 ;

```auto
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

```

```auto
-I/usr/include/opencv

```

Also I checked this topic : [https://devtalk.nvidia.com/default/topic/943129/jetson-tx1/highgui-error-v4l-v4l2-while-opening-camera-with-opencv4tegra-l4t-r24/](https://devtalk.nvidia.com/default/topic/943129/jetson-tx1/highgui-error-v4l-v4l2-while-opening-camera-with-opencv4tegra-l4t-r24/)

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

```auto
#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 downloaded Gtk2.0 and gst1.0

```auto
ii libgtk2.0-0:ar 2.24.23-0ubu armhf GTK+ graphical user interface lib

```

```auto
gir1.2-gstreamer-1.0 1.2.4-0ubuntu1

```

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

```auto
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

```auto
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.

```auto
./binary

```

**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:

```auto
HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP

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

```

The version of the open cv is : 2.4.13

Any help will be apreciated

---

<div class="post-metadata">

**Author:** ![WayneWWW](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/waynewww/32/14043_2.png) [@WayneWWW](https://forums.developer.nvidia.com/u/WayneWWW)\
**Post date:** [February 8, 2018, 2:07am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/2 "2018-02-08T02:07:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pm\_brk\_pm](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pm\_brk\_pm](https://forums.developer.nvidia.com/u/pm_brk_pm)\
**Post date:** [February 9, 2018, 6:20am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/3 "2018-02-09T06:20:02Z")

</div>

Hi Wayne I used this link according to your suggestion.

[https://devtalk.nvidia.com/default/topic/928345/jetson-tk1/opencv-videocapture-and-hardware-accelerated-video-encoding-support/](https://devtalk.nvidia.com/default/topic/928345/jetson-tk1/opencv-videocapture-and-hardware-accelerated-video-encoding-support/)

```auto
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 :

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![WayneWWW](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/waynewww/32/14043_2.png) [@WayneWWW](https://forums.developer.nvidia.com/u/WayneWWW)\
**Post date:** [February 9, 2018, 7:41am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/4 "2018-02-09T07:41:15Z")

</div>

pm\_brk\_pm,

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?

---

<div class="post-metadata">

**Author:** ![pm\_brk\_pm](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pm\_brk\_pm](https://forums.developer.nvidia.com/u/pm_brk_pm)\
**Post date:** [February 9, 2018, 9:03am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/5 "2018-02-09T09:03:22Z")

</div>

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 : [url][OpenCV VideoCapture and hardware-accelerated video encoding support - Jetson TK1 - NVIDIA Developer Forums](https://devtalk.nvidia.com/default/topic/928345/jetson-tk1/opencv-videocapture-and-hardware-accelerated-video-encoding-support/%5B/url%5D)

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

---

<div class="post-metadata">

**Author:** ![WayneWWW](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/waynewww/32/14043_2.png) [@WayneWWW](https://forums.developer.nvidia.com/u/WayneWWW)\
**Post date:** [February 9, 2018, 9:20am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/6 "2018-02-09T09:20:48Z")

</div>

pm\_brk\_pm,

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.

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

```

---

<div class="post-metadata">

**Author:** ![pm\_brk\_pm](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pm\_brk\_pm](https://forums.developer.nvidia.com/u/pm_brk_pm)\
**Post date:** [February 9, 2018, 10:09am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/7 "2018-02-09T10:09:05Z")

</div>

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.

```auto
/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:

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

```

---

<div class="post-metadata">

**Author:** ![WayneWWW](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/waynewww/32/14043_2.png) [@WayneWWW](https://forums.developer.nvidia.com/u/WayneWWW)\
**Post date:** [February 12, 2018, 2:16am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/8 "2018-02-12T02:16:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pm\_brk\_pm](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pm\_brk\_pm](https://forums.developer.nvidia.com/u/pm_brk_pm)\
**Post date:** [February 12, 2018, 6:21am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/9 "2018-02-12T06:21:55Z")

</div>

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

But this command unfortunately does not work;

```auto
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.

[https://devtalk.nvidia.com/default/topic/928345/jetson-tk1/opencv-videocapture-and-hardware-accelerated-video-encoding-support/](https://devtalk.nvidia.com/default/topic/928345/jetson-tk1/opencv-videocapture-and-hardware-accelerated-video-encoding-support/)

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

---

<div class="post-metadata">

**Author:** ![pm\_brk\_pm](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pm\_brk\_pm](https://forums.developer.nvidia.com/u/pm_brk_pm)\
**Post date:** [February 12, 2018, 9:36am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/10 "2018-02-12T09:36:17Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![WayneWWW](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/waynewww/32/14043_2.png) [@WayneWWW](https://forums.developer.nvidia.com/u/WayneWWW)\
**Post date:** [February 12, 2018, 10:17am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/11 "2018-02-12T10:17:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pm\_brk\_pm](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pm\_brk\_pm](https://forums.developer.nvidia.com/u/pm_brk_pm)\
**Post date:** [February 12, 2018, 10:42am UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/12 "2018-02-12T10:42:27Z")

</div>

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;

```auto
#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

    namedWindow("Preview",1);
    Mat frame;
    while(true)
    {
        cap >> frame; // get a new frame from camera
       // if(frame.size()>0){
        if(!frame.empty()){
        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.

---

<div class="post-metadata">

**Author:** ![TomNVIDIA](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/tomnvidia/32/14181_2.png) [@TomNVIDIA](https://forums.developer.nvidia.com/u/TomNVIDIA)\
**Post date:** [October 18, 2021, 6:44pm UTC](https://forums.developer.nvidia.com/t/camera-doesnt-work-with-opencv/57958/13 "2021-10-18T18:44:54Z")

</div>

Closed on October 18, 2021, 6:44pm UTC


