• 【OpenCV + Qt】 图像处理操作


    目录

    一:目标

    二:使用Qt界面

    三:图像处理操作完整代码


    一:目标

    Qt界面实现 雪花屏 高斯模糊 中值滤波 毛玻璃 灰度化 XY方向模糊 双边模糊 腐蚀 [图像处理操作]

    要求左边原图,右边效果图

    结果展示如下:[图像处理实现有点多,就不一个一个地展示了,各别展示如下]

    雪花屏

    毛玻璃

    灰度化处理

    二:使用Qt界面

    使用到Qt中的UI设计界面

    设计好界面之后最好先保存

    对每一个按钮设计槽函数

    三:图像处理操作完整代码

    难点在于:Qt是QImage而OpenCV是Mat,需要Mat转QImage才能在Qt界面中进行图片的正常显示

    头文件中导入opencv包 

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. using namespace cv;
    6. QT_BEGIN_NAMESPACE
    7. namespace Ui {class Widget;}
    8. QT_END_NAMESPACE
    9. class Widget : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. // Widget(QWidget *parent = nullptr);
    14. explicit Widget(QWidget *parent = 0);
    15. ~Widget();
    16. private slots:
    17. void on_pushButton_clicked();//雪花屏
    18. void on_pushButton_2_clicked();//高斯模糊
    19. void on_pushButton_3_clicked();//中值滤波
    20. void on_pushButton_4_clicked();//毛玻璃
    21. void on_pushButton_5_clicked();//灰度化
    22. void on_pushButton_6_clicked();//XY方向模糊
    23. void on_pushButton_7_clicked();//双边模糊
    24. void on_pushButton_8_clicked();//腐蚀
    25. private:
    26. Ui::Widget *ui;
    27. Mat srcimage;//原图
    28. };
    29. #endif // WIDGET_H

    源文件中实现图像处理方法 

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. }
    9. Widget::~Widget()
    10. {
    11. delete ui;
    12. }
    13. //雪花屏
    14. void Widget::on_pushButton_clicked()
    15. {
    16. //读取原始图片
    17. Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    18. //Mat转QImage 颜色
    19. cvtColor(srcimage,srcimage,CV_BGR2RGB);
    20. //Mat转QImage 像素 oldlabel放置原图
    21. QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    22. disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    23. ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
    24. //像素二维矩阵函数
    25. int rows = srcimage.rows;
    26. //像素二维矩阵列数
    27. int cols = srcimage.cols * srcimage.channels();
    28. for(int i=0;i
    29. {
    30. uchar * data = srcimage.ptr(i);
    31. for(int j=0;j
    32. {
    33. //雪花屏特效
    34. int q = rand()%cols;
    35. data[q]=155;//某些通道随机改成155
    36. }
    37. }
    38. //Mat转QImage 像素 newlabel放置图像处理后图片
    39. QImage disimage2 = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    40. disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    41. ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
    42. }
    43. //高斯模糊
    44. void Widget::on_pushButton_2_clicked()
    45. {
    46. //读取原始图片
    47. Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    48. //Mat转QImage 颜色
    49. cvtColor(srcimage,srcimage,CV_BGR2RGB);
    50. //Mat转QImage 像素 oldlabel放置原图
    51. QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    52. Mat retimage;
    53. //高斯模糊
    54. GaussianBlur(srcimage,retimage,Size(5,5),0,0);
    55. //Mat转QImage 像素 newlabel放置图像处理后图片
    56. QImage disimage2 = QImage(retimage.data,retimage.cols,retimage.rows,retimage.cols*retimage.channels(),QImage::Format_RGB888);
    57. disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    58. ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
    59. }
    60. //中值滤波
    61. void Widget::on_pushButton_3_clicked()
    62. {
    63. //读取原始图片
    64. Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    65. //Mat转QImage 颜色
    66. cvtColor(srcimage,srcimage,CV_BGR2RGB);
    67. //Mat转QImage 像素 oldlabel放置原图
    68. QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    69. disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    70. ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
    71. Mat retumage;
    72. //中值滤波
    73. medianBlur(srcimage,retumage,5);
    74. //Mat转QImage 像素 newlabel放置图像处理后图片
    75. QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    76. disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    77. ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
    78. }
    79. //毛玻璃
    80. void Widget::on_pushButton_4_clicked()
    81. {
    82. //读取原始图片
    83. Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    84. //Mat转QImage 颜色
    85. cvtColor(srcimage,srcimage,CV_BGR2RGB);
    86. //Mat转QImage 像素 oldlabel放置原图
    87. QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    88. disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    89. ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
    90. //毛玻璃
    91. RNG rng;
    92. int random;
    93. int num = 5;
    94. for(int i=0;i
    95. {
    96. for(int j=0;j
    97. {
    98. //通过rng返回0-15随机数
    99. random = rng.uniform(0,num);
    100. srcimage.at(i,j)[0] = srcimage.at(i+random,j+random)[0];
    101. srcimage.at(i,j)[1] = srcimage.at(i+random,j+random)[1];
    102. srcimage.at(i,j)[2] = srcimage.at(i+random,j+random)[2];
    103. }
    104. }
    105. //Mat转QImage 像素 newlabel放置图像处理后图片
    106. QImage disimage2 = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    107. disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    108. ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
    109. }
    110. //灰度化
    111. void Widget::on_pushButton_5_clicked()
    112. {
    113. //读取原始图片
    114. Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    115. //Mat转QImage 颜色
    116. cvtColor(srcimage,srcimage,CV_BGR2RGB);
    117. //Mat转QImage 像素 oldlabel放置原图
    118. QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    119. disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    120. ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
    121. Mat retumage;
    122. //灰度处理 灰度是单通道8位 QImage是24位三通道
    123. cvtColor(srcimage,retumage,CV_BGR2GRAY);
    124. cvtColor(retumage,retumage,CV_GRAY2BGR);
    125. //Mat转QImage 像素 newlabel放置图像处理后图片
    126. QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    127. disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    128. ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
    129. }
    130. //XY方向模糊
    131. void Widget::on_pushButton_6_clicked()
    132. {
    133. //读取原始图片
    134. Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    135. //Mat转QImage 颜色
    136. cvtColor(srcimage,srcimage,CV_BGR2RGB);
    137. //Mat转QImage 像素 oldlabel放置原图
    138. QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    139. disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    140. ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
    141. Mat retumage;
    142. //xy轴模糊
    143. blur(srcimage,retumage,Size(10,10));
    144. //Mat转QImage 像素 newlabel放置图像处理后图片
    145. QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    146. disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    147. ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
    148. }
    149. //双边模糊
    150. void Widget::on_pushButton_7_clicked()
    151. {
    152. //读取原始图片
    153. Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    154. //Mat转QImage 颜色
    155. cvtColor(srcimage,srcimage,CV_BGR2RGB);
    156. //Mat转QImage 像素 oldlabel放置原图
    157. QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    158. disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    159. ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
    160. Mat retumage;
    161. //双倍模糊
    162. cv::bilateralFilter(srcimage,retumage,15,120,10,4);
    163. //Mat转QImage 像素 newlabel放置图像处理后图片
    164. QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    165. disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    166. ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
    167. }
    168. //腐蚀
    169. void Widget::on_pushButton_8_clicked()
    170. {
    171. //读取原始图片
    172. Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    173. //Mat转QImage 颜色
    174. cvtColor(srcimage,srcimage,CV_BGR2RGB);
    175. //Mat转QImage 像素 oldlabel放置原图
    176. QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    177. disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    178. ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
    179. Mat retumage;
    180. //腐蚀
    181. Mat element = cv::getStructuringElement(MORPH_RECT,Size(5,5));
    182. cv::erode(srcimage,retumage,element);
    183. //Mat转QImage 像素 newlabel放置图像处理后图片
    184. QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    185. disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    186. ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
    187. }

    主入口Qt窗口显示 

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }
  • 相关阅读:
    Flume配置1——基础案例
    3D-2D PNP
    收录查询,搜狗收录查询入口
    体系班第十三节
    快速上手分布式版本控制工具Git
    数据湖仓一体(五)安装spark
    Flink之Watermark水印、水位线
    FPGA设计时序约束八、others类约束之Set_Case_Analysis
    【读书笔记】【More Effective C++】异常(Exceptions)
    通信原理 | 滤波器:滤波器的概念、理想滤波器和实际滤波器
  • 原文地址:https://blog.csdn.net/m0_56051805/article/details/126132310