[Open CV Basics] Alpha Fusion of the two images,
In the image operation of Open CV, we can use the cvAddWeighted function to achieve the fusion of the two images. The complete signature of the function is:
void cvAddWeighted( const CvArr* src1, double alpha, const CvArr* src2, double beta, double gamma, CvArr* dst );
Parameters |
Meaning |
Src1 |
First image |
Alpha |
First image parameter during Fusion |
Src2 |
Type 2 Image |
Beta |
The second image parameter during Fusion |
Gamma |
Constant, usually depends on the average value and maximum value to be adjusted to the pixel. |
Dst |
Merged image |
The function of this function is weighted fusion image. The pixels of each pixel are calculated based on the pixel weights of the corresponding pixels of the two source images. The fusion formula is as follows:
Dst = alpha * src1 + beta * src2 + gamma
When alpha is a, beta is 1-a, and gamma is 0, where a> = 0 and a <= 1, the fusion equation is called the standard fusion equation.
The following is an example:
# Include "cv. h "# include" highgui. h "int main () {// name of the two images to be merged char * imgStr1 =" faceScene.jpg "; char * imgStr2 =" fruits.jpg "; // open two images: IplImage * img1 = cvLoadImage (imgStr1); IplImage * img2 = cvLoadImage (imgStr2 ); // set parameters alpha, beta, and gamma to a, 1-a, and 0, respectively. This is the standard Alpha Fusion equation float alpha = 0.3; float beta = 0.7; float gamma = 0; // create an image to receive the merged image IplImage * dst = cvCreateImage (cvSize (img1-> width, img1-> height), img1-> depth, 3); // use cvAddWeighted to execute Alpha Fusion cvAddWeighted (img1, alpha, img2, beta, gamma, dst); // output three images to view the effect char * s1 = "img1 ", * s2 = "img2", * s3 = "dst"; cvNamedWindow (s1); cvNamedWindow (s2); cvNamedWindow (s3); cvShowImage (s1, img1); cvShowImage (s2, img2); cvShowImage (s3, dst); cvWaitKey (0); cvReleaseImage (& img1); cvReleaseImage (& img2); cvReleaseImage (& dst); cvDestroyWindow (s1 ); cvDestroyWindow (s2); cvDestroyWindow (s3 );}
Image to be merged 1:
Image to be merged 2:
Merged images: