欢迎来到代码驿站!

Shell

当前位置:首页 > 脚本语言 > Shell

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

时间:2021-02-17 14:07:08|栏目:Shell|点击:

新建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

总结

上一篇:shell 操作钉钉机器人实现告警提醒的方法

栏    目:Shell

下一篇:利用shell创建文本菜单与窗口部件的方法

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

本文地址:http://www.codeinn.net/misctech/64958.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有