OpenCV-Python是旨在解決計算機視覺問題的Python綁定庫。
cv2.imread()
方法從指定的文件加載圖像。如果無法讀取圖像(由於缺少文件,權限不正確,格式不受支持或格式無效),則此方法將返回一個空矩陣。
用法: cv2.imread(path, flag)
參數:
path:一個字符串,代表要讀取的圖像的路徑。
flag:它指定應該讀取圖像的方式。默認值為cv2.IMREAD_COLOR
返回值:此方法返回從指定文件加載的圖像。
注意:該映像應位於工作目錄中,或者應提供完整的映像路徑。
所有三種類型的標誌描述如下:
cv2.IMREAD_COLOR: It specifies to load a color image. Any transparency of image will be neglected. It is the default flag. Alternatively, we can pass integer value 1 for this flag.
cv2.IMREAD_GRAYSCALE: It specifies to load an image in grayscale mode. Alternatively, we can pass integer value 0 for this flag.
cv2.IMREAD_UNCHANGED: It specifies to load an image as such including alpha channel. Alternatively, we can pass integer value -1 for this flag.
用於以下所有示例的圖像:
示例1:使用默認標誌
# Python program to explain cv2.imread() method
# importing cv2
import cv2
# path
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks.png'
# Using cv2.imread() method
img = cv2.imread(path)
# Displaying the image
cv2.imshow('image', img)
輸出:
示例2:
以灰度模式加載圖像
# Python program to explain cv2.imread() method
# importing cv2
import cv2
# path
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks.png'
# Using cv2.imread() method
# Using 0 to read image in grayscale mode
img = cv2.imread(path, 0)
# Displaying the image
cv2.imshow('image', img)
輸出:
相關用法
- Python OpenCV cv2.rectangle()用法及代碼示例
- Python OpenCV cv2.cvtColor()用法及代碼示例
- Python OpenCV cv2.copyMakeBorder()用法及代碼示例
- Python OpenCV cv2.line()用法及代碼示例
- Python OpenCV cv2.erode()用法及代碼示例
- Python OpenCV cv2.blur()用法及代碼示例
- Python OpenCV cv2.arrowedLine()用法及代碼示例
- Python OpenCV cv2.imshow()用法及代碼示例
- Python OpenCV cv2.ellipse()用法及代碼示例
- Python OpenCV cv2.putText()用法及代碼示例
- Python OpenCV cv2.circle()用法及代碼示例
- Python OpenCV cv2.imwrite()用法及代碼示例
注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 Python OpenCV | cv2.imread() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。