opencv图像四路拼接在qtgui界面上显示(带源码)

先上效果图,文章会分享源码哦

基本思路:先把四个视频的图像凭借到一个上,然后利用QTimer的信号设置到一定时间后就进行图片的更新

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <QImage>
#include <QTimer>
using namespace std;
using namespace cv;


namespace Ui {
class MainWidget;
}

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MainWidget(QWidget *parent = nullptr);
    ~MainWidget();
    void Draw_rectangle(Mat img,Point pt1, Point pt2);

private:
    Ui::MainWidget *ui;
    VideoCapture capture1;
    VideoCapture capture2;
    VideoCapture capture3;
    VideoCapture capture4;
    QTimer *timer;
public slots:
    void on_timer_timeout();
};

#endif // MAINWIDGET_H
#include "MainWidget.h"
#include "ui_MainWidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    this->timer=new QTimer(this);
    ui->setupUi(this);
    connect(this->timer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));
    this->capture1.open("/home/test/qt-workspace/opencv_view1/dm.mp4");
    this->capture2.open("/home/test/qt-workspace/opencv_view1/carMove.mp4");
    this->capture3.open("/home/test/qt-workspace/opencv_view1/carMove2.mp4");
    this->capture4.open("/home/test/qt-workspace/opencv_view1/1.avi");
    capture1.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    capture1.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
    capture2.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    capture2.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
    capture3.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    capture3.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
    capture4.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    capture4.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
    if(!capture1.isOpened())
    {
        std::cout<<"capture1 error"<<endl;
    }
    if(!capture2.isOpened())
    {
        std::cout<<"capture2 error"<<endl;
    }
    if(!capture3.isOpened())
    {
        std::cout<<"capture3 error"<<endl;
    }
    if(!capture4.isOpened())
    {
        std::cout<<"capture4 error"<<endl;
    }

    this->timer->start(30);


}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::Draw_rectangle(Mat img, Point pt1, Point pt2)
{
    rectangle(img, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}

void MainWidget::on_timer_timeout()
{
    Mat frame1, frame2,frame3,frame4,ROIImage1, ROIImage2,mask1,mask2,gray_Image1, gray_Image2;
    Mat ROIImage3, ROIImage4,mask3,mask4,gray_Image3, gray_Image4;
    Mat frame5(480,640,CV_8UC3,Scalar(0,0,0));
            capture1 >> frame1;
            capture2 >> frame2;
            capture3 >> frame3;
            capture4 >> frame4;
            cv::resize(frame1,frame1,Size(320,240));
            cv::resize(frame2,frame2,Size(320,240));
            cv::resize(frame3,frame3,Size(320,240));
            cv::resize(frame4,frame4,Size(320,240));
            Draw_rectangle(frame1, Point(0,0),Point(frame1.cols, frame1.rows));
            Draw_rectangle(frame2, Point(0, 0), Point(frame2.cols, frame2.rows));
            Draw_rectangle(frame3, Point(0, 0), Point(frame3.cols, frame3.rows));
            Draw_rectangle(frame4, Point(0, 0), Point(frame4.cols, frame4.rows));
            //sprintf(temp_1, "capture1");
            //putText(frame1, temp_1, Point(frame1.cols / 16, frame1.rows / 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 255));
            //sprintf(temp_2, "capture2");
            //putText(frame2, temp_2, Point(frame2.cols / 16, frame2.rows / 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 255));
            ROIImage1 = frame5(Rect(0,0,320,240));
            ROIImage2 = frame5(Rect(320, 0, 320, 240));
            ROIImage3 = frame5(Rect(0,240,320,240));
            ROIImage4 = frame5(Rect(320,240,320,240));
            //imshow("【ROIImage1】", ROIImage1);
            //imshow("【ROIImage2】", ROIImage2);
            cvtColor(frame1, gray_Image1,CV_RGB2GRAY);
            cvtColor(frame2, gray_Image2, CV_RGB2GRAY);
            cvtColor(frame3, gray_Image3, CV_RGB2GRAY);
            cvtColor(frame4, gray_Image4, CV_RGB2GRAY);
            mask1 = gray_Image1;
            mask2 = gray_Image2;
            mask3 = gray_Image3;
            mask4 = gray_Image4;
            frame1.copyTo(ROIImage1, mask1);
            frame2.copyTo(ROIImage2, mask2);
            frame3.copyTo(ROIImage3, mask3);
            frame4.copyTo(ROIImage4, mask4);
            cvtColor(frame5,frame5,CV_BGR2RGB);
            QImage disimage=QImage(frame5.data,frame5.cols,frame5.rows,QImage::Format_RGB888);
            ui->label->setPixmap(QPixmap::fromImage(disimage.scaled(ui->label->size(),Qt::KeepAspectRatio)));
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Qt显示OpenCV图像并框选ROI,可以按照以下步骤进行: 1.使用OpenCV读取图像文件: ```c++ cv::Mat image = cv::imread("image.jpg"); ``` 2.将图像转换为Qt格式: ```c++ QImage img(image.data, image.cols, image.rows, QImage::Format_RGB888); ``` 3.创建一个QLabel并设置图像: ```c++ QLabel* label = new QLabel(this); label->setPixmap(QPixmap::fromImage(img)); ``` 4.添加一个鼠标事件处理程序以框选ROI: ```c++ void MyWidget::mousePressEvent(QMouseEvent* event) { if (event->button() == Qt::LeftButton) { // 记录鼠标按下的位置 startPoint = event->pos(); } } void MyWidget::mouseReleaseEvent(QMouseEvent* event) { if (event->button() == Qt::LeftButton) { // 记录鼠标松开的位置 endPoint = event->pos(); // 计算矩形的左上角和右下角坐标 int x = qMin(startPoint.x(), endPoint.x()); int y = qMin(startPoint.y(), endPoint.y()); int width = qAbs(startPoint.x() - endPoint.x()); int height = qAbs(startPoint.y() - endPoint.y()); // 在图像上绘制矩形 cv::rectangle(image, cv::Rect(x, y, width, height), cv::Scalar(0, 255, 0), 2); // 将图像转换为Qt格式并设置到QLabel上 QImage img(image.data, image.cols, image.rows, QImage::Format_RGB888); label->setPixmap(QPixmap::fromImage(img)); } } ``` 在上面的代码中,我们记录鼠标按下和松开时的位置,并计算矩形的左上角和右下角坐标。然后,我们使用OpenCV图像上绘制一个矩形,并将图像转换为Qt格式并设置到QLabel上。 完整的示例代码如下: ```c++ #include <QtWidgets> #include <opencv2/opencv.hpp> class MyWidget : public QWidget { public: MyWidget(QWidget* parent = nullptr) : QWidget(parent) { // 使用OpenCV读取图像文件 cv::Mat image = cv::imread("image.jpg"); // 将图像转换为Qt格式 QImage img(image.data, image.cols, image.rows, QImage::Format_RGB888); // 创建一个QLabel并设置图像 label = new QLabel(this); label->setPixmap(QPixmap::fromImage(img)); // 设置窗口大小 setFixedSize(image.cols, image.rows); // 设置鼠标跟踪 setMouseTracking(true); label->setMouseTracking(true); label->installEventFilter(this); } protected: void mousePressEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton) { // 记录鼠标按下的位置 startPoint = event->pos(); } } void mouseReleaseEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton) { // 记录鼠标松开的位置 endPoint = event->pos(); // 计算矩形的左上角和右下角坐标 int x = qMin(startPoint.x(), endPoint.x()); int y = qMin(startPoint.y(), endPoint.y()); int width = qAbs(startPoint.x() - endPoint.x()); int height = qAbs(startPoint.y() - endPoint.y()); // 在图像上绘制矩形 cv::Mat image = cv::imread("image.jpg"); cv::rectangle(image, cv::Rect(x, y, width, height), cv::Scalar(0, 255, 0), 2); // 将图像转换为Qt格式并设置到QLabel上 QImage img(image.data, image.cols, image.rows, QImage::Format_RGB888); label->setPixmap(QPixmap::fromImage(img)); } } private: QLabel* label; QPoint startPoint; QPoint endPoint; }; int main(int argc, char* argv[]) { QApplication app(argc, argv); MyWidget widget; widget.show(); return app.exec(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值