- addWeighted(InputArray Src1, int alpth, InputArray Src2, int beta, InputArray dst);
alpth + beta = 1 权重(调节透明度)
当 alpth = 0, 显示Src2
cpp
QString imgpath1 = "xxxxxxxx";
QString imgpath2 = "xxxxxxxxx";
Mat imgSrc1 = imread(imgpath1.toStdString());
Mat imgSrc2 = imread(imgpath2,toStdString());
Mat imgDst;
cvtColor(imgSrc1, imgSrc1, COLOR_BGR2RGB);
cvtColor(imgSrc2, imgSrc2, COLOR_BGR2RGB);
addWeighted(imgSrc1, 0.5, imgSrc2, 0.5, imgDst);
QImage myQimg = QImage((const unsigned char*)imgDst.data(), imgDst.cols, imgDst.rows, static_cast<int>(imgDst.step), QImage::Format_RGB888);
ui->lable->setPixmap(QPixmap::fromImage(myQimg.scaled(ui->lable0>size())));
blend.h
cpp
#ifndef BLEND_H
#define BLEND_H
#include <QWidget>
#include <opencv2/opencv.hpp>
using namespace cv;
namespace Ui {
class Blend;
}
class Blend : public QWidget
{
Q_OBJECT
public:
explicit Blend(QWidget *parent = nullptr);
~Blend();
void initMainWindow();
void imgShow();
void imgProc(double value);
private slots:
void on_verticalSlider_valueChanged(int value);
private:
Ui::Blend *ui;
Mat myImg;
QImage myQImg;
Mat imgSrc2;
Mat imgSrc1;
};
#endif // BLEND_H
blend.cpp
cpp
#include "blend.h"
#include "ui_blend.h"
#include <QDebug>
Blend::Blend(QWidget *parent) :
QWidget(parent),
ui(new Ui::Blend)
{
qDebug() << __func__;
ui->setupUi(this);
qDebug() << __func__ <<__LINE__;
setFixedSize(682,474);
initMainWindow();
}
Blend::~Blend()
{
delete ui;
}
void Blend::initMainWindow()
{
ui->label->resize(631,456);
QString imgPath = R"(D:\new_share\016_opencv\OpencvBlend\2.jpg)";
Mat img = imread(imgPath.toStdString());
cvtColor(img,img,COLOR_BGR2RGB);
myImg = img;
myQImg = QImage((const unsigned char*)img.data, img.cols, img.rows, static_cast<int>(img.step), QImage::Format_RGB888).copy();
imgSrc1 = myImg;
QString img2 = R"(D:\new_share\016_opencv\OpencvBlend\1.webp)";
imgSrc2 = imread(img2.toStdString());
cvtColor(imgSrc2, imgSrc2, COLOR_BGR2RGB);
imgShow();
}
void Blend::imgShow()
{
ui->label->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->label->size())));
}
void Blend::imgProc(double value)
{
Mat imgDst;
addWeighted(imgSrc2, value, imgSrc1, 1-value, 0, imgDst);
myQImg = QImage((const unsigned char*)imgDst.data, imgDst.cols, imgDst.rows,static_cast<int>(imgDst.step), QImage::Format_RGB888).copy();
imgShow();
}
void Blend::on_verticalSlider_valueChanged(int value)
{
imgProc(value/100.0);
}
blend.ui
