USB Camera problem


I tried to run OpenCV sample code to test the USB webcam I plugged to the board, but it failed. The error I got:
Unable to stop the stream: Device or resource busy

The sample I used was Laplace.cpp

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <ctype.h>
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

static void help()
    cout <<
            "\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n"
            "It captures from the camera of your choice: 0, 1, ... default 0\n"
            "./laplace [camera #, default 0]\n" << endl;

int sigma = 3;
int smoothType = CV_GAUSSIAN;

int main( int argc, char** argv )
    VideoCapture cap;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) == 2 ? argv[1][0] - '0' : 0);
    else if( argc >= 2 )
        if( cap.isOpened() )
            cout << "Video " << argv[1] <<
                ": width=" << cap.get(CV_CAP_PROP_FRAME_WIDTH) <<
                ", height=" << cap.get(CV_CAP_PROP_FRAME_HEIGHT) <<
                ", nframes=" << cap.get(CV_CAP_PROP_FRAME_COUNT) << endl;
        if( argc > 2 && isdigit(argv[2][0]) )
            int pos;
            sscanf(argv[2], "%d", &pos);
            cout << "seeking to frame #" << pos << endl;
            cap.set(CV_CAP_PROP_POS_FRAMES, pos);

    if( !cap.isOpened() )
        cout << "Could not initialize capturing...\n";
        return -1;

    namedWindow( "Laplacian", 0 );
    createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );

    Mat smoothed, laplace, result;

        Mat frame;
        cap >> frame;
        if( frame.empty() )

        int ksize = (sigma*5)|1;
        if(smoothType == CV_GAUSSIAN)
            GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
        else if(smoothType == CV_BLUR)
            blur(frame, smoothed, Size(ksize, ksize));
            medianBlur(frame, smoothed, ksize);

        Laplacian(smoothed, laplace, CV_16S, 5);
        convertScaleAbs(laplace, result, (sigma+1)*0.25);
        imshow("Laplacian", result);

        int c = waitKey(30);
        if( c == ' ' )
            smoothType = smoothType == CV_GAUSSIAN ? CV_BLUR : smoothType == CV_BLUR ? CV_MEDIAN : CV_GAUSSIAN;
        if( c == 'q' || c == 'Q' || (c & 255) == 27 )

    return 0;

Please give me some advices. Thanks so much!!


Hi Leow,

You may try referring below link to initial camera capture first.



I had tried to initial camera capture by referring the the link and nothing happened when I run the code (I am not sure it should be). Previously, I had also tried accessing the camera using gstreamer:

gst-launch-1.0 nvcamerasrc fpsRange="30.0 30.0" ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! nvtee ! nvvidconv flip-method=2 ! 'video/x-raw(memory:NVMM), format=(string)I420' ! nvoverlaysink -e

and it was a success.

My concern now is to access the USB webcam I plugged in, where I always get the error:

Unable to stop the stream: Device or resource busy

It seems like quite a number of people face the same problem with USB webcam.

Thanks so much =)



Here are the update on the USB camera issue with OpenCV which I have been trying all this while.
Finally, it works on my board!
First, I had flashed my board with the latest release L4T 23.2 using JetPack 2.1.
Second, instead of using OpenCV4Tegra, I actually compiled and installed OpenCV 3.1.0 which I followed exactly written in this thread (message #5):
Other than hog.cpp code mentioned above, I had also tried with a few other codes and all works well. The only issue I experienced so far is that the video tends to lag by ~0.5 second (delayed). I am not sure anyone experience the same thing?


You can modify the included streamer sample app to use /dev/video0 as an source instead of /nvcamera0

Isn’t the steamer sample app the one running on VisionWorks? (nvx_sample_nvgstcamera_capture)

Isn’t there any other way to capture the video stream without passing through VisionWorks (which I just started to study)?

Hi Anacleto86,

You could Multimedia user guide to try the gstreamer pipeline directly, see below link: