首頁 > 軟體

OpenCV+Qt實現影象處理操作工具的範例程式碼

2022-08-03 18:04:31

一、目標

Qt介面實現 雪花屏 高斯模糊 中值濾波 毛玻璃 灰度化 XY方向模糊 雙邊模糊 腐蝕 [影象處理操作]

要求左邊原圖,右邊效果圖

結果展示如下:[影象處理實現有點多,就不一個一個地展示了,各別展示如下]

雪花屏

毛玻璃

灰度化處理

二、使用Qt介面

使用到Qt中的UI設計介面

設計好介面之後最好先儲存

對每一個按鈕設計槽函數

三、影象處理操作完整程式碼

難點在於:Qt是QImage而OpenCV是Mat,需要Mat轉QImage才能在Qt介面中進行圖片的正常顯示

標頭檔案中匯入opencv包 

#ifndef WIDGET_H
#define WIDGET_H
#include<opencv2/opencv.hpp>
#include <QWidget>
using namespace cv;
 
QT_BEGIN_NAMESPACE
namespace Ui {class Widget;}
QT_END_NAMESPACE
 
class Widget : public QWidget
{
    Q_OBJECT
public:
//  Widget(QWidget *parent = nullptr);
    explicit Widget(QWidget *parent = 0);
    ~Widget();
 
private slots:
    void on_pushButton_clicked();//雪花屏
    void on_pushButton_2_clicked();//高斯模糊
    void on_pushButton_3_clicked();//中值濾波
    void on_pushButton_4_clicked();//毛玻璃
    void on_pushButton_5_clicked();//灰度化
    void on_pushButton_6_clicked();//XY方向模糊
    void on_pushButton_7_clicked();//雙邊模糊
    void on_pushButton_8_clicked();//腐蝕
private:
    Ui::Widget *ui;
    Mat srcimage;//原圖
};
 
#endif // WIDGET_H

原始檔中實現影象處理方法 

#include "widget.h"
#include "ui_widget.h"
 
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}
 
Widget::~Widget()
{
    delete ui;
}
 
//雪花屏
void Widget::on_pushButton_clicked()
{
    //讀取原始圖片
    Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    //Mat轉QImage 顏色
    cvtColor(srcimage,srcimage,CV_BGR2RGB);
    //Mat轉QImage 畫素   oldlabel放置原圖
    QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
 
    //畫素二維矩陣函數
    int rows = srcimage.rows;
    //畫素二維矩陣列數
    int cols = srcimage.cols * srcimage.channels();
    for(int i=0;i<rows;i++)
    {
        uchar * data = srcimage.ptr<uchar>(i);
        for(int j=0;j<cols;j++)
        {
            //雪花屏特效
            int q = rand()%cols;
            data[q]=155;//某些通道隨機改成155
        }
    }
    //Mat轉QImage 畫素   newlabel放置影象處理後圖片
    QImage disimage2 = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
}
 
//高斯模糊
void Widget::on_pushButton_2_clicked()
{
    //讀取原始圖片
    Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    //Mat轉QImage 顏色
    cvtColor(srcimage,srcimage,CV_BGR2RGB);
    //Mat轉QImage 畫素   oldlabel放置原圖
    QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
 
    Mat retimage;
    //高斯模糊
    GaussianBlur(srcimage,retimage,Size(5,5),0,0);
 
    //Mat轉QImage 畫素   newlabel放置影象處理後圖片
    QImage disimage2 = QImage(retimage.data,retimage.cols,retimage.rows,retimage.cols*retimage.channels(),QImage::Format_RGB888);
    disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
}
 
//中值濾波
void Widget::on_pushButton_3_clicked()
{
    //讀取原始圖片
    Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    //Mat轉QImage 顏色
    cvtColor(srcimage,srcimage,CV_BGR2RGB);
    //Mat轉QImage 畫素   oldlabel放置原圖
    QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
 
    Mat retumage;
    //中值濾波
    medianBlur(srcimage,retumage,5);
 
    //Mat轉QImage 畫素   newlabel放置影象處理後圖片
    QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
}
 
