QT+OpenCV综合示例:图像混合(滑动条)
生活随笔
收集整理的這篇文章主要介紹了
QT+OpenCV综合示例:图像混合(滑动条)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
QT+OpenCV綜合示例:圖像混合(滑動條)
- 1、代碼:
- 2、運行結果:
0)Widget_op.ui (界面文件)設計:
1、代碼:
1)opencv_imwrite_Q.pro 添加:
INCLUDEPATH+= D:\opencv-3.1.0\opencv\build\includewin32:CONFIG(release, debug|release): LIBS += -LD:/opencv-3.1.0/opencv/build/x64/vc12/lib/ -lopencv_world310 else:win32:CONFIG(debug, debug|release): LIBS += -LD:/opencv-3.1.0/opencv/build/x64/vc12/lib/ -lopencv_world310d else:unix: LIBS += -LD:/opencv-3.1.0/opencv/build/x64/vc12/lib/ -lopencv_world310INCLUDEPATH += D:/opencv-3.1.0/opencv/build/x64/vc12 DEPENDPATH += D:/opencv-3.1.0/opencv/build/x64/vc122)主函數 main.cpp 添加:
#include "Widget_c.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget_c w;w.show();return a.exec(); }3)Widget_c.h (主窗口頭文件)添加:
#ifndef WIDGET_C_H #define WIDGET_C_H #ifdef WIN32 #pragma execution_character_set("utf-8") #endif #pragma warning(disable:4819) #include <QWidget> #include <opencv2/opencv.hpp> #include <QImage> #include <QMouseEvent> #include <QLabel>namespace Ui { class Widget_c; }class Widget_c : public QWidget {Q_OBJECTpublic:explicit Widget_c(QWidget *parent = 0);~Widget_c();bool eventFilter(QObject *obj, QEvent *event);private slots:void on_horizontalSlider_valueChanged(int value);private:Ui::Widget_c *ui;cv::Mat g_srcImage1;cv::Mat g_srcImage2;cv::Mat g_dstImage;const int g_nMaxAlphaValue=100; //Alpha值的最大值private:QImage MatToQImage(const cv::Mat& mat);void display_MatInQT(QLabel* label,cv::Mat& mat); };#endif // WIDGET_C_H4)Widget_c.cpp (主窗口源文件)添加:
#include "Widget_c.h" #include "ui_Widget_c.h" #pragma warning(disable:4819) #include <QMessageBox> #include <QFileDialog>using namespace cv;Widget_c::Widget_c(QWidget *parent) :QWidget(parent),ui(new Ui::Widget_c) {ui->setupUi(this);this->setFixedSize(600,450);this->setWindowFlags(Qt::WindowCloseButtonHint |Qt::WindowMinimizeButtonHint );ui->label->installEventFilter(this); //安裝事件過濾器ui->label->setText(tr("點擊加載圖片..."));ui->label->setFont(QFont("微軟雅黑",20,QFont::Bold,true));ui->label->setStyleSheet("color:blue; background-color:lightYellow;"); //文本顏色(前景色)//加載圖像 (兩圖像的尺寸需相同)//g_srcImage1 = imread("F:/C++/2. OPENCV 3.1.0/Trackbar_QSlier/1.jpg");//g_srcImage2 = imread("F:/C++/2. OPENCV 3.1.0/Trackbar_QSlier/2.jpg");//設置滑動條控件的最小/大值ui->horizontalSlider->setMinimum(0);ui->horizontalSlider->setMaximum(100);ui->horizontalSlider->setEnabled(false);//ui->lineEdit->setText(tr("50 %")); }Widget_c::~Widget_c() {delete ui; }// MatToQImage 圖像轉換函數 QImage Widget_c::MatToQImage(const cv::Mat& mat) {// 8-bits unsigned, NO. OF CHANNELS = 1if(mat.type() == CV_8UC1){QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);// Set the color table (used to translate colour indexes to qRgb values)image.setColorCount(256);for(int i = 0; i < 256; i++){image.setColor(i, qRgb(i, i, i));}// Copy input Matuchar *pSrc = mat.data;for(int row = 0; row < mat.rows; row ++){uchar *pDest = image.scanLine(row);memcpy(pDest, pSrc, mat.cols);pSrc += mat.step;}return image;}// 8-bits unsigned, NO. OF CHANNELS = 3else if(mat.type() == CV_8UC3){// Copy input Matconst uchar *pSrc = (const uchar*)mat.data;// Create QImage with same dimensions as input MatQImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_RGB888);return image.rgbSwapped();}else if(mat.type() == CV_8UC4){//qDebug() << "CV_8UC4";// Copy input Matconst uchar *pSrc = (const uchar*)mat.data;// Create QImage with same dimensions as input MatQImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_ARGB32);return image.copy();}else{//qDebug() << "ERROR: Mat could not be converted to QImage.";return QImage();}}void Widget_c::display_MatInQT(QLabel* label,Mat& mat) {label->setPixmap(QPixmap::fromImage(MatToQImage(mat)).scaled(label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));}// 滑動條處理函數 process function void Widget_c::on_horizontalSlider_valueChanged(int value) {if(g_srcImage1.data&&g_srcImage2.data){// 顯示slider 值:QString str = QString("%1%2").arg(value).arg(" %");ui->lineEdit->setText(str);//求出當前alpha值相對于最大值的比例double g_dAlphaValue = (double)value / g_nMaxAlphaValue;//則beta值為1減去alpha值double g_dBetaValue = (1.0 - g_dAlphaValue);// 根據alpha和beta值進行線性混合(加權)// dst(I)=saturate(src1(I)?alpha+src2(I)?beta+gamma)addWeighted(g_srcImage1, g_dAlphaValue, g_srcImage2, g_dBetaValue, 0.0, g_dstImage);//imshow("show",g_dstImage);// 通過QT label 顯示display_MatInQT(ui->label,g_dstImage);}else{QMessageBox::warning(this, tr("提示"),tr("未成功載入圖片!"), QMessageBox::Ok);}}// 事件過濾器處理 bool Widget_c::eventFilter(QObject *obj, QEvent *event) {if (obj == ui->label)//指定某個QLabel{if (event->type() == QEvent::MouseButtonPress) //鼠標點擊{QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 時間轉換if(mouseEvent->button() == Qt::LeftButton){QStringList fileNames = QFileDialog::getOpenFileNames(this,tr("保存對話框"),"F:/C++/2. OPENCV 3.1.0/Trackbar_QSlier", tr("圖片文件(*.png *.jpg *.jpeg *.bmp *.tif *.tiff);;所有文件(*)"));if(fileNames.length()==2) // 逐行顯示文件路徑名{g_srcImage1 = imread(fileNames[0].toLocal8Bit().data());g_srcImage2 = imread(fileNames[1].toLocal8Bit().data());if(g_srcImage1.data&&g_srcImage2.data){ui->horizontalSlider->setEnabled(true);}else{QMessageBox::warning(NULL,"警告","載入圖片錯誤:請載入兩張同類型、尺寸的圖片!",QMessageBox::Yes);}ui->horizontalSlider->setValue(50);}else{QMessageBox::information(NULL,"提示","請載入兩張同類型、尺寸的圖片!",QMessageBox::Yes);}return true;}else{return false;}}else{return false;}}else{// pass the event on to the parent classreturn QWidget::eventFilter(obj, event);} }2、運行結果:
總結
以上是生活随笔為你收集整理的QT+OpenCV综合示例:图像混合(滑动条)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QT学习笔记(六):Qt5主窗口框架示例
- 下一篇: Python存储生成的决策树——pick