程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python - OpenCV image operation

編輯:Python

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

OpenCV Basic use

Read the picture

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 

drawing

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved