1. 程式人生 > >開啟雙目攝像頭,連續擷取並分割為左右畫面進行儲存

開啟雙目攝像頭,連續擷取並分割為左右畫面進行儲存

#include <iostream>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
    char c = 0;
    VideoCapture capture(0);
    Mat cam ;
    int i, j = 0;
    char filename[10];
    while (capture.read(cam))   //迴圈讀取攝像畫面幀capture >> cam
    {
        int w, h ;
        w= cam.cols;  //獲取相機畫面寬高
        h= cam.rows;      
        imshow("cam",cam);        
        Rect rect0(0, 0, w/2, h); //(x,y,width,height)定義矩形框擷取
        Mat caml = cam(rect0); //擷取左攝像頭畫面
        Rect rect1(w/2, 0, w/2, h);
        Mat camr = cam(rect1); //擷取右半邊

        c = cvWaitKey(10);
        if(c == 'c') //按c鍵採集影象
        {
            sprintf(filename, "left%d.jpg",i++);
            imwrite(filename, caml);
            sprintf(filename, "right%d.jpg",j++);
            imwrite(filename, camr);
            cout<<"capture ok!"<<endl; //每次擷取成功列印一次,圖片儲存在工程的編譯目錄下
        }
        c = cvWaitKey(10);
        if (c == 'q')//按q鍵退出 ,27為Esc鍵
        {
             break;
        }
    }
    return 0;
}