14. Image and scalar addition (cv2.add)
The function cv2.add() is used for image addition.
Function Description:
cv2.add(src1, src2 [, dst[, mask[, dtype]]) → dst
The function cv2.add() adds two images of the same size and type, or adds an image to a scalar.
When an image is added to a scalar, the channel values of all pixels of the image are added to the channel values of the scalar respectively.
Parameter Description:
- scr1, scr2: an image for addition, or an image and a numpy array scalar
- dst: output image; optional; the default value is None
- Mask: mask image, 8-bit grayscale format; For a pixel with a mask image value of 0, each channel value of the pixel corresponding to the output image is also 0. Optional. The default value is None
- dtype: the depth of the image array, that is, the number of bits of each pixel value. Optional
- Return value: dst, operation result image, ndarray multidimensional array
matters needing attention:
- There is a difference between OpenCV addition and numpy addition: cv2.add() is a saturation operation (if it is greater than 255 after addition, the result is 255), while numpy addition is a modular operation.
- When adding two pictures using the cv2.add() function, the size and type (number of channels) of the pictures must be the same.
- Use the cv2.add() function to add an image to a scalar, which refers to a 1x3 numpy array. After adding, the image turns white as a whole.
Basic routine: 1.23 image and scalar addition
# 1.23 image addition (adding with scalar) img1 = cv2.imread("../images/imgB1.jpg") # Read color image (BGR) img2 = cv2.imread("../images/imgB3.jpg") # Read color image (BGR) Value = 100 # constant # Scalar = np.array([[50., 100., 150.]]) # scalar Scalar = np.ones((1, 3), dtype="float") * Value # scalar imgAddV = cv2.add(img1, Value) # OpenCV addition: image + constant imgAddS = cv2.add(img1, Scalar) # OpenCV addition: image + scalar print("Shape of scalar", Scalar) for i in range(1, 6): x, y = i*10, i*10 print("(x,y)={},{}, img1:{}, imgAddV:{}, imgAddS:{}" .format(x,y,img1[x,y],imgAddV[x,y],imgAddS[x,y])) plt.subplot(131), plt.title("1. img1"), plt.axis('off') plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)) # Display img1(RGB) plt.subplot(132), plt.title("2. img + constant"), plt.axis('off') plt.imshow(cv2.cvtColor(imgAddV, cv2.COLOR_BGR2RGB)) # Show imgAddV(RGB) plt.subplot(133), plt.title("3. img + scalar"), plt.axis('off') plt.imshow(cv2.cvtColor(imgAddS, cv2.COLOR_BGR2RGB)) # Show imgAddS(RGB) plt.show()
Routine description 1.23:
The running results of this routine are as follows.
Shape of scalar [[150. 150. 150.]] (x,y)=10,10, img1:[ 9 9 69], imgAddV:[159 9 69], imgAddS:[159 159 219] (x,y)=20,20, img1:[ 3 252 255], imgAddV:[153 252 255], imgAddS:[153 255 255] (x,y)=30,30, img1:[ 1 255 254], imgAddV:[151 255 254], imgAddS:[151 255 255] (x,y)=40,40, img1:[ 1 255 254], imgAddV:[151 255 254], imgAddS:[151 255 255] (x,y)=50,50, img1:[ 1 255 255], imgAddV:[151 255 255], imgAddS:[151 255 255]
- Note the difference between "constant" and "scalar" when cv2.add() adds an image to a scalar:
- Adding the image to a constant value only adds the blue component of channel B to the constant, while the values of channels G and R remain unchanged, so the image is blue.
- Add the image to a scalar, "scalar" refers to a 1x3 numpy array. At this time, the B/G/R channels are added to the corresponding constants in the array, so the image is white.
- The scalar numpy array is in the form of NP. Array ([[c1,c2,c3]]), and the constants C1, C2 and C3 can be the same or different.