关闭

opencv代码段1

标签: opencv
288人阅读 评论(0) 收藏 举报
分类:

test camera

camera.cpp

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char** argv)
{
    if(argc != 2){
        cerr << "Usage: ./camera [camera id]"   << endl
           << "Example: ./camera 0" << endl;
        return 1;     
    }

    cout << "Press \"c\" to capture picture."   << endl
           << "Press \"q\" to exit." << endl;
    //cv::VideoCapture capture("bike.avi");

    int camera_id = atoi(argv[1]);
    cv::VideoCapture capture(camera_id);
    if(!capture.isOpened()){
        cerr << "Can not open camera" << endl;
        return 1;
    }

    double rate= capture.get(CV_CAP_PROP_FPS);
    bool stop(false);
    cv::Mat frame; 
    cv::namedWindow("Camera");

    int delay= 1000/rate;
    int count = 0;
    while (!stop) {
        if (!capture.read(frame))
            break;

        cv::imshow("Camera",frame);

        char key = cv::waitKey(delay);

        switch(key){
        case 'c':{
            stringstream ss;
            ss << count++;
            string filename = ss.str(); 
            filename += ".jpg";
            cv::imwrite(filename, frame);
            cout << "save " << filename << endl;
            break;
        }
        case 'q':{
            stop = true;
            break;
        }
        default:
            break;
        }
    }
    capture.release();
    cv::destroyAllWindows();
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(camera)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -std=c++11")

find_package( OpenCV REQUIRED )
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable( camera camera.cpp)
target_link_libraries( camera ${OpenCV_LIBS})
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场