我们如果想实现以下图像的混合应该如何实现呢?图像混合技术其实就是两幅图像每个对应像素的加权平均,依照权重的不同图像的重点突出部分也不一样。new=src1w1+src2w2+w0;
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(){
int i, j,temp;
const int n = 2;
Mat src = imread("D:/t.jpg");
Mat srcci = imread("D:/4.jpg");
Mat dst = src.clone();
for (i = 0; i < src.rows; i++)
{
uchar* p = dst.ptr<uchar>(i);
uchar* q = src.ptr<uchar>(i);
uchar*w = srcci.ptr<uchar>(i);
for (j = 0; j < src.rows*src.channels(); j++){
temp = w[j] * 0.3 + p[j] * 0.7 + 30;
p[j] = (temp > 255) ? 255 : (temp);
}
}
imshow("window-src", src);
imshow("window-srcci", srcci);
imshow("window-dst", dst);
waitKey(0);
}
应用滑动条后
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
static int a=0;
void changea(int, void*){
int i, j, temp;
cout << a << endl;
double n =a;
n = n / 100;
cout << n << endl;
Mat src = imread("D:/a.jpg");
Mat srcci = imread("D:/b.jpg");
Mat dst = src.clone();
for (i = 0; i < src.rows; i++)
{
uchar* p = dst.ptr<uchar>(i);
uchar* q = src.ptr<uchar>(i);
uchar*w = srcci.ptr<uchar>(i);
for (j = 0; j < src.cols*3; j++){
temp = w[j] * n + p[j] * (1 - n) + 30;
p[j] = (temp > 255) ? 255 : (temp);
}
}
imshow("图像混合", dst);
}
int main(){
namedWindow("图像混合",1);
createTrackbar("Weight:", "图像混合", &a, 100, changea);
changea(0, NULL);
waitKey(0);
}
createTrackbar(“Weight:”, “图像混合”, &a, 100, changea);增加滑动条到“图像混合”窗口,并显示窗口,每次调滑动条都会调用函数changea(),changea()有固定的参数int,void*;