• vs+opencv+QT调试程序


    2021-09-28vs+opencv+QT简单的图像处理工程_opencv 用qt还是vs_二两山栀子的博客-CSDN博客

    【vs+opencv+Qt搭建简单的图像处理界面】https://www.bilibili.com/video/BV16T411j7XQ?vd_source=0aeb782d0b9c2e6b0e0cdea3e2121eba

    调试过程一直出现这种问题,后来改DEBUG为release就可以了

    mainwindow.hpp

    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. using namespace cv;
    11. namespace Ui {
    12. class MainWindow;
    13. class mainwindowClass;
    14. }
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18. public:
    19. explicit MainWindow(QWidget* parent = 0);
    20. ~MainWindow();
    21. private slots:
    22. void on_pushButton_clicked();
    23. void on_pushButton_2_clicked();
    24. void on_pushButton_3_clicked();
    25. void on_pushButton_4_clicked();
    26. private:
    27. QImage MatToQImage(const cv::Mat& mat); // MAT类型 转 QImage类型
    28. void display_MatInQT(QLabel* label, cv::Mat mat); // MAT对象 QT显示
    29. private:
    30. Ui::mainwindowClass* ui;
    31. Mat image;
    32. Mat mat_Gaussian;
    33. Mat gray;
    34. };
    35. #endif // MAINWINDOW_H#pragma once

    mainwindow.cpp

    1. #include "mainwindow.hpp"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4. MainWindow::MainWindow(QWidget* parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::mainwindowClass)
    7. {
    8. ui->setupUi(this);
    9. ui->pushButton_2->setEnabled(false);
    10. ui->pushButton_3->setEnabled(false);
    11. ui->pushButton_4->setEnabled(false);
    12. }
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17. void MainWindow::on_pushButton_clicked()
    18. {
    19. //调用窗口打开文件
    20. ui->label->clear();
    21. ui->label_1->clear();
    22. QString filename = QFileDialog::getOpenFileName(this,
    23. tr("open image"),
    24. ".",
    25. tr("Image file(*.png *.jpg *.bmp)"));
    26. image = imread(filename.toLocal8Bit().data());
    27. if (image.data) {
    28. ui->pushButton_2->setEnabled(true);
    29. ui->pushButton_3->setEnabled(true);
    30. ui->pushButton_4->setEnabled(true);
    31. // 通过 lable 方式显示图片
    32. display_MatInQT(ui->label, image);
    33. }
    34. else
    35. {
    36. QMessageBox::information(this, tr("提示"), tr("未成功载入图片!"), QMessageBox::Ok);
    37. }
    38. }
    39. void MainWindow::on_pushButton_2_clicked()
    40. {
    41. ui->label_1->clear();
    42. if (image.data)
    43. {
    44. // 高斯模糊
    45. GaussianBlur(image, mat_Gaussian, Size(29, 29), 0, 0);
    46. display_MatInQT(ui->label_1, mat_Gaussian);
    47. }
    48. else
    49. {
    50. QMessageBox::information(this, tr("提示"), tr("未成功载入图片!"), QMessageBox::Ok);
    51. }
    52. }
    53. void MainWindow::on_pushButton_3_clicked()
    54. {
    55. ui->label_1->clear();
    56. if (image.data)
    57. {
    58. // 灰度化
    59. cvtColor(image, gray, COLOR_BGR2GRAY);
    60. display_MatInQT(ui->label_1, gray);
    61. }
    62. else
    63. {
    64. QMessageBox::information(this, tr("提示"), tr("未成功载入图片!"), QMessageBox::Ok);
    65. }
    66. }
    67. void MainWindow::on_pushButton_4_clicked()
    68. {
    69. ui->label_1->clear();
    70. if (image.data)
    71. {
    72. // 边缘检测
    73. Canny(image, gray, 150, 100, 3);
    74. display_MatInQT(ui->label_1, gray);
    75. }
    76. else
    77. {
    78. QMessageBox::information(this, tr("提示"), tr("未成功载入图片!"), QMessageBox::Ok);
    79. }
    80. }
    81. QImage MainWindow::MatToQImage(const cv::Mat& mat)
    82. {
    83. // 8-bits unsigned, NO. OF CHANNELS = 1
    84. if (mat.type() == CV_8UC1)
    85. {
    86. QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
    87. // Set the color table (used to translate colour indexes to qRgb values)
    88. image.setColorCount(256);
    89. for (int i = 0; i < 256; i++)
    90. {
    91. image.setColor(i, qRgb(i, i, i));
    92. }
    93. // Copy input Mat
    94. uchar* pSrc = mat.data;
    95. for (int row = 0; row < mat.rows; row++)
    96. {
    97. uchar* pDest = image.scanLine(row);
    98. memcpy(pDest, pSrc, mat.cols);
    99. pSrc += mat.step;
    100. }
    101. return image;
    102. }
    103. // 8-bits unsigned, NO. OF CHANNELS = 3
    104. else if (mat.type() == CV_8UC3)
    105. {
    106. // Copy input Mat
    107. const uchar* pSrc = (const uchar*)mat.data;
    108. // Create QImage with same dimensions as input Mat
    109. QImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_RGB888);
    110. return image.rgbSwapped();
    111. }
    112. else if (mat.type() == CV_8UC4)
    113. {
    114. //qDebug() << "CV_8UC4";
    115. // Copy input Mat
    116. const uchar* pSrc = (const uchar*)mat.data;
    117. // Create QImage with same dimensions as input Mat
    118. QImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_ARGB32);
    119. return image.copy();
    120. }
    121. else
    122. {
    123. //qDebug() << "ERROR: Mat could not be converted to QImage.";
    124. return QImage();
    125. }
    126. }
    127. //
    128. void MainWindow::display_MatInQT(QLabel* label, Mat mat)
    129. {
    130. label->setPixmap(QPixmap::fromImage(MatToQImage(mat)).scaled(label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    131. }

  • 相关阅读:
    jQuery:jQuery笔记1
    详解Python的pyyaml模块
    基于JavaWeb和mysql实现校园订餐前后台管理系统(源码+数据库)
    文本分词2.0
    Dart的基本数据类型详解 int double String bool List Maps
    创建asp.net core mvc项目
    JavaAPI常用类01
    运筹模型的变量的对称性案例及分析
    腾讯起诉vivo不正当竞争;谷歌俄罗斯分公司申请破产,官方称“银行账户被俄罗斯没收”;Opera 87发布|极客头条
    理解记忆相关
  • 原文地址:https://blog.csdn.net/weixin_45295333/article/details/132701896