欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux下利用Opencv打开笔记本摄像头问题

程序员文章站 2022-05-24 13:30:00
新建test文件夹,文件夹存在test.cpp和cmakelists.txttest.cpp#include #includ...

新建test文件夹,文件夹存在test.cppcmakelists.txttest.cpp#include <iostream>

#include <string>
#include <sstream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp> 
using namespace cv;
using namespace std;
const char* keys = 
{
  "{help h usage ? | | print this message}"
  "{@video | | video file, if not defined try to use webcamera}"
};
int main(int argc, const char** argv)
{
  commandlineparser parser(argc, argv, keys);
  parser.about("reading a video and camera v1.0.0");
  if (parser.has("help"))
  {
    parser.printmessage();
    return 0;
  }
  string videofile = parser.get<string>(0);
  if (!parser.check())
  {
    parser.printerrors();
    return 0;
  }
  videocapture cap; 
  if (videofile != "")
  {
    cap.open(videofile);// read a video file
  }else {
    cap.open(0);// read the default caera
  }
  if (!cap.isopened())// check if we succeeded
  {
    return -1;
  }
  namedwindow("video", 1);
  while (1)
  {
    mat frame;
    cap >> frame; // get a new frame from camera
    imshow("video", frame);
    if (waitkey(30) >= 0) break;
  }
  // release the camera or video file
  cap.release();
  return 0;
}

cmakelists.txt

project(test)
cmake_minimum_required(version 2.8.7)
# option to enable openmp; only relevant for the kcf version with the
# vot scale estimation
option(with_openmp "enable openmp" off)
if(with_openmp)
  find_package(openmp required)
  set(cmake_cxx_flags "${cmake_cxx_flags} ${openmp_cxx_flags}")
  set(cmake_exe_linker_flags "${cmake_exe_linker_flags} ${openmp_exe_linker_flags}")
endif(with_openmp)
# add c++11 support
if(cmake_compiler_is_gnucc)
  add_definitions ( -std=c++11 )
endif(cmake_compiler_is_gnucc)
# add opencv
set(opencv_dir_hint "")
if(win32)
  get_filename_component(opencv_dir_platform $env{opencv_dir} directory)
  get_filename_component(opencv_dir_hint ${opencv_dir_platform} directory)
endif(win32)
set(opencv_static off)
find_package(opencv required hints ${opencv_dir_hint})

编译过程:

> cd test
> g++ test.cpp -o test `pkg-config --cflags --libs opencv`
> ./test

Linux下利用Opencv打开笔记本摄像头问题

总结

以上所述是小编给大家介绍的linux下利用opencv打开笔记本摄像头问题,希望对大家有所帮助