//毛玻璃
void Widget::on_pushButton_4_clicked()
{
    //讀取原始圖片
    Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    //Mat轉QImage 顏色
    cvtColor(srcimage,srcimage,CV_BGR2RGB);
    //Mat轉QImage 畫素   oldlabel放置原圖
    QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
 
    //毛玻璃
    RNG rng;
    int random;
    int num = 5;
    for(int i=0;i<srcimage.rows -num;i++)
    {
        for(int j=0;j<srcimage.cols -num;j++)
        {
            //通過rng返回0-15亂數
            random = rng.uniform(0,num);
            srcimage.at<Vec3b>(i,j)[0] = srcimage.at<Vec3b>(i+random,j+random)[0];
            srcimage.at<Vec3b>(i,j)[1] = srcimage.at<Vec3b>(i+random,j+random)[1];
            srcimage.at<Vec3b>(i,j)[2] = srcimage.at<Vec3b>(i+random,j+random)[2];
        }
    }
 
    //Mat轉QImage 畫素   newlabel放置影象處理後圖片
    QImage disimage2 = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
}
 
//灰度化
void Widget::on_pushButton_5_clicked()
{
    //讀取原始圖片
    Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    //Mat轉QImage 顏色
    cvtColor(srcimage,srcimage,CV_BGR2RGB);
    //Mat轉QImage 畫素   oldlabel放置原圖
    QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
 
    Mat retumage;
    //灰度處理 灰度是單通道8位元 QImage是24位元三通道
    cvtColor(srcimage,retumage,CV_BGR2GRAY);
    cvtColor(retumage,retumage,CV_GRAY2BGR);
 
    //Mat轉QImage 畫素   newlabel放置影象處理後圖片
    QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
}
 
//XY方向模糊
void Widget::on_pushButton_6_clicked()
{
    //讀取原始圖片
    Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    //Mat轉QImage 顏色
    cvtColor(srcimage,srcimage,CV_BGR2RGB);
    //Mat轉QImage 畫素   oldlabel放置原圖
    QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
 
    Mat retumage;
    //xy軸模糊
    blur(srcimage,retumage,Size(10,10));
 
    //Mat轉QImage 畫素   newlabel放置影象處理後圖片
    QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
}
 
//雙邊模糊
void Widget::on_pushButton_7_clicked()
{
    //讀取原始圖片
    Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    //Mat轉QImage 顏色
    cvtColor(srcimage,srcimage,CV_BGR2RGB);
    //Mat轉QImage 畫素   oldlabel放置原圖
    QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
 
    Mat retumage;
    //雙倍模糊
    cv::bilateralFilter(srcimage,retumage,15,120,10,4);
 
    //Mat轉QImage 畫素   newlabel放置影象處理後圖片
    QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
}
 
//腐蝕
void Widget::on_pushButton_8_clicked()
{
    //讀取原始圖片
    Mat srcimage = imread("D:/000imageopencv/efls111.jpg");
    //Mat轉QImage 顏色
    cvtColor(srcimage,srcimage,CV_BGR2RGB);
    //Mat轉QImage 畫素   oldlabel放置原圖
    QImage disimage = QImage(srcimage.data,srcimage.cols,srcimage.rows,srcimage.cols*srcimage.channels(),QImage::Format_RGB888);
    disimage = disimage.scaled(ui->oldlabel->width(),ui->oldlabel->height());
    ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
 
    Mat retumage;
    //腐蝕
    Mat element = cv::getStructuringElement(MORPH_RECT,Size(5,5));
    cv::erode(srcimage,retumage,element);
 
    //Mat轉QImage 畫素   newlabel放置影象處理後圖片
    QImage disimage2 = QImage(retumage.data,retumage.cols,retumage.rows,retumage.cols*retumage.channels(),QImage::Format_RGB888);
    disimage2 = disimage2.scaled(ui->newlabel->width(),ui->newlabel->height());
    ui->newlabel->setPixmap(QPixmap::fromImage(disimage2));
}

主入口Qt視窗顯示 

#include "widget.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

以上就是OpenCV+Qt實現影象處理操作工具的範例程式碼的詳細內容,更多關於OpenCV Qt影象處理的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com