Image synthesis - Detailed explanation of OpenCV-Python image fusion

Image synthesis - Detailed explanation of OpenCV-Python image fusion

In image processing, image synthesis is an important task. OpenCV provides many methods to achieve image composition. Among them, the cv::addWeighted() function is a commonly used image fusion method. It can add two images with a certain weight to generate a new fusion image.

Below we will explain the cv::addWeighted() function in OpenCV in detail and give the corresponding source code.

Function prototype:

Dst(I)=alpha×Img1(I)+beta×Img2(I)+gamma

Among them, alpha and beta are weight coefficients, and gamma is the offset. For color images, the above formula is performed independently for each channel.

Sample code:

import cv2 as cv
import numpy as np

Read in image

img1 = cv.imread('img1.png')
img2 = cv.imread('img2.png')

image fusion

result = cv.addWeighted(img1, 0.7, img2, 0.3, 0)

Display the fused image

cv.imshow(‘result’, result)
cv.waitKey(0)
cv.destroyAllWindows()

In the above example code, we first read the two images that need to be fused, and then use the cv::addWeighted() function to linearly add the two images according to the specified weight coefficient, and finally obtain a fused image. image. Finally, we display the result using the cv::imshow() function.

Summarize:

Image synthesis is an important task in image processing. OpenCV provides many methods to achieve image composition. Among them, the cv::addWeighted() function is a commonly used image fusion method. It can add two images with a certain weight to generate a new fusion image. Through the explanation of this article and the demonstration of the sample code, I believe that everyone has a deeper understanding of the image fusion method in OpenCV.

Guess you like

Origin blog.csdn.net/CodeWG/article/details/131014600