Use Opencv in Linux to open the laptop camera, and use linuxopencv

Source: Internet
Author: User

Use Opencv in Linux to open the laptop camera, and use linuxopencv

NewTestFolder. The folder exists.Test. cppAndCMakeLists.txt

Test. 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 estimationoption(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 supportif(CMAKE_COMPILER_IS_GNUCC)    ADD_DEFINITIONS ( -std=c++11 )endif(CMAKE_COMPILER_IS_GNUCC)# add OpenCVset(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})

Compilation process:

 

> Cd test
> G ++ test. cpp-o test 'pkg-config -- cflags -- libs opencv'
>./Test

 

 

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.