opencv学习_qt显示图片

Posted Leslie X徐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv学习_qt显示图片相关的知识,希望对你有一定的参考价值。

opencv+qt

1.opencv显示图片

hpp

三个主要函数

  • imgInit():负责加载图片,转换格式等操作
  • imgProc():负责对图片的处理和算法实现
  • imgShow():负责在窗口显示图片

#include "opencv2/opencv.hpp"
using namespace cv;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void imgInit();
    void imgProc(float contrast,int brightness);
    void imgShow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    Mat myImg;
    QImage myQImg;
};

cpp

void MainWindow::imgInit()
{
    Mat ImgData = imread("test.jpg");
    cvtColor(ImgData,myImg,COLOR_BGR2RGB);
    myQImg = QImage((const unsigned char*)(myImg.data),myImg.cols,myImg.rows,QImage::Format_RGB888);
    imgShow();
}

void MainWindow::imgProc(float con, int bri)
{
    Mat src = myImg;
    Mat dst = Mat::zeros(src.size(),src.type());
    src.convertTo(dst,-1,con,bri);
    myQImg = QImage((const unsigned char*)(dst.data),dst.cols,dst.rows,QImage::Format_RGB888);
    imgShow();
}


void MainWindow::imgShow()
{
    ui->label->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->label->size())));//显示到Qlabel上
}


void MainWindow::on_pushButton_clicked()
{
    imgInit();
}

注意:

Mat默认的是BGR空间,而QImage是RGB,所以使用cvtColor函数预处理一下。

第二个函数QImage(uchar *data, int width, int height, QImage::Format format)

2.添加滑条处理图片

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    imgInit();
    imgShow();
    ui->verticalSlider->setSliderPosition(50);
    ui->verticalSlider_2->setSliderPosition(50);
}

void MainWindow::on_verticalSlider_sliderMoved(int position)
{
    imgProc(position/33.3, 0);
}


void MainWindow::on_verticalSlider_valueChanged(int value)
{
    imgProc(value/33.3, 0);
}


void MainWindow::on_verticalSlider_2_sliderMoved(int position)
{
    imgProc(1.0, position);
}


void MainWindow::on_verticalSlider_2_valueChanged(int value)
{
    imgProc(1.0, value);
}

3.滤波函数

四个滤波函数:

  • cv::blur():均值滤波
  • cv::cv::GaussianBlur():高斯滤波
  • cv::medianBlur():中值滤波
  • cv::bilateralFilter():双边滤波
void MainWindow::imgInit()
{
    QString imgPath = "test.jpg";
    Mat imgData = imread(imgPath.toLatin1().data());
    if(imgData.empty()){qDebug("no img");return;}
    cvtColor(imgData,imgData,COLOR_BGR2RGB);
    myImg = imgData;

    myBlurQImg = QImage((const unsigned char*)(imgData.data),imgData.cols,imgData.rows,QImage::Format_RGB888);
    myGaussianQImg   =   myBlurQImg;
    myMedianQImg    =   myBlurQImg;
    myBilateralQImg =   myBlurQImg;

    imgShow();
}

void MainWindow::imgShow()
{
    ui->labAverage->setPixmap(QPixmap::fromImage(myBlurQImg.scaled(ui->labAverage->size())));
    ui->labGaussian->setPixmap(QPixmap::fromImage(myGaussianQImg.scaled(ui->labGaussian->size())));
    ui->labMedian->setPixmap(QPixmap::fromImage(myMedianQImg.scaled(ui->labMedian->size())));
    ui->labBilateral->setPixmap(QPixmap::fromImage(myBilateralQImg.scaled(ui->labBilateral->size())));
}

