Opencv cannot access my webcam

自闭症网瘾萝莉.ら 提交于 2019-12-03 18:00:18

I solved my problem few minutes ago. And I decided share my solution for people who handling similar error.

First I didnt install some of below packets ( I dont remember which of them, so I paste all of them)

libjpeg62-dev

libtiff4-dev

zlib1g-dev

libjasper-dev

libavcodec-dev

libdc1394-22-dev

libgstreamer0.10-dev

libgstreamer-plugins-base0.10-dev

libavformat-dev

libv4l-dev

libswscale-dev

Then You should configure your cmake process with this code

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

Please notice USE_V4L=ON this code..

I hope you solve after reading my solution.

#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;
}

Try the above code...

In some cases it is down to the inbuilt cameras response time (as it was in my case). I discovered that the in-built webcam on my HP G62 only "wakes up" after the first opencv cap.read(frame) call. Therefore to get a positive read from the camera (and hence no error later in the code) I made the call several times before proceeding:

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

For me the optimum was 4 read calls, which ensured my camera was awake and on before running through the main block of code. It is possible that a simple "waitKey" call would work and only two read calls, although I haven't tried this.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!