Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to realize Image Fusion and addition

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use Python to achieve image fusion and addition operation". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Python to achieve image fusion and addition operation" can help you solve the problem.

one。 Image addition operation 1.Numpy library addition

The operation method is as follows: target image = image 1 + image 2, and the result is modular operation.

When the pixel value is 255, the result is the result of modularization for 255, for example: (25564)% 25564

2.OpenCV addition operation

Another method is to directly call the OpenCV library to implement the image addition operation, as follows:

Target image = cv2.add (image 1, image 2)

The result is a saturation operation, that is:

When the pixel value is 255, the result is 255, for example: (25564) = 255

The corresponding code for the two methods is as follows:

# encoding:utf-8import cv2import numpy as npimport matplotlib.pyplot as plt# reads pictures img = cv2.imread ('picture.bmp') test = img# method 1: Numpy addition operation result1 = img + test# method 2: OpenCV addition operation result2 = cv2.add (img, test) # display image cv2.imshow ("original", img) cv2.imshow ("result2", result1) cv2.imshow ("result2", result2) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()

The output is shown in the following figure, where result1 is the first method, result2 is the second method, and there are more white dots.

Note: the size and type of images involved in the operation must be the same. The following is the result of adding to the color image.

two。 Image fusion

Image fusion usually refers to the fusion of two or more images into one image, the fused image contains more information, which is more convenient for people to observe or computer processing. As shown in the following figure, fuse two unclear images to get a clearer picture.

Image fusion increases the coefficient and brightness adjustment on the basis of image addition.

Image addition: target image = image 1 + image 2

Image fusion: target image = image 1 * coefficient 1 + image 2 * coefficient 2 + brightness adjustment

The main function called is addWeighted, and the method is as follows:

Dst = cv2.addWeighter (scr1, alpha, src2, beta, gamma) dst = src1 * alpha + src2 * beta + gamma

The parameter gamma cannot be omitted.

The code is as follows:

# encoding:utf-8import cv2import numpy as npimport matplotlib.pyplot as plt# read picture src1 = cv2.imread ('test22.jpg') src2 = cv2.imread (' picture.bmp') # Image fusion result = cv2.addWeighted (src1, 1, src2, 1, 0) # display image cv2.imshow ("src1", src1) cv2.imshow ("src2", src2) cv2.imshow ("result", result) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()

It is important to note that the pixel sizes of the two fused images need to be the same, as shown in the following image, the two RGB images with 410 pixels are fused.

The blending of setting different proportions is as follows:

Result = cv2.addWeighted (src1, 0.6, src2, 0.8,10)

three。 Image type conversion

Image type conversion refers to the conversion of one type to another, such as color image to grayscale image, BGR image to RGB image. OPenCV provides more than 200 different types of conversions, of which three are the most commonly used, as follows:

Cv2.COLOR_BGR2GRAY

Cv2.COLOR_BGR2RGB

Cv2.COLOR_GRAY2BGR

The code is as follows:

# encoding:utf-8import cv2import numpy as npimport matplotlib.pyplot as plt# read picture src = cv2.imread ('01.bmp') # Image type conversion result = cv2.cvtColor (src, cv2.COLOR_BGR2GRAY) # display image cv2.imshow ("src", src) cv2.imshow ("result", result) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()

The output is shown in the following figure:

If you use channel conversion, the result is shown in the following figure:

Result = cv2.cvtColor (src, cv2.COLOR_BGR2RGB)

This is the end of the introduction on "how to use Python to achieve image fusion and addition". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report