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);
}
相关推荐
陈天伟教授3 小时前
基于学习的人工智能(7)机器学习基本框架
人工智能·学习
Z***G4794 小时前
网络爬虫学习:借助DeepSeek完善爬虫软件,实现模拟鼠标右键点击,将链接另存为本地文件
爬虫·学习·计算机外设
我命由我123456 小时前
微信开发者工具 - 模拟器分离窗口与关闭分离窗口
前端·javascript·学习·微信小程序·前端框架·html·js
DKPT6 小时前
ZGC和G1收集器相比哪个更好?
java·jvm·笔记·学习·spring
颜*鸣&空6 小时前
QT程序实现串口通信案例
开发语言·qt
Main. 246 小时前
从0到1学习Qt -- 常见控件之显示类控件
qt·学习
e***19356 小时前
爬虫学习 01 Web Scraper的使用
前端·爬虫·学习
二川bro11 小时前
多模态AI开发:Python实现跨模态学习
人工智能·python·学习
石像鬼₧魂石11 小时前
Netcat,网络瑞士军刀(新手学习备用)
学习
深蓝海拓11 小时前
opencv的模板匹配(Template Matching)学习笔记
人工智能·opencv·计算机视觉