OpenCV 筆記 2 Camera測試 控制兩個相機

Camera測試 控制兩個相機

兩顆相機的設計

// g++ opencv-camera.cpp -o a.out `pkg-config --cflags --libs opencv`
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;



void Color_Cout(Mat frame,int Select_color1)
{
    for (int i = 0; i < frame.rows; i = i + 10)
    {
        for (int j = 0; j < frame.cols * 3; j = j + 3 * 10)
            cout << (int)frame.at<unsigned char>(i, j + Select_color1) << " ";
        cout << endl;
    }
    cout << endl << endl;
}




int main() {

    Mat frame0;
    VideoCapture cap0(0);
    if (!cap0.isOpened()) {
        cout << "Cannot open camera 0 \n";
        return 1;
    }
    
   
    Mat frame1;
    VideoCapture cap1(1);
    if (!cap1.isOpened()) {
        cout << "Cannot open camera 1 \n";
        return 1;
    }
    

    int Camera_Number = 0;
   

    while (true) {
        
        if (Camera_Number == 0) 
        {
            bool ret0 = cap0.read(frame0);
            if (!ret0) {
                cout << "Can't receive frame 0 (stream end?). Exiting ...\n";
                break;
            }
            imshow("live", frame0);
        }
        else
        {
            bool ret1 = cap1.read(frame1);
            if (!ret1) {
                cout << "Can't receive frame 1 (stream end?). Exiting ...\n";
                break;
            }
            imshow("live", frame1);
        }


        
        int WK = waitKey(1);
        if (WK == 'q') 
        {
            break;
        }
        else if (WK == 'a')
        {
            if (Camera_Number == 0)
                Camera_Number = 1;
            else 
                Camera_Number = 0;
        }
        else if (WK == 's')
        {
            cout << "Camera Size:" << frame0.total() << endl;
            cout << "Camera rows:" << frame0.rows << endl;
            cout << "Camera cols:" << frame0.cols << endl;
        }
        else if (WK == 'd')
        {
            int R_color1(2), G_color1(1), B_color1(0);

            if (Camera_Number == 0)
            {
                Color_Cout(frame0, R_color1);
                Color_Cout(frame0, G_color1);
                Color_Cout(frame0, B_color1);
            }
            else if (Camera_Number == 1)
            {
                Color_Cout(frame1, R_color1);
                Color_Cout(frame1, G_color1);
                Color_Cout(frame1, B_color1);
            }


        }


    }
    
    return 0;
}



 

單顆相機的設計

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



void Color_Cout(Mat frame,int Select_color1)
{
    for (int i = 0; i < frame.rows; i = i + 10)
    {
        for (int j = 0; j < frame.cols * 3; j = j + 3 * 10)
            cout << (int)frame.at<unsigned char>(i, j + Select_color1) << " ";
        cout << endl;
    }
    cout << endl << endl;
}




int main() {

    Mat frame0;
    VideoCapture cap0(0);
    if (!cap0.isOpened()) {
        cout << "Cannot open camera 0 \n";
        return 1;
    }



    int Camera_Number = 0;
    while (true) 
    {
        bool ret0 = cap0.read(frame0);
        if (!ret0) 
        {
            cout << "Can't receive frame 0 (stream end?). Exiting ...\n";
            break;
        }
        imshow("live", frame0);

        int WK = waitKey(1);
        if (WK == 'q')
        {
            break;
        }
        else if (WK == 's')
        {
            cout << "Camera Size:" << frame0.total() << endl;
        }
        else if (WK == 'd')
        {
            int R_color1(2), G_color1(1), B_color1(0);
            Color_Cout(frame0, R_color1);
            Color_Cout(frame0, G_color1);
            Color_Cout(frame0, B_color1);
        }
    }

    return 0;
}