欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV+Qt實(shí)現(xiàn)圖像處理操作

 更新時(shí)間:2022年08月05日 15:28:01   作者:我今年十六歲  
這篇文章主要為大家詳細(xì)介紹了OpenCV+Qt實(shí)現(xiàn)圖像處理操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了OpenCV+Qt實(shí)現(xiàn)圖像處理操作的具體代碼,供大家參考,具體內(nèi)容如下

一、目標(biāo)

Qt界面實(shí)現(xiàn) 雪花屏 高斯模糊 中值濾波 毛玻璃 灰度化 XY方向模糊 雙邊模糊 腐蝕 [圖像處理操作]

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

結(jié)果展示如下:[圖像處理實(shí)現(xiàn)有點(diǎn)多,就不一個(gè)一個(gè)地展示了,各別展示如下]

雪花屏

毛玻璃

灰度化處理

二、使用Qt界面

使用到Qt中的UI設(shè)計(jì)界面

設(shè)計(jì)好界面之后最好先保存

對(duì)每一個(gè)按鈕設(shè)計(jì)槽函數(shù)

三、圖像處理操作完整代碼

難點(diǎn)在于:Qt是QImage而OpenCV是Mat,需要Mat轉(zhuǎn)QImage才能在Qt界面中進(jìn)行圖片的正常顯示

頭文件中導(dǎo)入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

源文件中實(shí)現(xiàn)圖像處理方法 

#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轉(zhuǎn)QImage 顏色
? ? cvtColor(srcimage,srcimage,CV_BGR2RGB);
? ? //Mat轉(zhuǎn)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));
?
? ? //像素二維矩陣函數(shù)
? ? int rows = srcimage.rows;
? ? //像素二維矩陣列數(shù)
? ? 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;//某些通道隨機(jī)改成155
? ? ? ? }
? ? }
? ? //Mat轉(zhuǎn)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轉(zhuǎn)QImage 顏色
? ? cvtColor(srcimage,srcimage,CV_BGR2RGB);
? ? //Mat轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)QImage 顏色
? ? cvtColor(srcimage,srcimage,CV_BGR2RGB);
? ? //Mat轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)QImage 顏色
? ? cvtColor(srcimage,srcimage,CV_BGR2RGB);
? ? //Mat轉(zhuǎn)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++)
? ? ? ? {
? ? ? ? ? ? //通過(guò)rng返回0-15隨機(jī)數(shù)
? ? ? ? ? ? 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轉(zhuǎn)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轉(zhuǎn)QImage 顏色
? ? cvtColor(srcimage,srcimage,CV_BGR2RGB);
? ? //Mat轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)QImage 顏色
? ? cvtColor(srcimage,srcimage,CV_BGR2RGB);
? ? //Mat轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)QImage 顏色
? ? cvtColor(srcimage,srcimage,CV_BGR2RGB);
? ? //Mat轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)QImage 顏色
? ? cvtColor(srcimage,srcimage,CV_BGR2RGB);
? ? //Mat轉(zhuǎn)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轉(zhuǎn)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();
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用Matlab制作三子棋游戲的示例代碼

    利用Matlab制作三子棋游戲的示例代碼

    三子棋是一種民間傳統(tǒng)游戲,又叫九宮棋、圈圈叉叉、一條龍、井字棋等。將正方形對(duì)角線連起來(lái),相對(duì)兩邊依次擺上三個(gè)雙方棋子,只要將自己的三個(gè)棋子走成一條線,對(duì)方就算輸了。本文將用Matlab制作這一經(jīng)典游戲,感興趣的可以試一試
    2022-03-03
  • C++中的Reactor原理與實(shí)現(xiàn)

    C++中的Reactor原理與實(shí)現(xiàn)

    reactor設(shè)計(jì)模式是event-driven?architecture的一種實(shí)現(xiàn)方式,處理多個(gè)客戶端并發(fā)的向服務(wù)端請(qǐng)求服務(wù)的場(chǎng)景,每種服務(wù)在服務(wù)端可能由多個(gè)方法組成,這篇文章主要介紹了Reactor原理與實(shí)現(xiàn),需要的朋友可以參考下
    2022-07-07
  • C++實(shí)現(xiàn)哈夫曼樹(shù)的方法

    C++實(shí)現(xiàn)哈夫曼樹(shù)的方法

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)哈夫曼樹(shù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C++ accumulate函數(shù)詳細(xì)介紹和具體案例

    C++ accumulate函數(shù)詳細(xì)介紹和具體案例

    這篇文章主要介紹了C++ accumulate函數(shù)詳細(xì)介紹和具體案例,accumulate是numeric庫(kù)中的一個(gè)函數(shù),主要用來(lái)對(duì)指定范圍內(nèi)元素求和,但也自行指定一些其他操作,如范圍內(nèi)所有元素相乘、相除等
    2022-08-08
  • Qt實(shí)現(xiàn)無(wú)邊框窗口的示例代碼

    Qt實(shí)現(xiàn)無(wú)邊框窗口的示例代碼

    本文主要介紹了Qt實(shí)現(xiàn)無(wú)邊框窗口的示例代碼,主要包括鼠標(biāo)光標(biāo)在不同區(qū)域的變化,關(guān)閉拖動(dòng)窗口,窗口支持任意拉伸等,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • C語(yǔ)言函數(shù)指針與回調(diào)函數(shù)的實(shí)現(xiàn)

    C語(yǔ)言函數(shù)指針與回調(diào)函數(shù)的實(shí)現(xiàn)

    本文主要介紹了C語(yǔ)言函數(shù)指針與回調(diào)函數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • C語(yǔ)言模擬實(shí)現(xiàn)掃雷游戲

    C語(yǔ)言模擬實(shí)現(xiàn)掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言模擬實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • QT實(shí)現(xiàn)自定義Http客戶端的示例代碼

    QT實(shí)現(xiàn)自定義Http客戶端的示例代碼

    這篇文章主要為大家詳細(xì)介紹了QT如何實(shí)現(xiàn)自定義Http客戶端的,可以實(shí)現(xiàn)支持get,post請(qǐng)求方式;支持連接超時(shí)處理;支持網(wǎng)絡(luò)錯(cuò)誤,嘗試重連等功能,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-11-11
  • C++動(dòng)態(tài)分配和撤銷內(nèi)存以及結(jié)構(gòu)體類型作為函數(shù)參數(shù)

    C++動(dòng)態(tài)分配和撤銷內(nèi)存以及結(jié)構(gòu)體類型作為函數(shù)參數(shù)

    這篇文章主要介紹了C++動(dòng)態(tài)分配和撤銷內(nèi)存以及結(jié)構(gòu)體類型作為函數(shù)參數(shù),是C++入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • C語(yǔ)言實(shí)現(xiàn)掃雷游戲詳細(xì)流程

    C語(yǔ)言實(shí)現(xiàn)掃雷游戲詳細(xì)流程

    windows自帶的游戲《掃雷》是陪伴了無(wú)數(shù)人的經(jīng)典游戲,本文將利用C語(yǔ)言實(shí)現(xiàn)這一經(jīng)典的游戲,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下
    2022-05-05

最新評(píng)論