简体   繁体   English

在OpenCV中动态更改视频

[英]Dynamically changing video in OpenCV

I have 3 video source connected to my PC and I want to show them on one screen. 我有3个视频源连接到我的电脑,我想在一个屏幕上显示它们。 I initially started to put the video sources next to each other and that works fine, but I want to be able to enable/disable each video source at run time. 我最初开始将视频源彼此相邻并且工作正常,但我希望能够在运行时启用/禁用每个视频源。

So I am want to use keyboard keys (r(right) and l(left)) to change what cameras are being shown at the moment. 所以我想使用键盘键(r(右)和l(左))来改变当前正在显示的摄像机。

I want to move the declaration of the following 3 variables to the outside of the while loop so I can access them in the if-cases and change them. 我想将以下3个变量的声明移到while循环的外部,这样我就可以在if-cases中访问它们并更改它们。

    cv::Mat3b combinedFrame(camRightSize.height, camMiddleSize.width + camRightSize.width);

    cv::Mat3b leftSideOfScreen(combinedFrame, Rect(0, 0, camMiddleSize.width, camMiddleSize.height));
    cameraMiddleFrameMirroredResize.copyTo(leftSideOfScreen);
    cv::Mat3b rightSideOfScreen(combinedFrame, Rect(camMiddleSize.width, 0, camRightSize.width, camRightSize.height));

Below is my whole code: 以下是我的全部代码:

int main(int argc, char **argv) {

    int combinedScreenWidth = 1440;
    int combinedScreenHeight = 540;
    int rearCameraBiggerByThis = 200;
    int combinedScreenWidthHalv = combinedScreenWidth / 2;

    bool showRight = true;
    bool showLeft = false;

    //initialize and allocate memory to load the video stream from camera 

    cv::VideoCapture cameraRight(0);    // RIGHT
    cv::VideoCapture cameraMiddle(3);   // REAR
    cv::VideoCapture cameraLeft(3);     // LEFT

    if (!cameraRight.isOpened())    return 1;
    if (!cameraMiddle.isOpened())   return 1;
    if (!cameraLeft.isOpened())     return 1;

    cv::Mat3b cameraRightFrame;
    cv::Mat3b cameraMiddleFrame;
    cv::Mat3b cameraLeftFrame;

    cv::Mat3b cameraRightFrameMirrored;
    cv::Mat3b cameraMiddleFrameMirrored;
    cv::Mat3b cameraLeftFrameMirrored;

    Size camRightSize;
    Size camMiddleSize;
    Size camLeftSize;

    cv::Mat3b cameraRightFrameMirroredResize;
    cv::Mat3b cameraMiddleFrameMirroredResize;
    cv::Mat3b cameraLeftFrameMirroredResize;


    while (true) {
        // Grab and retrieve each frames of the video sequentially 
        cameraRight >> cameraRightFrame;
        cameraMiddle >> cameraMiddleFrame;
        cameraLeft >> cameraLeftFrame;
        // Mirror
        cv::flip(cameraRightFrame, cameraRightFrameMirrored, 1);
        cv::flip(cameraMiddleFrame, cameraMiddleFrameMirrored, 1);
        cv::flip(cameraLeftFrame, cameraMiddleFrameMirrored, 1);
        // Resize
        camRightSize = cameraRightFrame.size();
        camMiddleSize = cameraMiddleFrame.size();
        camLeftSize = cameraLeftFrame.size();

        resize(cameraMiddleFrameMirrored, cameraMiddleFrameMirroredResize, Size(combinedScreenWidthHalv + rearCameraBiggerByThis, combinedScreenHeight));
        resize(cameraRightFrameMirrored, cameraRightFrameMirroredResize, Size(combinedScreenWidthHalv - rearCameraBiggerByThis, combinedScreenHeight));

        // Compilation
        camRightSize = cameraRightFrameMirroredResize.size();
        camMiddleSize = cameraMiddleFrameMirroredResize.size();
        camLeftSize = cameraLeftFrameMirroredResize.size();

        if (showRight && showLeft) {    // LEFT + REAR + RIGHT

        } else if (showRight) {         // REAR + RIGHT

        } else if (showLeft) {          // LEFT + REAR

        } else {                        // REAR

        }

        cv::Mat3b combinedFrame(camRightSize.height, camMiddleSize.width + camRightSize.width);

        cv::Mat3b leftSideOfScreen(combinedFrame, Rect(0, 0, camMiddleSize.width, camMiddleSize.height));
        cameraMiddleFrameMirroredResize.copyTo(leftSideOfScreen);
        cv::Mat3b rightSideOfScreen(combinedFrame, Rect(camMiddleSize.width, 0, camRightSize.width, camRightSize.height));
        cameraRightFrameMirroredResize.copyTo(rightSideOfScreen);

        // declare windows
        cv:namedWindow("Combined", CV_WINDOW_NORMAL);
        cv::setWindowProperty("Combined", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
        cv::putText(combinedFrame, "REAR", cv::Point(500, 50), cv::FONT_HERSHEY_PLAIN, 2.0, cv::Scalar(255, 255, 255), 2 );

        cv::putText(combinedFrame, "RIGHT", cv::Point(950, 50), cv::FONT_HERSHEY_PLAIN, 2.0, cv::Scalar(255, 255, 255), 2 );


        cv::imshow("Combined", combinedFrame);          // 1440 x 540 Screen size
        //cv::imshow("Right Cam", cameraRightFrame);
        //cv::imshow("Middle Cam", cameraMiddleFrame);
        //cv::imshow("Left Cam", cameraLeftFrame);


        //wait for 40 milliseconds
        int c = cvWaitKey(1);

        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
        if (27 == char(c)) {
            break;
        }
        else if (114 == char(c)) {
            showRight = !showRight;
        }
        else if (108 == char(c)) {
            showLeft = !showLeft;
        }
    }

    return 0;
}

According to your description, I think what you're wanting to write is: 根据你的描述,我认为你想要写的是:

combinedFrame(Rect(0, 0, camMiddleSize.width, camMiddleSize.height)).copyTo(leftSideOfScreen);

That is: 那是:

  • create a cv::Mat from another one (call to cv::Mat::copyTo(cv::Mat&) ) 从另一个创建一个cv::Mat (调用cv::Mat::copyTo(cv::Mat&)
  • the "source matrix" is obtained by extracting a rectangle from combinedFrame : combinedFrame(Rect(0, 0, camMiddleSize.width, camMiddleSize.height)) “源矩阵”是通过从combinedFrame提取矩形获得的: combinedFrame(Rect(0, 0, camMiddleSize.width, camMiddleSize.height))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM