Qt OpenCV 学习(四):车道线检测

1. mainwindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/opencv.hpp>
#include <QString>
#include <QFileDialog>
#include <QTimer>
#include <QDebug>
#include <QLabel>

using namespace cv;
using namespace std;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void showFrame();

private slots:
    void on_btnOpen_clicked();

    void on_btnPlay_clicked();

private:
    Ui::MainWindow *ui;
    QLabel *infoLabel;
    VideoCapture videoCap;
    QTimer *videoTimer;
};
#endif // MAINWINDOW_H

2. mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);

    infoLabel = new QLabel();
    infoLabel->setMinimumWidth(200);
    this->statusBar()->addWidget(infoLabel);

    videoTimer = new QTimer();

    connect(videoTimer, &QTimer::timeout, this, &MainWindow::showFrame);
}

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

void MainWindow::showFrame() {
    Mat currentFrame;

    videoCap >> currentFrame;
    if (!currentFrame.empty()) {
        infoLabel->setText("video is playing");
        Mat gaussionFrame, grayFrame, cannyFrame;
        // 高斯变换
        GaussianBlur(currentFrame, gaussionFrame, Size(5, 5), 1, 0);
        // 灰度
        cvtColor(gaussionFrame, grayFrame, COLOR_BGR2GRAY);
        // canny 边缘检测
        Canny(grayFrame, cannyFrame, 50, 100);

        Mat resultFrame;
        Mat maskFrame = Mat::zeros(cannyFrame.size(), cannyFrame.type());

        vector<Point>polyPoints;
        polyPoints.push_back(Point(0, 368));
        polyPoints.push_back(Point(300, 210));
        polyPoints.push_back(Point(340, 210));
        polyPoints.push_back(Point(640, 368));

        fillPoly(maskFrame, vector<vector<Point>>{polyPoints}, Scalar(255, 255, 255));
        bitwise_and(cannyFrame, maskFrame, resultFrame);

        vector<Vec4i>lines;
        HoughLinesP(resultFrame, lines, 1, CV_PI / 180, 5, 40, 20);

        for (size_t i = 0; i < lines.size(); i++) {
            Vec4i carLine = lines[i];
            double lineSlope = (double(carLine[3]) - double(carLine[1])) / (double(carLine[2]) - double(carLine[0]));
            if (lineSlope > 0) {
                // 左车道线
                line(currentFrame, Point(carLine[0], carLine[1]), Point(carLine[2], carLine[3]), Scalar(0, 255, 255), 5, LINE_AA);
            } else if (lineSlope < 0) {
                // 右车道线
                line(currentFrame, Point(carLine[0], carLine[1]), Point(carLine[2], carLine[3]), Scalar(0, 255, 255), 5, LINE_AA);
            }
        }

        QImage showImg = QImage((const unsigned char*)(currentFrame.data), currentFrame.cols, currentFrame.rows, currentFrame.step, QImage::Format_RGB888);
        ui->labelPlay->setPixmap(QPixmap::fromImage(showImg.scaled(ui->labelPlay->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    } else {
        infoLabel->setText("video is over");
        qDebug() << "video is over!";
        videoTimer->stop();
        videoCap.release();
    }
}

void MainWindow::on_btnOpen_clicked() {
    QString videoPath = QFileDialog::getOpenFileName(this, "open video", ".", "video(*.mp4 *.avi, *.flv); All files(*.*)");
    ui->lineEdit->setText(videoPath);
    infoLabel->setText("video is loaded");
}

void MainWindow::on_btnPlay_clicked() {
    ui->labelPlay->clear();
    QString videoPath = ui->lineEdit->text();
    videoCap.open(videoPath.toStdString());

    double rate = videoCap.get(CAP_PROP_FPS);
    double videoDelay = 1000 / rate;
    videoTimer->start(videoDelay);
}
相关推荐
清风玉骨10 分钟前
Qt-QHBoxLayout布局类控件(42)
开发语言·qt
一 乐26 分钟前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·数据库·学习·考研·微信·小程序·源码
感谢地心引力33 分钟前
【Qt】Qt安装(2024-10,QT6.7.3,Windows,Qt Creator 、Visual Studio、Pycharm 示例)
c++·windows·python·qt·visual studio
William_Edmund1 小时前
C++ 算法学习——1.8 悬线法
学习
朝九晚五ฺ1 小时前
【Linux探索学习】第三弹——Linux的基础指令(下)——开启新篇章的大门
linux·运维·学习
T0uken1 小时前
【QT Quick】C++交互:调用QML函数
c++·qt·交互
IM_DALLA1 小时前
【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL74
学习·fpga开发·verilog学习
王俊山IT2 小时前
C++学习笔记----8、掌握类与对象(五)---- 嵌套类与类中枚举
开发语言·c++·笔记·学习
北极无雪2 小时前
Spring源码学习(拓展篇):SpringMVC中的异常处理
java·开发语言·数据库·学习·spring·servlet
T0uken3 小时前
【QT Quick】C++交互:QML对象操作
c++·qt·交互