圖像混合

本章主要學習addWeighted(src1,alpha,src2,beta,gamma,dst,dtype=-1)這個函數,

參數1:輸入圖像Mat-src1;

參數2:輸入圖像Mat-src1的alpha值;

參數3:輸入圖像Mat-src2;

參數4:輸入圖像Mat-src2的alpha值(1-alpha);

參數5:gamma值,默認爲0;

參數6:輸出混合圖像

函數功能將兩幅圖像相加,注意兩幅圖像類型必須相同,否則相加會出現錯誤,這個API是opencv自帶的,也可以直接用add(src1,src2,dst,Mat()),來操作,效果沒有addWeighted

源代碼如下:

#include <opencv2/opencv.hpp>

#include <iostream>

using namespace cv;

using namespace std;

int main(int argc, char** argv) 

{

Mat src1, src2, dst;

src1 = imread("C:/Users/Administrator/Desktop/3.jpg");

src2 = imread("C:/Users/Administrator/Desktop/4.jpg");

if (!src1.data)

{

cout << "the image src1 could not load..." << endl;

return -1;

}

if (!src2.data)

{

cout << "the image src2 could not load..." << endl;

return -1;

}

double alpha = 0.5;

if (src1.rows == src2.rows && src1.cols == src2.cols && src1.type() == src2.type())

{

addWeighted(src1, alpha, src2, (1.0 - alpha), 0.0, dst, -1);//圖像相加

//add(src1, src2, dst, Mat());

imshow("src1", src1);

imshow("src2", src2);

imshow("blend image", dst);

imwrite("C:/Users/Administrator/Desktop/blend image.jpg", dst);

}

else {

printf("could not blend images,the size of image is not same\n");

return -1;

}

waitKey(0);

return 0;

}

3.jpg4.jpgblend image.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章