12、编译QT Opencv项目----------Opencv移植Arm

编译QT Opencv项目

编译QT Opencv项目

因为我们使用的QT项目中包含Opencv,所以要修改pro文件,连接opencv库

bash 复制代码
#-------------------------------------------------
#
# Project created by QtCreator 2024-12-03T00:01:13
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = FaceDetectArm
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui


# 添加包含路径
INCLUDEPATH += /opt/arm-sysroot/usr/local/include/opencv4

#-------------------------------------------------
# 添加库路径和库
LIBS += -L/opt/arm-sysroot/usr/local/lib \
         -L/opt/arm-sysroot/usr/local/lib/opencv4/3rdparty \
        -lopencv_core \
        -lopencv_imgproc \
        -lopencv_objdetect \
        -lopencv_highgui \
        -lopencv_videoio \
        -lopencv_imgcodecs \
        -lopenjp2 \
        -lopencv_calib3d \
        -lopencv_features2d \
        -lopencv_flann \
        -ldl

# 如果需要链接 Qt 库(通常 Qt 会自动处理)
LIBS += -L/opt/qt5.12.10-arm/lib \
        -lQt5Widgets \
        -lQt5Core \
        -lQt5Gui

# 定义资源文件夹路径
RESOURCES_DIR = $$PWD/resources

# 定义目标复制目录路径
DEST_DIR = $$OUT_PWD/resources

# 仅在 Linux 系统下应用
unix {
    # 添加构建后命令,复制 resources 文件夹到输出目录
    QMAKE_POST_LINK += mkdir -p $$DEST_DIR && cp -r $$RESOURCES_DIR/* $$DEST_DIR/
}

头文件mainwindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QImage>
#include <QPixmap>
#include <QMessageBox>
#include <QFileDialog>
#include <opencv2/opencv.hpp>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void updateFrame();
    void on_processImageButton_clicked();
private:
    Ui::MainWindow *ui;
    cv::VideoCapture cap;
    cv::CascadeClassifier faceCascade;
    QTimer *timer;

    QImage Mat2QImage(const cv::Mat &mat);
};

#endif // MAINWINDOW_H

源文件mainwindow.cpp

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QImage>
#include <QPixmap>
#include <QMessageBox>
#include <QFileDialog>
#include <opencv2/opencv.hpp>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void updateFrame();
    void on_processImageButton_clicked();
private:
    Ui::MainWindow *ui;
    cv::VideoCapture cap;
    cv::CascadeClassifier faceCascade;
    QTimer *timer;

    QImage Mat2QImage(const cv::Mat &mat);
};

#endif // MAINWINDOW_H

主函数main.cpp

cpp 复制代码
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

ui布局

资源文件

项目的人脸训练数据放在resources目录下

haarcascade_frontalface_alt.xml

进入项目目录,创建build文件夹

bash 复制代码
cd /home/ubuntu/FaceDetectArm
mkdir build
cd build
pwd
/home/ubuntu/FaceDetectArm/build

进入build文件夹, 用qmake生成makefile文件

/opt/qt5.12.10-arm/bin/qmake ..

执行make编译

make -j4

相关推荐
xcyxiner2 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner3 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner3 天前
DicomViewer (添加模型类)3
qt
xcyxiner4 天前
DicomViewer (目录调整) 2
qt
xcyxiner4 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能6 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G6 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
森G6 天前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
森G6 天前
71、打包发布---------打包发布
c++·qt
初圣魔门首席弟子6 天前
Node.js 详细介绍(知识库版)
windows·qt·node.js·知识库