opencv+qt图像对比度和亮度demo

opencv_1.h

#ifndef OPENCV_1_H
#define OPENCV_1_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace cv;

class OPencv_1 : public QWidget
{
    Q_OBJECT

public:
    OPencv_1(QWidget *parent = nullptr);
    ~OPencv_1();

    void initMainWindow();
    void imgProc(float contrast, int brightness);

private slots:
    void slotContrastMoved(int position);
    void slotContrastChanged(int value);
    void slotBrightnessMoved(int position);
    void slotBrightnessChanged(int value);
private:
    Mat         myImg;
    QImage      myQImg;
    QLabel      *_viewLabel=nullptr;
    QHBoxLayout *_mainLayout = nullptr;
    QLabel      *_contrastLabel = nullptr;
    QLabel      *_brightnessLabel = nullptr;
    QSlider     *_contrastSlider = nullptr;
    QSlider     *_brightnessSlider = nullptr;
    QGridLayout *_GLayout = nullptr;

};

#endif // OPENCV_1_H

opencv_1.cpp

#include "opencv_1.h"
#include 

OPencv_1::OPencv_1(QWidget *parent)
    : QWidget(parent)
{   
    _mainLayout = new QHBoxLayout(this);
    _viewLabel = new QLabel(this);
    _viewLabel->setFixedSize(600,400);
    _mainLayout->addWidget(_viewLabel,9);
    _GLayout = new QGridLayout(this);
    _contrastSlider = new QSlider(this);
    _brightnessSlider = new QSlider(this);
    _contrastLabel = new QLabel(QString("contrast"),this);
    _brightnessLabel = new QLabel(QString("bright"),this);
    _GLayout->addWidget(_contrastSlider,0,0,9,1);
    _GLayout->addWidget(_brightnessSlider,0,1,9,1);
    _GLayout->addWidget(_contrastLabel,10,0,1,1);
    _GLayout->addWidget(_brightnessLabel,10,1,1,1);
    _mainLayout->addLayout(_GLayout,1);
    initMainWindow();
    connect(_contrastSlider,&QSlider::sliderMoved,this,&OPencv_1::slotContrastMoved);
    connect(_contrastSlider,&QSlider::valueChanged,this,&OPencv_1::slotContrastChanged);
    connect(_brightnessSlider,&QSlider::sliderMoved,this,&OPencv_1::slotBrightnessMoved);
    connect(_brightnessSlider,&QSlider::valueChanged,this,&OPencv_1::slotBrightnessChanged);
}

void OPencv_1::initMainWindow()
{
    QString imgFile("./demoImg.jpg");
    Mat imgData = imread(imgFile.toLatin1().data());
    //imshow("First show",imgData);
    cvtColor(imgData,imgData,COLOR_BGR2RGB);
    myImg = imgData;
    myQImg = QImage((const unsigned char*)imgData.data,imgData.cols,imgData.rows,QImage::Format_RGB888);
    _viewLabel->setPixmap(QPixmap::fromImage(myQImg.scaled(_viewLabel->size(),Qt::KeepAspectRatio)));
}

void OPencv_1::imgProc(float contrast, int brightness)
{
    Mat imgSrc = myImg;
    Mat imgDst = Mat::zeros(imgSrc.size(),imgSrc.type());
    imgSrc.convertTo(imgDst,-1,contrast,brightness);
    myQImg = QImage((const unsigned char*)imgDst.data,imgDst.cols,imgDst.rows,QImage::Format_RGB888);
    _viewLabel->setPixmap(QPixmap::fromImage(myQImg.scaled(_viewLabel->size(),Qt::KeepAspectRatio)));
}

void OPencv_1::slotContrastMoved(int position)
{
    imgProc(position/33.3,0);
}

void OPencv_1::slotContrastChanged(int value)
{
    imgProc(value/33.3,0);
}

void OPencv_1::slotBrightnessMoved(int position)
{
    imgProc(1.0,position);
}

void OPencv_1::slotBrightnessChanged(int value)
{
    imgProc(1.0,value);
}

OPencv_1::~OPencv_1()
{
    delete _viewLabel;
    delete _contrastLabel;
    delete _brightnessLabel;
    delete _contrastSlider;
    delete _brightnessSlider;
    delete _GLayout;
    delete _mainLayout;

    _viewLabel=nullptr;
    _contrastLabel = nullptr;
    _brightnessLabel = nullptr;
    _contrastSlider = nullptr;
    _brightnessSlider = nullptr;
    _GLayout = nullptr;
    _mainLayout = nullptr;
}

opencv+qt图像对比度和亮度demo_第1张图片

opencv+qt图像对比度和亮度demo_第2张图片

你可能感兴趣的:(opencv+qt)