OpenCV图像处理--一个窗口显示多个摄像头

OpenCV图像处理--一个窗口显示多个摄像头,第1张

OpenCV图像处理--一个窗口显示多个摄像头

【一】原理
1、开启两个摄像头,系统默认从0开始查找,两个摄像头使用0和1就够啦,然后把显示窗口尺寸固定,这里用的是320和240

VideoCapture capture1(0);
VideoCapture capture2(1);

2、创建一个黑色图片,大小刚好可以容纳两个摄像头窗口,width=640,height=240

Mat frame(240,640,CV_8UC3,Scalar(0,0,0));

3、创建ROI区域

ROIImage1 = frame(Rect(0,0,320,240));
ROIImage2 = frame(Rect(320, 0, 320, 240));

4、创建掩膜

掩膜必须是灰度图,因此先要把图片转化为灰度图

cvtColor(frame1, gray_Image1,CV_RGB2GRAY);
cvtColor(frame2, gray_Image2, CV_RGB2GRAY);

然后把图片复制给掩膜

mask1 = gray_Image1;
mask2 = gray_Image2;

5、把掩膜复制到ROI区域,frame1为要覆盖的图片,意思就是把frame1复制ROI区域的掩膜上。

frame1.copyTo(ROIImage1, mask1);
frame2.copyTo(ROIImage2, mask2);

【二】效果图

【三】源码

#include 
#include 

using namespace std;
using namespace cv;

//【画框】
void Draw_rectangle(Mat img,Point pt1, Point pt2)
{
    rectangle(img, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}

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

    Mat srcImage,srcImage2,srcImage3,srcImage4;
    Mat ROIImage1,ROIImage2,ROIImage3,ROIImage4,mask1,mask2,mask3,mask4,gray_Image1, gray_Image2,gray_Image3,gray_Image4;
    VideoCapture cap(0);
    VideoCapture cap1("car_input.mp4");
    VideoCapture cap2("carMove2.mp4");
    VideoCapture cap3("carMove.mp4");
    cap.set(CV_CAP_PROP_frame_WIDTH, 320);
    cap.set(CV_CAP_PROP_frame_HEIGHT, 240);
    cap1.set(CV_CAP_PROP_frame_WIDTH, 320);
    cap1.set(CV_CAP_PROP_frame_HEIGHT, 240);
    cap2.set(CV_CAP_PROP_frame_WIDTH, 320);
    cap2.set(CV_CAP_PROP_frame_HEIGHT, 240);
    cap3.set(CV_CAP_PROP_frame_WIDTH, 320);
    cap3.set(CV_CAP_PROP_frame_HEIGHT, 240);

    if (!cap.isOpened())
    {
        cout << "VideoCapture open fail" << endl;
        return 0;
    }
    if (!cap1.isOpened())
    {
        cout << "VideoCapture1 open fail" << endl;
        return 0;
    }

    if (!cap2.isOpened())
    {
        cout << "VideoCapture2 open fail" << endl;
        return 0;
    }
    if (!cap3.isOpened())
    {
        cout << "VideoCapture3 open fail" << endl;
        return 0;
    }
    Mat frame3(480,640,CV_8UC3,Scalar(0,0,0));
    char temp_1[20];
    char temp_2[20];
    char temp_3[20];
    char temp_4[20];
    while (cap.isOpened())
    {
        cap >> srcImage;
        cap1 >> srcImage2;
        cap2 >> srcImage3;
        cap3 >> srcImage4;

        resize(srcImage2, srcImage2,Size(320,240));
        resize(srcImage3, srcImage3,Size(320,240));
        resize(srcImage4, srcImage4,Size(320,240));
        Draw_rectangle(srcImage, Point(0,0),Point(srcImage.cols, srcImage.rows));
        Draw_rectangle(srcImage2, Point(0, 0), Point(srcImage2.cols, srcImage2.rows));
        Draw_rectangle(srcImage3, Point(0, 0), Point(srcImage3.cols, srcImage3.rows));
        Draw_rectangle(srcImage4, Point(0, 0), Point(srcImage4.cols, srcImage4.rows));

        sprintf(temp_1, "capture1");
        putText(srcImage, temp_1, Point(srcImage.cols / 16, srcImage.rows / 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 255));
        sprintf(temp_2, "capture2");
        putText(srcImage2, temp_2, Point(srcImage2.cols / 16, srcImage2.rows / 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 255));
        sprintf(temp_3, "capture3");
        putText(srcImage3, temp_3, Point(srcImage3.cols / 16, srcImage3.rows / 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 255));
        sprintf(temp_4, "capture4");
        putText(srcImage4, temp_4, Point(srcImage4.cols / 16, srcImage4.rows / 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 255));


        ROIImage1 = frame3(Rect(0,0,320,240));
        ROIImage2 = frame3(Rect(320, 0, 320, 240));
        ROIImage3 = frame3(Rect(0, 240, 320, 240));
        ROIImage4 = frame3(Rect(320, 240, 320, 240));
        //imshow("ROIImage1", ROIImage1);
        //imshow("ROIImage2", ROIImage2);
        cvtColor(srcImage, gray_Image1,CV_RGB2GRAY);
        cvtColor(srcImage2, gray_Image2, CV_RGB2GRAY);
        cvtColor(srcImage3, gray_Image3, CV_RGB2GRAY);
        cvtColor(srcImage4, gray_Image4, CV_RGB2GRAY);

        mask1 = gray_Image1;
        mask2 = gray_Image2;
        mask3 = gray_Image3;
        mask4 = gray_Image4;
        srcImage.copyTo(ROIImage1, mask1);
        srcImage2.copyTo(ROIImage2, mask2);
        srcImage3.copyTo(ROIImage3, mask3);
        srcImage4.copyTo(ROIImage4, mask4);
//        imshow("ROIImage1", srcImage);
//        imshow("ROIImage2", srcImage2);
//        imshow("srcImage3", srcImage3);
//        imshow("srcImage4", srcImage4);
        imshow("main", frame3);
        if (waitKey(25)==27)
        {
            break;
        }
    }
    cap.release();
    destroyAllWindows();
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5702609.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存