I have 3 video source connected to my PC and I want to show them on one screen. 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.
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.
    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;
}
 
    