The content of this article is about the implementation of image fusion, addition operation and image type conversion in python (with code). It has certain reference value. Friends in need can refer to it. I hope It will help you.
1.Numpy library addition
The operation method is: target image = image 1 image 2, the operation result is modulo operation .
1) When the pixel value is 2) When the pixel value is >255, the result is the result modulo 255 , for example: (255 64)%5=64
2.OpenCV addition operation
Another method is to directly call the OpenCV library to implement image addition operation. The method is as follows:
Target image = cv2.add(image 1, image 2)
The result at this time is a saturation operation, that is:
1) When the pixel value 2) When the pixel value > 255, the result is 255, for example: (255 64) = 255
The corresponding codes of the two methods are as follows:
#encoding:utf-8import cv2 import numpy as np import matplotlib.pyplot as plt#读取图片img = cv2.imread('picture.bmp') test = img #方法一: Numpy加法运算result1 = img + test #方法二: OpenCV加法运算result2 = cv2.add(img, test) #显示图像 cv2.imshow("original", img) cv2.imshow("result1", result1) cv2.imshow("result2", result2) #等待显示 cv2.waitKey(0) cv2.destroyAllWindows()
The output result is shown in the figure below, where result1 is the first method, result2 is the second method, and there are more white points 255.
Note: The size and type of images participating in the operation must be consistent. Below is the result of adding the color images.
Image fusion usually refers to fusing information from two or more images into one In terms of images, fused images contain more information and can be more convenient for people to observe or computer processing. As shown in the figure below, two unclear images are fused to obtain a clearer image.
#Image fusion adds coefficients and brightness adjustments based on image addition.
1) Image addition: Target image = Image 1 Image 2
2) Image fusion: Target image = Image 1 * Coefficient 1 Image 2 * Coefficient 2 Brightness adjustment amount
The main function called is addWeighted, method As follows:
dst = cv2.addWeighter(scr1, alpha, src2, beta, gamma)
dst = src1 * alpha src2 * beta gamma
Parameters gamma cannot be omitted.
The code is as follows:
#encoding:utf-8import cv2 import numpy as np import matplotlib.pyplot as plt #读取图片 src1 = cv2.imread('test22.jpg') src2 = cv2.imread('picture.bmp') #图像融合 result = cv2.addWeighted(src1, 1, src2, 1, 0) #显示图像 cv2.imshow("src1", src1) cv2.imshow("src2", src2) cv2.imshow("result", result) #等待显示 cv2.waitKey(0) cv2.destroyAllWindows()
It should be noted that the pixel size of the two fused images needs to be the same. As shown in the figure below, two RGB images with pixels of 410*410 are fused.
Set the fusion of different proportions as follows:
result = cv2.addWeighted(src1, 0.6, src2, 0.8, 10)
Image type conversion refers to converting one type to another type, such as converting a color image to a grayscale image , Convert BGR images to RGB images. OPenCV provides conversions between more than 200 different types, of which the most commonly used include 3 categories, as follows:
cv2.COLOR_BGR2GRAY
cv2.COLOR_BGR2RGB
cv2.COLOR_GRAY2BGR
The code is as follows:
#encoding:utf-8import cv2 import numpy as np import matplotlib.pyplot as plt #读取图片 src = cv2.imread('01.bmp') #图像类型转换 result = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) #显示图像 cv2.imshow("src", src) cv2.imshow("result", result) #等待显示 cv2.waitKey(0) cv2.destroyAllWindows()
The output result is as shown below:
If channel conversion is used, the result is as shown below:
result = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
Image processing usually requires converting color images into grayscale images for subsequent operations. More knowledge will be shared in the future. I hope you like it, especially I am a classmate doing image recognition and image processing.
Related recommendations:
Python implements image geometric transformation
##Python image grayscale transformation and image array operation
The above is the detailed content of Implementation of image fusion, addition operation and image type conversion in python (with code). For more information, please follow other related articles on the PHP Chinese website!