After configuring all the environments, start the first step of image processing using python+opencv.
Read and display an image:
import cv2 as cv src=cv.imread('E:\imageload\example.png') cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE) cv.imshow('input_image', src) cv.waitKey(0) cv.destroyAllWindows()
Output effect:
Code Explanation:
src=cv.imread('E:\imageload\example.png')
#Read the picture of this path Note that the path here must be all in English, not Chinese, but the separator \ is optional, and it can also be in the form of / \\ // (at least in python3)
cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE)
The #namedWindow function is used to create a window. The default value is WINDOW_AUTOSIZE, so in general, we can fill in the first variable for this function. Actually this
A line of code can be displayed normally without it (imshow will display it below)
cv.imshow('input_image', src)
#Display an image in the specified window
cv.waitKey(0)
# Parameter=0: (It can also be a value less than 0) always displayed, and it will disappear when a key is pressed on the keyboard
Parameter > 0: how many milliseconds to display
cv.destroyAllWindows()
#Delete all created windows and release resources
Note: If the namedWindow and imshow functions are used at the same time, the first parameter name of the two functions must be the same
An important point: Project Encoding must be set to utf-8 in pycahrm, otherwise Pycharm will report an error when commenting Chinese characters in the newly created py file
Of course, if you have created a new py file, to avoid errors, you should comment on the first line of the code: #encoding=gbk