This series of articles is to explain the knowledge of Python OpenCV image processing. In the early stage, it mainly explains the introduction of image and basic usage of OpenCV. In the middle stage, it explains various algorithms of image processing, including image sharpening operator, image enhancement technology, image segmentation, etc. In the later stage, it studies the application of image recognition and image classification combined with deep learning. Hope this article is helpful to you, if there are deficiencies, please forgive ~

At the same time, I recommend the author’s C++ image series knowledge: [digital image processing]. MFC detail display BMP format picture [digital image processing] two. MFC single document segmentation window display picture [digital image processing] three. MFC realization of image grayscale, sampling and quantization functions of detail [digital image processing] four. MFC dialog box to draw gray histogram [digital image processing] five. Gray level linear change, gray level nonlinear change, thresholding and equalization of MFC image point operation. MFC spatial geometric transformation of image translation, mirror image, rotation, zoom detail. MFC image enhancement of ordinary smoothing, Gaussian smoothing, Laplacian, Sobel, Prewitt sharpening details

[Python image processing] OpenCV image processing (Python image processing) OpenCV+Numpy library to read and modify pixels. Get image attributes, ROI areas of interest and channel processing. Image smoothing mean filtering, box filtering, Gaussian filtering and median filtering

This article mainly explains Python to call OpenCV to realize image fusion and addition, including three parts of knowledge: image fusion, image addition, image type conversion. The full text is the basic knowledge, hope to help you. 1. Image addition operation 2. Image fusion 3

PS: This article refers to my previous series of image processing articles and OpenCV library functions, while part of the reference to NetEase cloud video, we recommend to learn.


Image addition operation

1.Numpy library addition its operation method is: target image = image 1 + image 2, and the operation result is modular operation. 1) When pixel value <=255, the result is “image 1+ image 2”, for example: 120+48=168 2) When pixel value >255, the result is the result of modulo 255, for example: (255+64)%255=64

Another method is to directly call the OpenCV library to realize the image addition operation, the method is as follows: target image = cv2.add(image 1, image 2) the result is saturated operation, namely: 1) when the pixel value <=255, the result is “image 1+ image 2”, for example: 120+48=168 2) when the pixel value is >255, the result is 255, for example :(255+64) = 255

The corresponding code for the two methods is shown below:

#encoding: utF-8 import cv2 import numpy as np import matplotlib.pyplot as PLT #encoding: utF-8 import cv2 import numpy as NP import matplotlib.pyplot as PLT Numpy = img + test Cv2. imshow("original", img) cv2.imshow("result1", Result1) cv2.imshow("result2", result2) # cv2.waitKey(0) cv2.destroyAllWindows()Copy the code

The output results are shown in the figure below, where Result1 is the first method, result2 is the second method, and there are more white dots 255.






Note: Images must be of the same size and type. Here is the result of the addition operation on the color image.







2. Image fusion

Image fusion usually refers to the fusion of two or more image information into one image. The fused image contains more information and can be more convenient for observation or computer processing. As shown in the figure below, two unclear images are fused to produce a clearer picture.






Image fusion is based on image addition to increase the coefficient and brightness adjustment. 1) Image addition: target image = image 1 + image 2 2) Image fusion: target image = image 1 * coefficient 1 + image 2 * coefficient 2 + brightness adjustment function is called addWeighted 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-8 import cv2 import numpy as NP import matplotlib.pyplot as PLT src1 = cv2.imread('test22.jpg') src2 Cv2. imread('picture.bmp') # cv2.imread('picture.bmp') # cv2.imread('picture.bmp') # cv2.imread('picture.bmp') # cv2.addWeighted(src1, 1, src2, 1, 0) # cv2.imshow("src1", Src1) cv2.imshow("src2", src2) cv2.imshow("result", result)Copy the code

It should be noted that the pixel sizes of the two fused images should be consistent. As shown in the figure below, two RGB images with 410*410 pixels are fused.






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







3. Image type conversion

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

  • cv2.COLOR_BGR2GRAY
  • cv2.COLOR_BGR2RGB
  • cv2.COLOR_GRAY2BGR

The code looks like this:

#encoding: UTF-8 import cv2 import numpy as np import matplotlib.pyplot as PLT #encoding = cv2.imread('01.bmp') #encoding: utF-8 import cv2 import numpy as np import matplotlib.pyplot as PLT Result = cv2.cvtcolor (SRC, cv2.color_bgr2gray) # cv2.imshow(" SRC ", SRC) cv2.imshow("result", Cv2.waitkey (0) cv2.DestroyallWindows ()Copy the code

The output result is as follows:






If channel conversion is used, result = cv2.cvtcolor (SRC, cv2.color_bgr2rgb)






Image processing usually requires the conversion of color image to gray image for subsequent operations, more knowledge will continue to share, I hope to like, especially to do image recognition, image processing students. Hope this article is helpful to you, if there are mistakes or inadequacies, please forgive me. I plan to go out for marriage leave in September, so I can enjoy the happiest time with her and not be bothered by work. However, whenever I finish writing an article or answering a question by myself, the pleasure of sharing knowledge really fascinates me, which is the charm of knowledge, the happiness of teachers! (By: Eastmount 2018-09-03 14pm blog.csdn.net/Eastmount/)