cvSetImageROI(IplImage* src,CvRect rect);
src表示的是源图像,rect只的是ROI区域。
cvResetImageROI(src);
Mat roi = Mat(src, Rect(x, y, width, height));
water_mark.copyTo(roi);
cv::addWeighted(roi, alpha, water_mark, beta, gamma, roi);
叠加英文文字,目前似乎不支持中文吧?
QDateTime time = QDateTime::currentDateTime();
QString time_str = time.toString("yyyy-MM-dd hh:mm:ss");
QString text = str+time_str;
cv::putText(image,text.toAscii().data(),Point(10,50),CV_FONT_HERSHEY_COMPLEX,0.5,Scalar(255,255,0));
如果要叠加中文文字,需要Qt的辅助:
重载paintEvent,并且使用QImage作为过渡,先在QImage上通过QPainter.drawText()绘制文字,用该QImage构造出QPixmap,然后通过drawPixmap绘制出叠加后的图片:
代码:
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter;
if(!text.isEmpty())
{
painter.begin(&qimg);
painter.setBrush(Qt::green);
painter.setFont(QFont("Times", 16, QFont::Bold));
painter.drawText(10,30, text);
painter.end();
}
pixmap = QPixmap::fromImage(qimg);
painter.begin(this);
painter.drawPixmap(250,50, pixmap);
painter.end();
}
效果图:
完整代码:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
using namespace cv;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void paintEvent(QPaintEvent *);
private slots:
void on_open_clicked();
void on_input_clicked();
void on_save_clicked();
private:
Ui::MainWindow *ui;
QPixmap pixmap;
QImage qimg;
QString text;
cv::Mat image;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pixmap = QPixmap(512, 512);
pixmap.fill(Qt::white);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter;
if(!text.isEmpty())
{
painter.begin(&qimg);
painter.setBrush(Qt::green);
painter.setFont(QFont("Times", 16, QFont::Bold));
painter.drawText(10,30, text);
painter.end();
}
pixmap = QPixmap::fromImage(qimg);
painter.begin(this);
painter.drawPixmap(250,50, pixmap);
painter.end();
}
void MainWindow::on_open_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), ".",
tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
qimg.load(fileName);
update();
}
void MainWindow::on_input_clicked()
{
QString in = QInputDialog::getText(this, "Input your text", "Input");
QDateTime time = QDateTime::currentDateTime();
QString time_str = time.toString("yyyy-MM-dd hh:mm:ss");
text = in + "\n" + time_str;
qDebug() << text;
update();
}
void MainWindow::on_save_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Open Image"),
".",
tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
qimg.save(fileName);
}