I. Image Fusion
Image fusion usually refers to the fusion of information from multiple images to obtain a result that is richer in information and can aid in observation or computer processing. Figure 5-1 shows the effect of fusing two unclear images to get a clearer result.
Image fusion is the addition of coefficients and luminance adjustments to the image addition, and its main differences from the image are as follows [1-3]:
Image addition: target image = image 1 + image 2
Image fusion: Target image = Image 1 × Coefficient 1 + Image 2 × Coefficient 2 + Brightness adjustment amount
In OpenCV, image fusion is mainly realized by calling the addWeighted() function with the following prototype. Note that the pixel size of the two fused images must be the same, and the parameter gamma cannot be omitted.
dst = (scr1, alpha, src2, beta, gamma) dst = src1 * alpha + src2 * beta + gamma
The following code is an image fusion of two images, both of which have a coefficient of 1.
#coding:utf-8 # By:Eastmount import cv2 import numpy as np import as plt #Read the picture src1 = ('') src2 = ('') #Image Fusion result = (src1, 1, src2, 1, 0) #Display Image ("src1", src1) ("src2", src2) ("result", result) # Waiting for the display (0) ()
The output is shown in Figure 5-2, which fuses the src1 image and the src2 image by a scale factor to produce the target result map RESULT.
Different fusion ratios can also be set. Figure 5-3 shows the effect of the following core function.
(src1, 0.6, src2, 0.8, 10)
II. Image ROI area localization
ROI (Region of Interest) represents the region of interest, which refers to the region that needs to be processed outlined from the processed image in the form of boxes, circles, ellipses, irregular polygons and so on. The ROI region of interest can be obtained by various operators and functions, and is widely used in hotspot maps, face recognition, image segmentation and other fields. As in Figure 5-4 to obtain the face outline of Lena map [4].
ROI area can be obtained directly by pixel matrix, such as img[200:400, 200:400]. The following code is to get the face ROI area and display it.
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy as np #Read the picture img = ("") #Define 200×200 matrix 3 corresponding to BGR face = ((200, 200, 3)) # Show original image ("Demo", img) # Show ROI area face = img[150:350, 150:350] ("face", face) # Waiting for the display (0) ()
The output is shown in Figure 5-5, which extracts the face from Lena's original image.
Similarly, if you want to fuse the extracted ROI region to another image, you can use the assignment statement. The following code fuses the extracted Lena head outline into a new image.
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy as np #Read the picture img = ("") test = ("",) # Define 150×150 matrix 3 corresponding to BGR face = ((150, 150, 3)) # Show original image ("Demo", img) # Show ROI area face = img[200:350, 200:350] test[250:400, 250:400] = face ("Result", test) # Waiting for the display (0) ()
The result of the run is shown in Figure 5-6, which fuses the extracted 150×150 face contours into the new image [250:400, 250:400] region.
III. Image Properties
In the previous article we have seen the keywords size and shape. This article takes a look at three of the most common attributes in images, which are image shape, pixel size, and image type.
(1)shape
Get the shape of the image with the shape keyword and return a tuple containing the number of rows, columns and channels. Where grayscale images return the number of rows and columns, color images return the number of rows, columns and channels.
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy #Read the picture img = ("") # Get image shape print() #Display Image ("Demo", img) # Waiting for the display (0) ()
The final output is shown in Figure 5-7, (412, 412, 3), which indicates that the image has a total of 412 rows and 412 columns of pixels, including 3 channels.
(2)size
Get the number of pixels in the image by using the size keyword, which returns the number of rows x the number of columns for grayscale images and the number of rows x the number of columns x the number of channels for color images. The following code is to get the size of the " "image.
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy #Read the picture img = ("") # Get image shape print() # Get the number of pixels print()
The output is shown below and contains 510468 pixels which is 413 x 412 x 3.
(412, 412, 3)
509232
(3)dtype
Get the data type of the image with the dtype keyword, which usually returns uint8.
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy #Read the picture img = ("") # Get image shape print() # Get the number of pixels print() # Get image data type print()
IV. Image Channel Separation and Merging
OpenCV implements the processing of image channels through the split() function and merge() function, including channel separation and channel merging.
(1) split() function
The color image read by OpenCV consists of three primary colors, blue (B), green (G), and red (R), and each color can be considered as a channel component [4], as shown in Figure 5-8.
The split() function is used to split a multi-channel array into three single channels, and its function prototype is shown below:
mv = split(m[, mv])
- m denotes the multichannel array of inputs
- mv denotes the output array or vector container
The following code is to obtain the color "small celluloid" image of three color channels and display them separately.
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy #Read the picture img = ("") # Split Channel b, g, r = (img) # Show original image ("B", b) ("G", g) ("R", r) # Waiting for the display (0) ()
The display result is shown in Figure 5-9, which demonstrates the color components of the B, G, and R channels.
At the same time, you can get different channel colors with the core code:
- b = (a)[0]
- g = (a)[1]
- r = (a)[2]
(2) merge() function
This function is the inverse operation of the split() function, which synthesizes multiple arrays into an array of channels, thus realizing the merging of image channels, and its function prototype is as follows:
dst = merge(mv[, dst])
- mv denotes the input array to be merged, all matrices must have the same size and depth
- dst means to output an array with the same size and depth as mv
The code to implement the fusion of the three color channels of the image is as follows:
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy as np #Read the picture img = ("") # Split Channel b, g, r = (img) #Merge channels m = ([b, g, r]) ("Merge", m) # Waiting for the display (0) ()
The display result is shown in Figure 5-10, which combines the color components of the split B, G, and R channels, and then displays the combined image.
At the same time, you can call the function to extract different colors of the image, such as extracting the B color channel, G, B channel set to 0. The code is shown below:
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy as np #Read the picture img = ("") rows, cols, chn = # Split Channel b = (img)[0] # Set g and r channels to 0 g = ((rows,cols), dtype=) r = ((rows,cols), dtype=) #Merge channels m = ([b, g, r]) ("Merge", m) # Waiting for the display (0) ()
At this time the image displayed for the blue channel, as shown in Figure 5-11, the other colors of the channel method is similar.
V. Image type conversion
In our daily life, most of the color images we see are of RGB type, but in the process of image processing, it is often necessary to use grayscale images, binary images, HSV, HSI and other colors. Image type conversion refers to the conversion of one type to another, such as color images to grayscale images, BGR images to RGB images.OpenCV provides more than 200 different types of conversions between types, the most commonly used include 3 categories, as follows:
- cv2.COLOR_BGR2GRAY
- cv2.COLOR_BGR2RGB
- cv2.COLOR_GRAY2BGR
OpenCV provides the cvtColor() function to implement these functions. The function prototype is shown below:
dst = (src, code[, dst[, dstCn]])
- src denotes the input image, the original image to be color space transformed
- dst represents the output image with the same size and depth as src
- code indicates the code or logo of the conversion
- dstCn denotes the number of target image channels, and its value is 0, then there are src and code decisions
The role of the function is to convert an image from one color space to another color space, where RGB refers to Red, Green and Blue, an image consists of these three channels (channel); Gray means that there is only a gray value of a channel; HSV contains Hue (hue), Saturation (saturation) and Value (brightness) three channels. In OpenCV, common color space conversion identifiers include CV_BGR2BGRA, CV_RGB2GRAY, CV_GRAY2RGB, CV_BGR2HSV, CV_BGR2XYZ, CV_BGR2HLS [3].
Here is the code that calls the cvtColor() function to grayscale the image.
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy as np import as plt #Read the picture src = ('') #Image type conversion result = (src, cv2.COLOR_BGR2GRAY) #Display Image ("src", src) ("result", result) # Waiting for the display (0) ()
The output is shown in Figure 5-12, which converts the color image on the left to a grayscale image on the right, and more grayscale conversion algorithms will be described in detail in a later article.
Similarly, the following core code can be called to convert a color image to HSV color space, as shown in Figure 5-13.
grayImage = (src, cv2.COLOR_BGR2HSV)
The following code compares nine common color spaces, including BGR, RGB, GRAY, HSV, YCrCb, HLS, XYZ, LAB and YUV, and loops through the processed image.
# -*- coding:utf-8 -*- # By:Eastmount import cv2 import numpy as np import as plt #Read the original image img_BGR = ('') #BGR to RGB conversion img_RGB = (img_BGR, cv2.COLOR_BGR2RGB) # Graying out img_GRAY = (img_BGR, cv2.COLOR_BGR2GRAY) #BGR to HSV img_HSV = (img_BGR, cv2.COLOR_BGR2HSV) #BGR to YCrCb img_YCrCb = (img_BGR, cv2.COLOR_BGR2YCrCb) #BGR to HLS img_HLS = (img_BGR, cv2.COLOR_BGR2HLS) #BGR to XYZ img_XYZ = (img_BGR, cv2.COLOR_BGR2XYZ) #BGR to LAB img_LAB = (img_BGR, cv2.COLOR_BGR2LAB) #BGR to YUV img_YUV = (img_BGR, cv2.COLOR_BGR2YUV) # Call matplotlib to display processing results titles = ['BGR', 'RGB', 'GRAY', 'HSV', 'YCrCb', 'HLS', 'XYZ', 'LAB', 'YUV'] images = [img_BGR, img_RGB, img_GRAY, img_HSV, img_YCrCb, img_HLS, img_XYZ, img_LAB, img_YUV] for i in range(9): (3, 3, i+1), (images[i], 'gray') (titles[i]) ([]),([]) ()
The results of its operation are shown in Figure 5-14:
VI. Summary
This chapter explains the basic image processing in Python and OpenCV, from reading display images to reading and modifying pixels, from creating, copying, and saving images to obtaining image attribute merge channels, and then explains image arithmetic and logical operations in detail, including image addition, subtraction, and operations, or operations, different-or operations, and non-operations, and finally, explains image fusion and obtaining image ROI regions and image Type Conversion. The knowledge in this chapter for the subsequent image processing, image recognition, image transformation to lay a solid foundation.
Above is Python image processing of image fusion and ROI region drawing details, more information about Python image processing please pay attention to my other related articles!