1. Print image properties, set image storage path
code show as below:
#Print image attributes, save image location import cv2 as cv import numpy as np # numpy is an open source Python scientific computing library def get_image_info(image): print (type(image)) # type() function if only the first The parameter returns the type of the object. Here the function displays the image type as an array of numpy type. print (image.shape) #The shape attribute of the image matrix represents the size of the image, and shape will return a tuple tuple, #The first element represents the number of rows in the matrix , the second tuple indicates the number of matrix columns, the third element is 3, indicating that the pixel value is composed of the three primary colors of light print (image.size) #Returns the size of the image, the specific value of size is the product of the three elements of shape print (image.dtype) #The type of the array element is obtained through the dtype attribute pixel_data= np.array(image) print (pixel_data)#Print the image matrix N-dimensional array object that is the matrix object src=cv.imread( ' E:\imageload\example.png ' ) cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE) cv.imshow('input_image', src) get_image_info(src) cv.imwrite( " E:/example.png " ,src) #Image storage path # gray=cv.cvtColor(src,cv.COLOR_BGR2GRAY) #Make the image color gray # cv.imwrite("E:/example .png",gray) cv.waitKey(0) cv.destroyAllWindows()
code show as below:
2. The retrieval and display of the computer camera
#The retrieval and display of the computer camera import cv2 as cv def video_demo(): capture = cv.VideoCapture(0) #The parameter is the id of the video device. If only one camera can be filled with 0, it means that the default camera is opened. The parameter here can also be the video file name path, as long as the specific path of the video file is written in it Good while True: #As long as the loop is not jumped out, each frame will be played in a loop, waitKey(10) means the interval is 10ms ret, frame = capture.read() # The read function reads a frame of the video (camera), it can return Two parameters. The first parameter is a bool type ret, whose value is True or False, which means whether the picture has been read. The second parameter is frame, which is the picture currently intercepting a frame frame=cv.flip(frame, 1 ) #Flip 0: Flip along the X axis (vertical flip) Greater than 0: Flip along the Y axis (horizontal flip) Less than 0: Flip along the X axis first, then flip along the Y axis, which is equivalent to rotating 180° cv.imshow( " video " ,frame) pc =cv.waitKey(10) #After 10ms, the waitKey function will return -1. If a key is pressed on the keyboard within 10ms, the waitKey function will return the ASCII code value of the corresponding key, and the ASCII code value must be greater than 0 if pc> 0: break # if cv.waitKey(10) == ord('z'): # Enter z on the keyboard to exit the window. If you don't press z to click to close, it will never be closed. It can also be set to other keys. The ord() function returns the ASCII value of the corresponding character # break video_demo() cv.destroyAllWindows()