Images can be combined(结合) in different ways. Since they are regular(一般) matrices, they can be added, subtracted, multiplied, or divided.
1. Example analysis
可以使用cv::add函数来实现相加功能。如想得到加权和,可使用更精确的cv::addWeighted函数。
#include#include #include #include using namespace cv;int main(){ Mat image1; Mat image2; image1 = imread("test.jpg"); image2 = imread("test_b.jpg"); namedWindow("Image 1"); imshow("Image 1", image1); namedWindow("Image 2"); imshow("Image 2", image2); Mat result; /* add two images: cvAddWeighted( const CvArr* src1, double alpha, const CvArr* src2, double beta, double gamma, CvArr* dst ); dst(I)=src1(I)*alpha+src2(I)*beta+gamma */ addWeighted(image1, 0.7, image2, 0.5, 0., result); namedWindow("result"); imshow("result", result); waitKey(); return 0;}
1.2 Overloaded image operators(重载图像运算符)
Most arithmetic functions(运算函数) have their corresponding operator(相应操作) overloaded in OpenCV 2. Consequently(因此), the call to cv::addWeighted can be written as follows:
result = 0.7*image1 + 0.9*image2;
The preceding(前面的) code is a more compact form(紧凑的形式) that is also easier to read.
1.3 Splitting the image channels(分割图像通道)
use the cv::split function that will copy the three channels of a color image into three distinct(不同) cv::Mat instances(实例). Suppose we want to add our rain image to the blue channel only. The following is how we would proceed:
// create vector of 3 images
std::vector<cv::Mat> planes;
// split 1 3-channel image into 3 1-channel images
cv::split(image1, planes);
// add to blue channel
planes[0] += image2;
// merge the 3 1-channel images into 1 3-channel image
cv::merge(planes, result);