Catalog
OpenCV Basic use
Read the picture
drawing
OpenCV It's a cross platform computer vision library . For developing real-time image processing 、 Computer vision and pattern recognition programs .
install OpenCV modular :pip install opencv-python
Display image is OpenCV The most basic operation ,imshow() Function can realize this operation .imshow() Function has two arguments : Displays the frame name of the image and the image itself to be displayed . Call directly imshow() The function image will disappear immediately . Make sure the picture is always displayed in the window , It can be done by waitKey() function .waitKey() The parameter of the function is the time to wait for the keyboard to trigger , The unit is millisecond , The return value is -1 ( Indicates that no key has been pressed )
import cv2 as cv
img = cv.imread('poo/5.jpg') # The path to read pictures cannot have Chinese
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # Grayscale conversion
cv.imshow('input img', img)
cv.waitKey(0) # Wait for keyboard input , The unit is millisecond Pass in 0 Wait indefinitely
cv.imshow('grayimg', gray_img)
cv.waitKey(0)
resizeimg = cv.resize(gray_img, dsize=(300, 200)) # Change the size of the picture
cv.imshow('grayimg', resizeimg)
cv.waitKey(0)
cv.imwrite('poo/gray_5.jpg', resizeimg) # Save the picture
cv.destroyAllWindows() # C++ Language , Memory must be released after use
OpenCV You can edit the picture at will .
import cv2 as cv
img = cv.imread('poo/6.jpg')
x, y, w, h = 50, 50, 80, 80
cv.putText(img, 'python', (200, 100), cv.FONT_HERSHEY_SCRIPT_SIMPLEX, 5, (255, 255, 0), 10)
cv.rectangle(img, (x, y, x + w, y + h), color=(0, 255, 0), thickness=2) # color-BGR
cv.circle(img, center=(x + w // 2, y + h // 2), radius=w // 2, color=(255, 0, 0), thickness=2)
cv.imshow('', img)
cv.waitKey(3000)
cv.destroyAllWindows()
【 Shangxuetang . Baizhan programmer 】 Gao Qi Python 400 Set ( The end ) # new edition _ Bili, Bili _bilibili