1. 程式人生 > >OpenCV讀雙目攝像頭合併影象並分割

OpenCV讀雙目攝像頭合併影象並分割

買了個usb介面的雙目攝像頭,首先讀出影象,發現讀出來的影象是合併的,將整個影象分割為左右影象,並且實時顯示.

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include<unistd.h>
using namespace cv;
using namespace std;

#define WIDTH 2560
#define HEIGHT 720
int main() {

    VideoCapture capture(0);
    if (!capture.isOpened())
    {
        cout<<"can not open the camera"<<endl; cin.get(); exit(1);
    }
    capture.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
    int count=0;

    Rect leftRect(0, 0, WIDTH/2, HEIGHT);   //建立一個Rect框,屬於cv中的類,四個引數代表x,y,width,height
    Rect rightRect(WIDTH/2-1, 0, WIDTH/2, HEIGHT);
    while (1) {
        Mat frame; capture>>frame; //載入影象
        if (frame.empty())
        {
            //判斷影象是否載入 cout<<"can not load the frame"<<endl;
        } else {
            count++;
            if (count == 1) {
                cout<<frame.cols<<" "<<frame.rows<<endl;
            }
            Mat leftImg,rightImg;
            frame(leftRect).copyTo(leftImg);
            frame(rightRect).copyTo(rightImg);
            imshow("left",leftImg);
            imshow("right",rightImg);
            imshow("oriImage", frame);
            char c=waitKey(30); //延時30毫秒 if (c == 27) //按ESC鍵退出 break;

        }
    }
}