void MainWindow::imgProc(int kernel)
{
    Mat src = myImg;

    Mat dst1 = src.clone();
    for (int i=1 ; i<kernel ; i=i+2 ) cv::blur(src,dst1,Size(i,i),Point(-1,-1));
    myBlurQImg = QImage((const unsigned char*)(dst1.data),dst1.cols,dst1.rows,QImage::Format_RGB888);

    Mat dst2 = src.clone();
    for (int i=1 ; i<kernel ; i=i+2 ) cv::GaussianBlur(src,dst2,Size(i,i),0,0);
    myGaussianQImg = QImage((const unsigned char*)(dst2.data),dst2.cols,dst2.rows,QImage::Format_RGB888);

    Mat dst3 = src.clone();
    for (int i=1 ; i<kernel ; i=i+2 ) cv::medianBlur(src,dst3,i);
    myMedianQImg = QImage((const unsigned char*)(dst3.data),dst3.cols,dst3.rows,QImage::Format_RGB888);

    Mat dst4 = src.clone();
    for (int i=1 ; i<kernel ; i=i+2 ) cv::bilateralFilter(src,dst4,i,i*2,i/2);
    myBilateralQImg = QImage((const unsigned char*)(dst4.data),dst4.cols,dst4.rows,QImage::Format_RGB888);

    imgShow();
}

void MainWindow::on_verticalSlider_sliderMoved(int position)
{
    imgProc(position);
}

void MainWindow::on_verticalSlider_valueChanged(int value)
{
    imgProc(value);
}


void MainWindow::on_btnsave_clicked()
{
    QString savefile = QFileDialog::getSaveFileName(this,tr("save file"),QDir::currentPath(),tr("pic(*.jpg *.png *.jpeg *.bmp)"));
    if(savefile.isEmpty())return;
    QScreen *screen = QGuiApplication::primaryScreen();

    if(ui->rbaverage->isChecked()) screen->grabWindow(ui->labAverage->winId()).save(savefile);
    if(ui->rbgaussian->isChecked()) screen->grabWindow(ui->labGaussian->winId()).save(savefile);
    if(ui->rbmedian->isChecked()) screen->grabWindow(ui->labMedian->winId()).save(savefile);
    if(ui->rbbilateral->isChecked()) screen->grabWindow(ui->labBilateral->winId()).save(savefile);
}

4.混合图像

混合图像函数:

  • cv::addWeighted(原图2,alpha,原图1,1-alpha,gramma,合成图);:alpha在0~1之间
void MainWindow::imgInit()
{
    Mat imgData = imread("test01.jpg");
    cvtColor(imgData,imgData,COLOR_BGR2RGB);
    myImg = imgData;
    myQImg = QImage((const uchar*)(myImg.data),myImg.cols,myImg.rows,QImage::Format_RGB888);
    imgShow();
}

void MainWindow::imgProc(float alpha)
{
    Mat src1 = myImg;
    Mat src2 = imread("test02.jpg");
    cvtColor(src2,src2,COLOR_BGR2RGB);
    //Warning 因为我们对 src1 和 src2 求 和 ,它们必须要有相同的尺寸(宽度和高度)和类型。
    cv::resize(src2,src2,Size(src1.cols,src1.rows));

    Mat dst;
    cv::addWeighted(src2,alpha,src1,1-alpha,0,dst);  //g(i,j)=alpha*f0(i,j)+(1-alpha)*f1(i,j)
    myQImg = QImage((const uchar*)(dst.data),dst.cols,dst.rows,QImage::Format_RGB888);
    imgShow();
}

void MainWindow::imgShow()
{
    ui->label->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->label->size())));
}


void MainWindow::on_verticalSlider_sliderMoved(int position)
{
    imgProc(position/100.0);
}


void MainWindow::on_verticalSlider_valueChanged(int value)
{
    imgProc(value/100.0);
}

以上是关于opencv学习_qt显示图片的主要内容,如果未能解决你的问题,请参考以下文章

Qt OpenCV 在界面显示图片 通过Lable方式 和GraphicsView 方式

Ubuntu中Qt+opencv图像显示

组合多张图片并在Qt中显示

opencv读入图片注意事项

基于opencv和qt的人脸检测小系统

vs2015 + opencv3.4.0 + qt msvc2015_64-5.7.1 视屏显示