It is used to wait for the key. When the user presses the keyboard, the statement will be executed and its return value will be obtained. When delay > 0, the program will wait for the user's key to trigger within a given delay time or wait for a delay time, and the program will continue to execute. If delay = 0, it means that the user must click the keyboard to trigger the program to continue.
cv2.waitKey(5000) # 5000ms, 5s program continues to execute. Of course, you can click the keyboard to execute immediately
4.cv2.destroyWindow()
Used to release or destroy the specified window. winname is the name of the window
None = cv2.destroyWindow(winname)
5.cv2.destroyAllWindows()
Used to release or destroy all windows
None = cv2.destroyAllWindows()
6.cv2.imwwrite()
Used to save images. retval is the return value. If the save is successful, the logical value True (True) will be returned; If the save is unsuccessful, the logical value False is returned
filename is the pathname of the target file to save, including the file extension. img is the name of the saved image.
params is a save type parameter and is optional.
retval = cv2.imwwrite(filename,img[, params])
Operational case
import cv2 as cv import numpy as np img = np.ones([400, 400, 1], np.uint8) img = img*0 # img[:, :, 0] = np.ones([400, 400])*127 cv.imshow("new image", img) cv.waitKey(5000) # Click 5000S and the program can be executed immediately cv.imwrite("D:/myImage.png", img)
7. Image classification
1. Binary image
Binary image (black and white only)
White pixels are 1 and black pixels are 0
2. Gray image
Grayscale image (the grayscale processing is 256 grayscale levels (exactly one byte), which is represented by the numerical interval [0255],
Among them. [0] represents pure black and [255] represents pure white
3. Color image
Three primary colors of color image (red, green and blue)
In RGB color space, there are R red channel, G green channel and B blue channel
Common 256256256 = 16777216 colors can be prepared after different combinations
The channel order is R - > G - > b, for example (205, 89, 68)
8. Pixel processing
#Generate an 8 * 8 two-dimensional array, all values are 0,,, and the data type is NP Uint8, (in fact, it can be regarded as a black image.) #img[0, 3] = 255 is to set the pixels in row 0 and column 3 to 255 #img = np.zeros((8, 8), dtype=np.uint8) import cv2 import numpy as np img = np.zeros((8, 8), dtype=np.uint8) print("img = \n", img) cv2.imshow("one",img) print("Read pixels img[0, 3]=", img[0, 3]) img[0, 3] = 255 print("After modification img = \n", img) print("Read the modified pixels img [0, 3] = ",img[0, 3]) cv2.imshow("two", img) cv2.waitKey() cv2.destroyAllWindows()
9. Editing an image
(that is to change a whole piece into a certain color)
import cv2 img = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG", 0) cv2.imshow("before", img) for i in range(10, 100): for j in range(80, 100): img[i, j] = 255 cv2.imshow("after", img) cv2.waitKey() cv2.destroyAllWindows()
10. Create three monochrome pictures
Among them! The first two: represent the number of all rows and columns of the selected picture, and 1 represents the second channel (0 represents the first channel).
If there is a picture img, img[20:30, 60:70, 1] indicates the 20th to 30th rows, 60th to 70th columns and the second channel of the selected picture.
#[300 (number of rows of picture), 300 (number of columns of picture, 3 (number of channels of picture)] import numpy as np import cv2 # ---------------blue--------------- blue = np.zeros((300, 300, 3), dtype=np.uint8) blue[:, :, 0] = 255 print("blue = \n", blue) cv2.imshow("blue", blue) # -------------green--------------- green = np.zeros((300, 300, 3), dtype=np.uint8) green[:, :, 1] = 255 print("green = \n", green) cv2.imshow("green", green) # -------------red--------------- red = np.zeros((300, 300, 3), dtype=np.uint8) red[:, :, 2] = 255 print("red = \n", red) cv2.imshow("red", red) # -------------Release window--------------- cv2.waitKey() cv2.destroyAllWindows()