本文主要講的是怎樣將2幅圖片疊加,就像電影里面有時候出現的鏡頭一樣,2幅圖片可以按照不同比例重疊,當然這里主要是利用opencv自帶的函數addWeighted().參考資料為opencv自帶tutiol及其代碼。
開發環境:ubuntu12.04+opencv2.4.2+Qt4.8.2+QtCreator2.5.
實驗功能:
單擊Open image1按鈕,打開第1幅圖片並在左邊顯示出來。
單擊Open image2按鈕,打開第2幅圖片並在中間顯示出來。
在Alpha欄輸入第1幅圖片在圖片混合時所占的比例,這時Beta欄的值也會跟着改變,因為2着的比例之和為1.0.
同理,在Beta欄中輸入第2幅圖片在圖片混合時所占的比例,這時Alpha欄里的值也會改變。
單擊Add image按鈕,在軟件右邊那欄可以看到圖片混合的效果。
實驗說明:
addWeighted()函數的第1個參數為輸入圖片1,第2個參數為圖片1的系數,參數3為輸入圖片2,參數4為圖片2的系數。其實這2個系數之和沒有必要為1,我這里在程序中定為1是防止圖片出現過飽和等情況。參數5為混合后圖片的深度,如果圖片1和圖片2深度相同的話,則該參數為-1. 最后1個參數為輸出混合圖片結果保存處。
獲得LineEdit的值是調用其text()函數,然后轉換成相應的數據類型即可.
QPalette為調色版,其顏色組分為3組,活動組,非活動組,不可用組;其中活動組為當前窗口所在組,非活動組為其它窗口所在組,不可用組是由於某種原因暫時不能使用的 組 。改變調色版的顏色可以使用setColor這個函數,這個函數有2個重載函數,其中參數ColorRole表示是更改哪個參數;比如說一般的背景色。
EditLine背景色顏色角色為QPalette::Base;前景色文字的顏色角色為QPalette::Text.
Button背景色顏色角色為QPalette::Button.前景色文字的顏色角色為QPalette::ButtonText.
Label前景文字顏色角色為QPalette::WindowText.
實驗結果:
圖片1和圖片2權重比為1:9時混合效果:

圖片1和圖片2權重比為1:1時混合效果:

圖片1和圖片2權重比為9:1時混合效果:

實驗主要部分代碼及注釋(附錄有工程code下載鏈接):
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPalette> #include <QFileDialog> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); alpha = 0.50; beta = 0.50; /*QPalette為調色版,其顏色組分為3組,活動組,非活動組,不可用組;其中活動組為當前窗口所在組,非活動組為其它窗口所在組,不可 用組是由於某種原因暫時不能使用的組 改變調色版的顏色可以使用setColor這個函數,這個函數有2個重載函數,其中參數ColorRole表示是更改哪個參數;比如說一般的背景色 就是QPalette::base,這里的文字顏色為QPalette::Text*/ QPalette pal_editline; pal_editline.setColor( QPalette::Text, Qt::green ); pal_editline.setColor( QPalette::Base, Qt::black ); ui->alphaEdit->setPalette( pal_editline ); ui->betaEdit->setPalette( pal_editline ); // ui->img1Browser->setStyleSheet( "background-color:black" ); // ui->img2Browser->setStyleSheet( "background-color:balck" ); //改變label的文字顏色 QPalette pal_label; pal_label.setColor( QPalette::WindowText, Qt::red );//這里要用WindowText,因為這是Labl上的文字 ui->alphaLabel->setPalette( pal_label ); ui->betaLabel->setPalette( pal_label ); //改變button上的文字顏色 QPalette pal_button; pal_button.setColor( QPalette::ButtonText, Qt::green ); pal_button.setColor( QPalette::Button, Qt::black ); ui->closeButton->setPalette( pal_button ); ui->open1Button->setPalette( pal_button ); ui->open2Button->setPalette( pal_button ); ui->addButton->setPalette( pal_button ); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_closeButton_clicked() { close(); } void MainWindow::on_open1Button_clicked() { QString qimg1 = QFileDialog::getOpenFileName( this, "Open image1", "../", tr("Image Files( *.png *.jpeg *.jpg *.bmp)") ); img1 = imread( qimg1.toAscii().data() ); ui->img1Browser->clear(); ui->img1Browser->append( tr("<img src=%1>").arg( qimg1 ) ); } void MainWindow::on_open2Button_clicked() { QString qimg2 = QFileDialog::getOpenFileName( this, "Open image1", "../", tr("Image Files( *.png *.jpeg *.jpg *.bmp)") ); img2 = imread( qimg2.toAscii().data() ); ui->img2Browser->clear(); ui->img2Browser->append( tr("<img src=%1>").arg( qimg2 ) ); } void MainWindow::on_addButton_clicked() { addWeighted( img1, alpha, img2, beta, 0.0, img3 );//圖像混合的函數 imwrite( "../add_img.jpg", img3 ); ui->img3Browser->clear(); ui->img3Browser->append( "<img src=../add_img.jpg>" ); } void MainWindow::on_betaEdit_editingFinished() { beta = ui->betaEdit->text().toFloat();//對於LineEdit是用text函數獲得其內容 alpha = 1.0 - beta; ui->alphaEdit->setText( QString("%1").arg(alpha) );//回車后alpha的值也會改變,且滿足2之和為1.0 } void MainWindow::on_alphaEdit_editingFinished() { alpha = ui->alphaEdit->text().toFloat(); beta = 1.0 - alpha; ui->betaEdit->setText( QString("%1").arg(beta) ); }
實驗總結:
對Qt某些控件顏色的操作稍微了解了點。另外圖片顯示時不能夠根據圖片實際大小來自動調整窗口。
