vs2022 QT Opencv用到的一些代码

 

 1 #pragma once
 2  
 3 #include <QMainWindow>
 4 #include <QPushButton>
 5 #include <QLabel>
 6 #include <opencv2/opencv.hpp>
 7  
 8 namespace Ui {
 9 class MainWindow;
10 class MyFirstQTClass;
11 }
12  
13 class MainWindow : public QMainWindow
14 {
15     Q_OBJECT
16  
17 public:
18     explicit MainWindow(QWidget *parent = nullptr);
19     ~MainWindow();
20  
21 private slots:
22     void on_loadImageButton_clicked();
23     void on_sharpenImageButton_clicked();
24     void on_histogramImageButton_clicked();
25     void on_SaveButton_clicked();
26     void on_clearButton_clicked();
27     void on_autoLevelsButton_clicked();
28     void on_toDmButton_clicked();
29  
30      
31      
32  
33 private:
34     Ui::MyFirstQTClass *ui;
35     cv::Mat image;
36  
37 };

 

MyFirstQT.cpp

  1 #include "MyFirstQT.h"
  2 #include "ui_MyFirstQT.h"
  3 #include <QFileDialog>
  4 #include <QMessageBox>
  5 #include <QPixmap>
  6 #include <opencv2/opencv.hpp>
  7 #include <QDebug> 
  8 #include <opencv2/imgproc.hpp>
  9 
 10 using namespace cv;
 11 using namespace std;
 12 
 13 MainWindow::MainWindow(QWidget *parent)
 14     : QMainWindow(parent)
 15   , ui(new Ui::MyFirstQTClass)
 16 {
 17     ui->setupUi(this);
 18     //connect(ui->loadImageButton, &QPushButton::clicked, this, &MainWindow::on_loadImageButton_clicked);//这句如果不注释,打开文件窗口会跳2次
 19     //connect(ui->sharpenImageButton, &QPushButton::clicked, this, &MainWindow::on_sharpenImageButton_clicked);
 20 }
 21 
 22 MainWindow::~MainWindow()
 23 {
 24     delete ui;
 25 }
 26 
 27 cv::Mat img_input;
 28 
 29 cv::Mat tempRH;//锐化
 30 void MainWindow::on_loadImageButton_clicked()
 31 {
 32 
 33     QString filename = QFileDialog::getOpenFileName(this, "打开图像文件", "C:/Users", "Image Files (*.bmp;*.png;*.jpg)");
 34 
 35     if (filename.isEmpty()) {
 36         QMessageBox::information(this, "提示", "文件打开失败1!");
 37         return;
 38     }
 39 
 40     img_input = cv::imread(filename.toLocal8Bit().toStdString());
 41 
 42     if (img_input.empty()) {
 43 
 44         QMessageBox::information(this, "提示", "文件打开失败2!");
 45         return;
 46     }
 47 
 48     cv::Mat temp;
 49     cv::cvtColor(img_input, temp, cv::COLOR_BGR2RGB);
 50 
 51     // 直接将文件名传递给 QPixmap 进行加载
 52     QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888));
 53 
 54     if (pixmap.isNull()) {
 55         QMessageBox::information(this, "提示", "文件打开失败2!");
 56         return;
 57     }
 58 
 59     // 获取 label 的尺寸
 60     int labelWidth = ui->label_image->width();
 61     int labelHeight = ui->label_image->height();
 62 
 63     // 将 QPixmap 进行缩放
 64     QPixmap scaledPixmap = pixmap.scaled(labelWidth, labelHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
 65 
 66     // 将缩放后的图像设置到 label 中
 67     ui->label_image->setPixmap(scaledPixmap);
 68     ui->label_image->setScaledContents(false); 
 69     ui->label_image->setAlignment(Qt::AlignCenter); 
 70 
 71 
 72 
 73 }
 74 
 75 void MainWindow::on_sharpenImageButton_clicked()
 76 {
 77     // 假设之前加载的图像存储在成员变量 img_input 中
 78     if (img_input.empty()) {
 79         QMessageBox::information(this, "提示", "尚未加载图片,无法进行锐化操作!");
 80         return;
 81     }
 82 
 83     // 定义锐化核
 84     cv::Mat kernel = (cv::Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
 85 
 86     cv::Mat img_sharpened;
 87     cv::filter2D(img_input, img_sharpened, -1, kernel);
 88 
 89     cv::Mat temp;
 90     cv::cvtColor(img_sharpened, temp, cv::COLOR_BGR2RGB);
 91 
 92     tempRH = img_sharpened;
 93 
 94     // 直接将锐化后的图像转换为 QPixmap 并显示在 label_image2 中
 95     QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888));
 96 
 97     if (pixmap.isNull()) {
 98         QMessageBox::information(this, "提示", "锐化后的文件打开失败!");
 99         return;
100     }
101 
102     // 获取 label_image2 的尺寸
103     int labelWidth2 = ui->label_image2->width();
104     int labelHeight2 = ui->label_image2->height();
105 
106     // 将 QPixmap 进行缩放
107     QPixmap scaledPixmap = pixmap.scaled(labelWidth2, labelHeight2, Qt::KeepAspectRatio, Qt::SmoothTransformation);
108 
109     // 将缩放后的图像设置到 label_image2 中
110     ui->label_image2->setPixmap(scaledPixmap);
111     ui->label_image2->setScaledContents(false); 
112     ui->label_image2->setAlignment(Qt::AlignCenter); 
113 }
114 
115 void MainWindow::on_histogramImageButton_clicked()
116 {
117 
118     if (img_input.empty()) {
119     QMessageBox::information(this, "提示", "尚未加载图片,无法获取直方图!");
120     return;
121     }
122 
123     cv::Mat grayImg;
124     cv::cvtColor(img_input, grayImg, cv::COLOR_BGR2GRAY);
125 
126     // 计算直方图
127     Mat hist;
128     int histSize = 256;
129     float range[] = { 0, 256 };
130     const float* histRange = { range };
131     calcHist(&grayImg, 1, 0, Mat(), hist, 1, &histSize, &histRange);
132 
133     // 绘制直方图
134     int hist_w = 500;  // 缩小宽度
135     int hist_h = 400;
136     int bin_w = cvRound((double)hist_w / histSize);
137 
138     Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
139     normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
140 
141     for (int i = 1; i < histSize; i++) {
142         line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(hist.at<float>(i - 1))),
143              Point(bin_w * i, hist_h - cvRound(hist.at<float>(i))),
144              Scalar(255, 255, 255), 2);
145     }
146 
147     // 绘制横坐标
148     for (int x = 0; x <= 255; x += 50) {
149         line(histImage, Point(bin_w * x, hist_h), Point(bin_w * x, hist_h - 10), Scalar(255, 255, 255), 1);
150         putText(histImage, QString::number(x).toStdString(), Point(bin_w * x - 10, hist_h + 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));
151     }
152     line(histImage, Point(bin_w * 255, hist_h), Point(bin_w * 255, hist_h - 10), Scalar(255, 255, 255), 1);  // 255 刻度线
153     putText(histImage, "255", Point(bin_w * 255 - 10, hist_h + 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));
154     line(histImage, Point(hist_w, hist_h), Point(hist_w, hist_h - 10), Scalar(255, 255, 255), 1);  // 超出 255 的刻度线
155     putText(histImage, "300", Point(hist_w - 20, hist_h + 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));
156 
157     // 绘制纵坐标
158     int y_interval = 250;
159     for (int y = 0; y <= 2500; y += y_interval) {
160         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);
161         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));
162     }
163 
164     // 将绘制的直方图转换为 QImage 并显示在 label_image2 中
165     QImage qImg((const unsigned char*)(histImage.data), histImage.cols, histImage.rows, histImage.step, QImage::Format_RGB888);
166     QPixmap pixmap = QPixmap::fromImage(qImg);
167     ui->label_image2->setPixmap(pixmap);
168 
169 }//直方图示范 https://blog.csdn.net/u010140856/article/details/79256015
170 
171 void MainWindow::on_SaveButton_clicked()
172 {
173      if (tempRH.empty()) {
174         QMessageBox::information(this, "提示", "尚未进行锐化操作,无法保存!");
175         return;
176     }
177 
178 
179 
180     QString savePath = QFileDialog::getSaveFileName(this, "保存锐化后的图片", "", "Image Files (*.bmp;*.png;*.jpg;*.jpeg;*.tif;*.tiff)");
181     if (savePath.isEmpty()) {
182         return;
183     }
184 
185     cv::imwrite(savePath.toLocal8Bit().toStdString(), tempRH);  // 保存锐化后的图片,保持原始尺寸
186 }
187 
188 //自动色阶
189 
190 void MainWindow::on_autoLevelsButton_clicked()
191 {
192 
193     if (img_input.empty()) {
194         QMessageBox::information(this, "提示", "尚未加载图片,无法进行转换为漫画操作!");
195         return;
196     }
197 
198     cv::Mat grayImg;
199     cv::cvtColor(img_input, grayImg, cv::COLOR_BGR2GRAY);
200 
201     // 双边滤波
202     cv::Mat bilateralFilteredImg;
203     cv::bilateralFilter(grayImg, bilateralFilteredImg, 9, 75, 75);
204 
205     // 自适应阈值
206     cv::Mat adaptiveThresholdImg;
207     cv::adaptiveThreshold(bilateralFilteredImg, adaptiveThresholdImg, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 9, 5);
208 
209     cv::Mat temp;
210     cv::cvtColor(adaptiveThresholdImg, temp, cv::COLOR_GRAY2RGB);
211 
212     // 直接将处理后的图像转换为 QPixmap 并显示在 label_image2 中
213     QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888));
214 
215     if (pixmap.isNull()) {
216         QMessageBox::information(this, "提示", "转换为漫画后的文件打开失败!");
217         return;
218     }
219 
220     // 获取 label_image2 的尺寸
221     int labelWidth2 = ui->label_image2->width();
222     int labelHeight2 = ui->label_image2->height();
223 
224     // 将 QPixmap 进行缩放
225     QPixmap scaledPixmap = pixmap.scaled(labelWidth2, labelHeight2, Qt::KeepAspectRatio, Qt::SmoothTransformation);
226 
227     // 将缩放后的图像设置到 label_image2 中
228     ui->label_image2->setPixmap(scaledPixmap);
229     ui->label_image2->setScaledContents(false);
230     ui->label_image2->setAlignment(Qt::AlignCenter);
231 }
232 
233 void MainWindow::on_toDmButton_clicked()
234 {
235     if (img_input.empty())
236     {
237         QMessageBox::information(this, "提示", "尚未加载图片,无法进行平滑操作!");
238         return;
239     }
240 
241     cv::Mat smoothedImg;
242     cv::GaussianBlur(img_input, smoothedImg, cv::Size(5, 5), 0);  // 使用高斯模糊进行平滑
243 
244     cv::Mat temp;
245     cv::cvtColor(smoothedImg, temp, cv::COLOR_BGR2RGB);
246 
247     // 直接将平滑后的图像转换为 QPixmap 并显示在 label_image2 中
248     QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888));
249 
250     if (pixmap.isNull())
251     {
252         QMessageBox::information(this, "提示", "平滑后的文件打开失败!");
253         return;
254     }
255 
256     // 获取 label_image2 的尺寸
257     int labelWidth2 = ui->label_image2->width();
258     int labelHeight2 = ui->label_image2->height();
259 
260     // 将 QPixmap 进行缩放
261     QPixmap scaledPixmap = pixmap.scaled(labelWidth2, labelHeight2, Qt::KeepAspectRatio, Qt::SmoothTransformation);
262 
263     // 将缩放后的图像设置到 label_image2 中
264     ui->label_image2->setPixmap(scaledPixmap);
265     ui->label_image2->setScaledContents(false);
266     ui->label_image2->setAlignment(Qt::AlignCenter);
267 
268 }
269 
270 
271 
272 void MainWindow::on_clearButton_clicked()
273 {
274     ui->label_image->clear();
275     ui->label_image2->clear();
276     tempRH.release();
277     img_input.release();
278 }

 

posted @ 2024-07-26 20:02  莫莫大人  阅读(20)  评论(0编辑  收藏  举报