打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python OpenCV cv2.imread()用法及代码示例

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)

输出:



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python使用opencv进行人脸识别
Python 3 显示图像的方法
Python
Python+opencv 图像处理(五)
人脸检测原理及示例(OpenCV+Python)
Opencv 读取灰度图像会识别为3通道问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服