python版opencv入門(1)


讀入圖片

cv2.imread()

第一個參數:圖片名

第二個參數:

• cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default
flag.
• cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
• cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Note: Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.

1   import numpy as np
2   import cv2
3   img = cv2.imread('test.jpg',0) #顯示一張灰度圖

 顯示圖片

cv2.imshow()

1 cv2.imshow('image',img)
2 cv2.waitKey(0)
3 cv2.destroyAllWindows()

寫入圖片

cv2.imwrite()

1 cv2.imwrite('test.png',img)

打開圖片,處理並保存的整個流程

 1 import numpy as np
 2 import cv2
 3 img = cv2.imread('test.jpg',0)
 4 cv2.imshow('image',img)
 5 k = cv2.waitKey(0)
 6 if k == 27:# wait for ESC key to exit
 7     cv2.destroyAllWindows()
 8 elif k == ord('s'): # wait for 's' key to save and exit
 9     cv2.imwrite('testgray.png',img)
10     cv2.destroyAllWindows()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM