MyFirstQT.cpp
#include "MyFirstQT.h" #include "ui_MyFirstQT.h" #include <QFileDialog> #include <QMessageBox> #include <QPixmap> #include <opencv2/opencv.hpp> #include <QDebug> #include <opencv2/imgproc.hpp> using namespace cv; using namespace std; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MyFirstQTClass) { ui->setupUi(this); //connect(ui->loadImageButton, &QPushButton::clicked, this, &MainWindow::on_loadImageButton_clicked);//这句如果不注释,打开文件窗口会跳2次 //connect(ui->sharpenImageButton, &QPushButton::clicked, this, &MainWindow::on_sharpenImageButton_clicked); } MainWindow::~MainWindow() { delete ui; } cv::Mat img_input; cv::Mat tempRH;//锐化 void MainWindow::on_loadImageButton_clicked() { QString filename = QFileDialog::getOpenFileName(this, "打开图像文件", "C:/Users", "Image Files (*.bmp;*.png;*.jpg)"); if (filename.isEmpty()) { QMessageBox::information(this, "提示", "文件打开失败1!"); return; } img_input = cv::imread(filename.toLocal8Bit().toStdString()); if (img_input.empty()) { QMessageBox::information(this, "提示", "文件打开失败2!"); return; } cv::Mat temp; cv::cvtColor(img_input, temp, cv::COLOR_BGR2RGB); // 直接将文件名传递给 QPixmap 进行加载 QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888)); if (pixmap.isNull()) { QMessageBox::information(this, "提示", "文件打开失败2!"); return; } // 获取 label 的尺寸 int labelWidth = ui->label_image->width(); int labelHeight = ui->label_image->height(); // 将 QPixmap 进行缩放 QPixmap scaledPixmap = pixmap.scaled(labelWidth, labelHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 将缩放后的图像设置到 label 中 ui->label_image->setPixmap(scaledPixmap); ui->label_image->setScaledContents(false); ui->label_image->setAlignment(Qt::AlignCenter); } void MainWindow::on_sharpenImageButton_clicked() { // 假设之前加载的图像存储在成员变量 img_input 中 if (img_input.empty()) { QMessageBox::information(this, "提示", "尚未加载图片,无法进行锐化操作!"); return; } // 定义锐化核 cv::Mat kernel = (cv::Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); cv::Mat img_sharpened; cv::filter2D(img_input, img_sharpened, -1, kernel); cv::Mat temp; cv::cvtColor(img_sharpened, temp, cv::COLOR_BGR2RGB); tempRH = img_sharpened; // 直接将锐化后的图像转换为 QPixmap 并显示在 label_image2 中 QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888)); if (pixmap.isNull()) { QMessageBox::information(this, "提示", "锐化后的文件打开失败!"); return; } // 获取 label_image2 的尺寸 int labelWidth2 = ui->label_image2->width(); int labelHeight2 = ui->label_image2->height(); // 将 QPixmap 进行缩放 QPixmap scaledPixmap = pixmap.scaled(labelWidth2, labelHeight2, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 将缩放后的图像设置到 label_image2 中 ui->label_image2->setPixmap(scaledPixmap); ui->label_image2->setScaledContents(false); ui->label_image2->setAlignment(Qt::AlignCenter); } void MainWindow::on_histogramImageButton_clicked() { if (img_input.empty()) { QMessageBox::information(this, "提示", "尚未加载图片,无法获取直方图!"); return; } cv::Mat grayImg; cv::cvtColor(img_input, grayImg, cv::COLOR_BGR2GRAY); // 计算直方图 Mat hist; int histSize = 256; float range[] = { 0, 256 }; const float* histRange = { range }; calcHist(&grayImg, 1, 0, Mat(), hist, 1, &histSize, &histRange); // 绘制直方图 int hist_w = 500; // 缩小宽度 int hist_h = 400; int bin_w = cvRound((double)hist_w / histSize); Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0)); normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); for (int i = 1; i < histSize; i++) { line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(hist.at<float>(i - 1))), Point(bin_w * i, hist_h - cvRound(hist.at<float>(i))), Scalar(255, 255, 255), 2); } // 绘制横坐标 for (int x = 0; x <= 255; x += 50) { line(histImage, Point(bin_w * x, hist_h), Point(bin_w * x, hist_h - 10), Scalar(255, 255, 255), 1); putText(histImage, QString::number(x).toStdString(), Point(bin_w * x - 10, hist_h + 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255)); } line(histImage, Point(bin_w * 255, hist_h), Point(bin_w * 255, hist_h - 10), Scalar(255, 255, 255), 1); // 255 刻度线 putText(histImage, "255", Point(bin_w * 255 - 10, hist_h + 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255)); line(histImage, Point(hist_w, hist_h), Point(hist_w, hist_h - 10), Scalar(255, 255, 255), 1); // 超出 255 的刻度线 putText(histImage, "300", Point(hist_w - 20, hist_h + 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255)); // 绘制纵坐标 int y_interval = 250; for (int y = 0; y <= 2500; y += y_interval) { line(histImage, Point(0, hist_h - y / (2500.0 / hist_h)), Point(10, hist_h - y / (2500.0 / hist_h)), Scalar(255, 255, 255), 1); putText(histImage, QString::number(y).toStdString(), Point(20, hist_h - y / (2500.0 / hist_h) - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255)); } // 将绘制的直方图转换为 QImage 并显示在 label_image2 中 QImage qImg((const unsigned char*)(histImage.data), histImage.cols, histImage.rows, histImage.step, QImage::Format_RGB888); QPixmap pixmap = QPixmap::fromImage(qImg); ui->label_image2->setPixmap(pixmap); }//直方图示范 https://blog.csdn.net/u010140856/article/details/79256015 void MainWindow::on_SaveButton_clicked() { if (tempRH.empty()) { QMessageBox::information(this, "提示", "尚未进行锐化操作,无法保存!"); return; } QString savePath = QFileDialog::getSaveFileName(this, "保存锐化后的图片", "", "Image Files (*.bmp;*.png;*.jpg;*.jpeg;*.tif;*.tiff)"); if (savePath.isEmpty()) { return; } cv::imwrite(savePath.toLocal8Bit().toStdString(), tempRH); // 保存锐化后的图片,保持原始尺寸 } //自动色阶 void MainWindow::on_autoLevelsButton_clicked() { if (img_input.empty()) { QMessageBox::information(this, "提示", "尚未加载图片,无法进行转换为漫画操作!"); return; } cv::Mat grayImg; cv::cvtColor(img_input, grayImg, cv::COLOR_BGR2GRAY); // 双边滤波 cv::Mat bilateralFilteredImg; cv::bilateralFilter(grayImg, bilateralFilteredImg, 9, 75, 75); // 自适应阈值 cv::Mat adaptiveThresholdImg; cv::adaptiveThreshold(bilateralFilteredImg, adaptiveThresholdImg, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 9, 5); cv::Mat temp; cv::cvtColor(adaptiveThresholdImg, temp, cv::COLOR_GRAY2RGB); // 直接将处理后的图像转换为 QPixmap 并显示在 label_image2 中 QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888)); if (pixmap.isNull()) { QMessageBox::information(this, "提示", "转换为漫画后的文件打开失败!"); return; } // 获取 label_image2 的尺寸 int labelWidth2 = ui->label_image2->width(); int labelHeight2 = ui->label_image2->height(); // 将 QPixmap 进行缩放 QPixmap scaledPixmap = pixmap.scaled(labelWidth2, labelHeight2, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 将缩放后的图像设置到 label_image2 中 ui->label_image2->setPixmap(scaledPixmap); ui->label_image2->setScaledContents(false); ui->label_image2->setAlignment(Qt::AlignCenter); } void MainWindow::on_toDmButton_clicked() { if (img_input.empty()) { QMessageBox::information(this, "提示", "尚未加载图片,无法进行平滑操作!"); return; } cv::Mat smoothedImg; cv::GaussianBlur(img_input, smoothedImg, cv::Size(5, 5), 0); // 使用高斯模糊进行平滑 cv::Mat temp; cv::cvtColor(smoothedImg, temp, cv::COLOR_BGR2RGB); // 直接将平滑后的图像转换为 QPixmap 并显示在 label_image2 中 QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888)); if (pixmap.isNull()) { QMessageBox::information(this, "提示", "平滑后的文件打开失败!"); return; } // 获取 label_image2 的尺寸 int labelWidth2 = ui->label_image2->width(); int labelHeight2 = ui->label_image2->height(); // 将 QPixmap 进行缩放 QPixmap scaledPixmap = pixmap.scaled(labelWidth2, labelHeight2, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 将缩放后的图像设置到 label_image2 中 ui->label_image2->setPixmap(scaledPixmap); ui->label_image2->setScaledContents(false); ui->label_image2->setAlignment(Qt::AlignCenter); } void MainWindow::on_clearButton_clicked() { ui->label_image->clear(); ui->label_image2->clear(); tempRH.release(); img_input.release(); }
MyFirstQT.h
#pragma once #include <QMainWindow> #include <QPushButton> #include <QLabel> #include <opencv2/opencv.hpp> namespace Ui { class MainWindow; class MyFirstQTClass; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_loadImageButton_clicked(); void on_sharpenImageButton_clicked(); void on_histogramImageButton_clicked(); void on_SaveButton_clicked(); void on_clearButton_clicked(); void on_autoLevelsButton_clicked(); void on_toDmButton_clicked(); private: Ui::MyFirstQTClass *ui; cv::Mat image; };
标签:clicked,QT,temp,label,Opencv,ui,vs2022,image2,cv From: https://www.cnblogs.com/kfcalf/p/18326157