ubuntu24.04 QT中配置opencv4.12

假如生成的opencv路径是:/usr/local/opencv4.12

cpp 复制代码
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#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

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target


# 以下是opencv的引用

INCLUDEPATH +=  /usr/local/opencv4.12/include\
                /usr/local/opencv4.12/include/opencv4 \
                /usr/local/opencv4.12/include/opencv4/opencv2

LIBS += /usr/local/opencv4.12/lib/libopencv_*.so \
        # /home/hwiki/OpenCV/install/lib/libopencv_core.so    \
        # /home/hwiki/OpenCV/install/lib/libopencv_imgproc.so \
        # /home/hwiki/OpenCV/install/lib/libopencv_imgcodecs.so
cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QPixmap>

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;  // 引入opencv的命名空间
using namespace std;

// 全局变量,用于存储原始图像
cv::Mat g_img_input;

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

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



void MainWindow::on_pushButton_clicked()
{

    QString filename = QFileDialog::getOpenFileName(this,
                                                    "打开图像文件",
                                                    ".",
                                                    "Image Files (*.bmp *.png *.jpg *.jpeg)");

    if (filename.isEmpty()) {
        QMessageBox::information(this, "提示", "未选择文件或文件打开失败!");
        return;
    }

    // 2. 使用 OpenCV 读取图像
    g_img_input = cv::imread(filename.toStdString()); // 在Ubuntu上,toStdString() 通常足够

    if (g_img_input.empty()) {
        QMessageBox::information(this, "提示", "无法读取图像文件,请检查文件路径和格式!");
        return;
    }

    // 3. 转换颜色空间 (OpenCV 读取的是 BGR, Qt 显示需要 RGB)
    cv::Mat img_rgb;
    cv::cvtColor(g_img_input, img_rgb, cv::COLOR_BGR2RGB);

    // 4. 将 cv::Mat 转换为 QImage
    // 注意:img_rgb 的数据必须是连续的,通常 imread 读取的是连续的
    QImage qimg(img_rgb.data,
                img_rgb.cols,
                img_rgb.rows,
                static_cast<int>(img_rgb.step), // 每行字节数
                QImage::Format_RGB888);

    // 5. 将 QImage 转换为 QPixmap 并显示在 QLabel 上
    QPixmap pixmap = QPixmap::fromImage(qimg);

    // 缩放图片以适应 QLabel 大小,同时保持原始比例
    QPixmap scaledPixmap = pixmap.scaled(ui->imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->imageLabel->setPixmap(scaledPixmap); // 假设您的 QLabel 名为 imageLabel

    // 设置 QLabel 的对齐方式,使图片居中显示
    ui->imageLabel->setAlignment(Qt::AlignCenter);

    // 6. 清理 (Mat 对象在函数结束时会自动释放)
    // img_input, img_rgb 会自动析构
}


void MainWindow::on_pushButton_gray_clicked()
{
    // 直接写在这里
    // 检查是否有已加载的图像
    if (g_img_input.empty()) {
        QMessageBox::information(this, "提示", "请先打开一个图像文件!");
        return;
    }

    // 将图像转换为灰度图
    cv::Mat img_gray;
    cv::cvtColor(g_img_input, img_gray, cv::COLOR_BGR2GRAY);

    // 将灰度图转换为 QImage (灰度格式)
    QImage qimg(img_gray.data,
                img_gray.cols,
                img_gray.rows,
                static_cast<int>(img_gray.step),
                QImage::Format_Grayscale8);

    // 将 QImage 转换为 QPixmap 并显示在 QLabel 上
    QPixmap pixmap = QPixmap::fromImage(qimg);

    // 缩放图片以适应 QLabel 大小,同时保持原始比例
    QPixmap scaledPixmap = pixmap.scaled(ui->imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->imageLabel->setPixmap(scaledPixmap);

    // 设置 QLabel 的对齐方式,使图片居中显示
    ui->imageLabel->setAlignment(Qt::AlignCenter);
}