Previous portal:
“Python Image Processing OpenCV (1) : Getting Started”
“Python Image Processing OpenCV (2) : Pixel Processing with Numpy manipulation and Matplotlib Display images”
“Python Image Processing OpenCV (3) : Image Properties, ROI Regions of Interest and Channel Processing”
Image addition
There are two ways to add images, one is to add the two images directly through Numpy, and the other is to add the two images through OpenCV’s add() function.
Regardless of the method used, the two images must have the same depth and type, which means the image must be the same size and type.
Numpy addition
Numpy is computed by img = img1 + img2, and then modulo the final result.
- When the final pixel value <= 255, the calculation result is directly
img1 + img2
。 - When the final pixel value is greater than 255, the result of the operation needs to take the modulus of 255.
OpenCV addition
OpenCV operates by calling add() directly, which is saturated.
- When the final pixel value <= 255, the calculation result is directly
img1 + img2
。 - When the final pixel value is greater than 255, it is saturated and the result is fixed at 255.
The following is an example of the two addition methods:
import cv2 as cv
# fetch image
img = cv.imread("maliao.jpg", cv.IMREAD_UNCHANGED)
test = img
# Numpy addition
result1 = img + test
# OpenCV addition
result2 = cv.add(img, test)
# display image
cv.imshow("img", img)
cv.imshow("result1", result1)
cv.imshow("result2", result2)
# wait for display
cv.waitKey()
cv.destroyAllWindows()
Copy the code
The results are as follows:
As you can see, Numpy modulo addition is more green overall, while OpenCV saturation addition is more white overall.
Image fusion
Image fusion is actually a kind of image addition, but it is different from image addition is to give different weight to the image, can make the image with fusion or transparent feeling.
Image addition: img = IMg1 + img2
Image fusion: img = IMg1 * alpha + IMG2 * beta + gamma
The function used for image fusion is addWeighted() as follows:
dst = cv.addWeighter(img1, alpha, img2, beta, gamma)
dst = img1 * alpha + img2 * beta + gamma
Copy the code
Here alpha and beta are both coefficients, while Gamma is a brightness regulator and cannot be omitted.
In the following example, I took another picture of rain and used it to make an image fusion with Mario:
import cv2 as cv
# fetch image
img1 = cv.imread("maliao.jpg", cv.IMREAD_UNCHANGED)
img2 = cv.imread("rain.jpg", cv.IMREAD_UNCHANGED)
# Image fusion
img = cv.addWeighted(img1, 0.4, img2, 0.6.10)
# display image
cv.imshow("img1", img1)
cv.imshow("img2", img2)
cv.imshow("img", img)
# wait for display
cv.waitKey()
cv.destroyAllWindows()
Copy the code
The results are as follows:
In image fusion, attention should be paid to the same as above, and the image size should be equal. In the above example, the two images are both 560 * 310 pixels and RGB images.
Change color space
There are more than 150 color space conversion methods in OpenCV. Let’s start with two of the most common: BGR <-> grayscale and BGR <-> HSV.
To change the color space, we use the cvtColor(input_image, flag) function, where flag is the type of transformation.
Some common flag values:
# BGR to grayscale
cv.COLOR_BGR2GRAY
# BGR HSV
cv.COLOR_BGR2HSV
# BGR RGB
cv.COLOR_BGR2RGB
# Grayscale to BGR
cv.COLOR_GRAY2BGR
Copy the code
As you can clearly see, the naming of flag is very straightforward. If you want to get all the other flags, you can use the following code:
import cv2 as cv
flags = [i for i in dir(cv) if i.startswith('COLOR_')]
print(flags)
Copy the code
The result is not pasted, quite long.
Note: HSV has a hue range of [0,179], a saturation range of [0,255] and a value range of [0,255]. Different software uses different ranges. Therefore, if you want to compare OpenCV values to them, you need to standardize these ranges.
We use the cvtColor() function to convert Mario to a grayscale image as shown in the following example:
import cv2 as cv
# fetch image
img = cv.imread("maliao.jpg", cv.IMREAD_UNCHANGED)
# Image type conversion
result = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
# Image display
cv.imshow("img", img)
cv.imshow("result", result)
# wait for display
cv.waitKey()
cv.destroyAllWindows()
Copy the code
The results are as follows:
The sample code
If you need to get the source code, you can reply “OpenCV” on the public account to get it.
reference
Blog.csdn.net/Eastmount/a…
woshicver.com/