There is an interesting way to use opencv to achieve image fusion on the Internet:
1. Design a mask (half full 1, half full 0) and calculate the gaussion_mask [I] of the level layer;
2. Calculate the Laplacian [I] on each layer of the two images and multiply it with gaussion_mask [I] to synthesize a result_lapacian [I];
3. continuously calculate the prydown of the two images, store the top layer in gaussion [I], multiply it with gaussion_mask [I], and synthesize a result_gaussion;
4. Continue to find the pryup for result_gaussion. Each layer is synthesized with result_lapacian [I]. Finally, a fusion image of the original image size is obtained.
# Include "opencv2/opencv. HPP "using namespace CV; /*************************************** ************************************ instructions: * [0, 1 ,..., level-1] Layer * blendmask is the Image Mask * maskgaussianpyramid is the mask of each layer of the pyramid * resultlappyr stores the image converted from the left and right sides of the pyramid *//* **************************************** * ****************************/class laplacianblending {PRIVATE: mat _ <vec3f> left; MAT _ <vec3f> right; MAT _ <float> blendmask; vector <mat _ <vec3f> leftlappyr, rightlappyr, and resultlappyr; // Laplacian pyramidsmat regression, regression, resulthighestlevel; vector <mat _ <vec3f> regression; // masks are 3-channels for easier multiplication with rgbint levels; void buildpyramids () {buildlaplacianpyramid (left, leftlappyr, lefthighestlevel); buildlaplacianpyramid (right, rightlappyr, righthighestlevel); trim ();} void Merge () {// The pyramid content is the mask assert (leftlappyr. size ()> 0); maskgaussianpyramid. clear (); MAT currentimg; cvtcolor (blendmask, currentimg, cv_gray2bgr); // store color IMG of blend mask into maskgaussianpyramidmaskgaussianpyramid. push_back (currentimg); // 0-levelcurrentimg = blendmask; For (int l = 1; L <levels + 1; l ++) {mat _ down; If (leftlappyr. size ()> L) pyrdown (currentimg, _ down, leftlappyr [l]. size (); elsepyrdown (currentimg, _ down, lefthighestlevel. size (); // lowest levelmat down; cvtcolor (_ Down, down, cv_gray2bgr); maskgaussianpyramid. push_back (down); // Add color blend mask into mask pyramidcurrentimg = _ down;} void buildlaplacianpyramid (const mat & IMG, vector <mat _ <vec3f> & lappyr, mat & highestlevel) {lappyr. clear (); MAT currentimg = IMG; For (int l = 0; L <levels; l ++) {mat down, up; pyrdown (currentimg, down ); pyrup (Down, up, currentimg. size (); MAT lap = currentimg-up; lappyr. push_back (LAP); currentimg = down;} currentimg. copyto (highestlevel);} mat _ <vec3f> reconstructimgfromlappyramid () {// each layer of the resultlappyr pyramid in which the left and right Laplacian images are assembled // interpolation from top to bottom to zoom in and add them together, that is, the blend image result mat currentimg = resulthighestlevel; For (int l = levels-1; L> = 0; l --) {mat up; pyrup (currentimg, up, resultlappyr [l]. size (); currentimg = up + resultlappyr [l];} return currentimg;} void blendlappyrs () {// obtain the image resultlappyrresulthighestlevel = lefthighestlevel formed by the Laplacian transform of the left and right graphs in each pyramid. mul (maskgaussianpyramid. back () + righthighestlevel. mul (scalar (1.0, 1.0, 1.0)-maskgaussianpyramid. back (); For (int l = 0; L <levels; l ++) {mat A = leftlappyr [l]. mul (maskgaussianpyramid [l]); MAT antimask = scalar (1.0, 1.0, 1.0)-maskgaussianpyramid [l]; mat B = rightlappyr [l]. mul (antimask); MAT _ <vec3f> blendedlevel = a + B; resultlappyr. push_back (blendedlevel);} public: laplacianblending (const mat _ <vec3f> & _ left, const mat _ <vec3f> & _ right, const mat _ <float> & _ blendmask, int _ levels): // construct function, used in laplacianblending lb (L, R, m, 4); left (_ left ), right (_ right), blendmask (_ blendmask), levels (_ levels) {assert (_ left. size () = _ right. size (); Assert (_ left. size () = _ blendmask. size (); buildpyramids (); // construct Laplacian pyramid and Gaussian pyramid blendlappyrs (); // blend left & right pyramids into one pyramid }; mat _ <vec3f> blend () {return reconstructimgfromlappyramid (); // reconstruct image from Laplacian pyramid }}; mat _ <vec3f> laplacianblend (const mat _ <vec3f> & L, const mat _ <vec3f> & R, const mat _ <float> & M) {laplacianblending lb (L, R, m, 4); Return lb. blend () ;}int main () {mat l8u = imread ("left.png"); MAT r8u = imread ("right.png"); imshow ("Left", l8u ); imshow ("right", r8u); MAT _ <vec3f> L; l8u. convertize (L, cv_32f, 1.0/255.0); // vec3f indicates that there are three channels, namely, L [row] [column] [depth] mat _ <vec3f> r; r8u. convertize (R, cv_32f, 1.0/255.0);/******************** void convertize (outputarray M, int Rtype, double alpha = 1, double Beta = 0) const; * **************** // * performs linear transformation on every source array element: DST (X, Y, c) = scale * SRC (X, Y, alpha) + beta. arbitrary combination of input and output array depths are allowed (number of channels must be the same ), thus the function can be usedfor type conversion * // create blend mask matrix mmat _ <float> M (L. rows, L. cols, 0.0); // assign all M values to 0 m (range: All (), range (0, M. cols/2) = 1.0; // retrieve all rows of M & [0, M. cols/2] column, assigned as 1.0mat _ <vec3f> blend = laplacianblend (L, R, m); imshow ("blended", blend); waitkey (0 ); return 0 ;}