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);
}
相关推荐
Monkey的自我迭代2 小时前
图像金字塔---图像上采样下采样
人工智能·opencv·计算机视觉
悠悠~飘3 小时前
php学习(第二天)
开发语言·学习·php
zgc12453673 小时前
汇编基础1
汇编·学习
qq7798233404 小时前
PMP考试学习计划与知识大纲
学习·产品经理
索迪迈科技4 小时前
java后端工程师进修ing(研一版‖day42)
java·开发语言·学习·算法
dlraba8024 小时前
OpenCV 实战:多角度模板匹配实现图像目标精准定位
人工智能·opencv·计算机视觉
长安——归故李5 小时前
【modbus学习】
java·c语言·c++·学习·算法·c#
索迪迈科技5 小时前
STL库——map/set(类函数学习)
开发语言·c++·学习
我命由我123455 小时前
Excel 表格 - Excel 减少干扰、专注于内容的查看方式
学习·ui·excel·photoshop·表格·ps·美工
我命由我123455 小时前
Excel 表格 - Excel 单元格添加边框
学习·ui·excel·课程设计·photoshop·ps·美工