【问题标题】:Opencv cannot access my webcamOpencv 无法访问我的网络摄像头
【发布时间】:2013-01-20 12:04:24
【问题描述】:

我无法使用 opencv 2.4.3 访问网络摄像头。

我的系统:

Hp Probook 4530s - 惠普固定高清网络摄像头

Ubuntu 12.10

OpenCV 2.4.3

如果我想捕捉我的内置摄像头,我会收到 ERROR: capture is NULL

我正在使用http://opencv.willowgarage.com/wiki/CameraCapture 示例代码。

示例代码是:

#include "cv.h" 
#include "highgui.h" 
#include <stdio.h>  
// A Simple Camera Capture Framework 
int main() {
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
 fprintf( stderr, "ERROR: capture is NULL \n" );
 getchar();
 return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
 // Get one frame
 IplImage* frame = cvQueryFrame( capture );
 if ( !frame ) {
   fprintf( stderr, "ERROR: frame is null...\n" );
   getchar();
   break;
 }
 cvShowImage( "mywindow", frame );
 // Do not release the frame!
 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
 //remove higher bits using AND operator
 if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}

我还尝试使用打字终端使用 xawtv -hwscan。我得到这个输出:

looking for available devices
port 129-144

type : Xvideo, image scaler
name : Intel(R) Textured Video`


/dev/video0: OK    
             [ -device /dev/video0 ]
type : libv4l

name : HP HD Webcam [Fixed]

flags:  capture

然后我可以输入 xawtv video0 访问我的网络摄像头。我想我的网络摄像头没有问题。 我在使用 opencv 时遇到问题。

【问题讨论】:

  • 如果您使用 C++,请尝试以下示例代码:docs.opencv.org/modules/highgui/doc/…
  • 我试试docs.opencv.org/modules/highgui/doc/… 中的代码,它不起作用
  • 尝试在此处使用不同的数字:VideoCapture cap(0);,例如 1 和 2。如果这没有帮助,则问题不在于 OpenCV。
  • @Niko 我知道这些论点我也尝试 0 1 2 甚至 -1 但这不起作用。
  • 尝试不同的网络摄像头,即外置摄像头,因为我确定我在某处读到某些摄像头无法工作。如果您能够证明另一个摄像头工作正常,那么您就会知道您遇到了驱动程序问题。

标签: opencv webcam


【解决方案1】:

我在几分钟前解决了我的问题。我决定与处理类似错误的人分享我的解决方案。

首先我没有安装下面的一些数据包(我不记得它们中的哪一个,所以我把它们都粘贴了)

libjpeg62-dev

libtiff4-dev

zlib1g-dev

libjasper-开发

libavcodec-dev

libdc1394-22-dev

libgstreamer0.10-dev

libgstreamer-plugins-base0.10-dev

libavformat-dev

libv4l-dev

libswscale-开发

那么你应该用这段代码配置你的 cmake 进程

cmake -D CMAKE_BULD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON USE_V4L=ON WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON USE_GStreamer=ON ..

请注意 USE_V4L=ON 此代码..

希望您在阅读我的解决方案后解决。

【讨论】:

    【解决方案2】:
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    
    using namespace cv;
    using namespace std;
    
    int main()
    {
      VideoCapture webcam;
      webcam.open(0);
    
      if(!webcam.isOpened())//**EDITED**
        {
          std::cout<<"CANNOT OPEN CAM"<<std::endl;
          return -1;
        }
    
      Mat frame;
    
      while(true)
      {
        webcam >> frame;
        imshow("TEST",frame);
        waitKey(20);
      }
      return 0;
    }
    

    试试上面的代码...

    【讨论】:

    • if(!webcam.isOpened) 行是错误报告。 ../src/view.cpp:13:14: 错误:无法将 'cv::VideoCapture::isOpened' 从类型 'bool (cv::VideoCapture::)()const' 转换为类型 'bool' ../ src/view.cpp:13:14: 错误:在一元的参数中!
    • 程序立即退出。我认为,它返回 -1 因为 !webcam 未打开。
    • 返回 CANNOT OPEN CAM
    【解决方案3】:

    在某些情况下,这取决于内置摄像头的响应时间(就像我的情况一样)。我发现 HP G62 上的内置网络摄像头仅在第一次 opencv cap.read(frame) 调用后“唤醒”。因此,为了从相机中获得正面读数(因此代码后面没有错误),我在继续之前多次调用:

    if (!cap.read(frame))
    {
        if(!cap.read(frame))
        {
            if(!cap.read(frame))
            {
                if(!cap.read(frame))
                {
                     printf("Cam read error");
                }
            }
        }
    }
    

    对我来说,最佳值是 4 次读取调用,这可确保我的相机在运行主代码块之前处于唤醒状态并开启。一个简单的“waitKey”调用可能会起作用,并且只有两个读取调用,尽管我没有尝试过。

    【讨论】:

    • 其实你的情况很有意思。您是否提供了一些有关您的计算机配置的额外详细信息?
    • 你应该使用for循环而不是嵌套的if。
    猜你喜欢
    • 2011-06-12
    • 2018-04-12
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多