Python | cv2.imread()方法
时间:2020-02-23 14:42:14 来源:igfitidea点击:
在本教程中,我们将在Python编程语言中查看如何使用Python中作为CV2(计算机视觉)库存在的Open-CV读取图像。
我们可以用 imread()
用于读取图像的CV2库的方法,首先我们必须在Python文件中导入CV2库 import statement
。
现在让我们看看语法和返回值 imread()
方法,然后我们将继续执行示例。
语法
cv2.imread(path ,flag)
参数
我们可以通过两个参数 imread()
方法。
在这两个中,PATH参数是强制性的,而标志参数是可选的。
path:
以字符串形式的系统中图像的位置。
其中这两个案例可以:i)
如果在当前工作目录中存在图像,则必须仅提及图像的名称,其扩展名像.jpg,.png等。ii)
如果在系统中的其他位置存在于当前工作目录中的其他位置时,则必须提供完整的路径,也称为图像的绝对路径。flag:
这指定了必须从系统读取图像的方式。
返回值
该方法返回代表给定图像的像素矩阵。
像素只不过是图像的最小单位。
标志参数
让我们看看我们可以传递哪些值来传递给标志参数:
标识 | 描述 |
---|---|
cv2.imread_unchanged | 此标志用于返回所加载的图像(使用Alpha通道,否则会裁剪)。 。或者,我们可以为此标志传递整数值-1. |
cv2.imread_grayscale | 此标志用于以灰度格式返回图像。alternative,我们可以将整数值0传递到此标志。 |
cv2.imread_color | 此标志用于以BGR颜色格式返回图像。它是默认标志。或者,我们可以将整数值1传递给此标志。 |
cv2.imread_anydepth | 当输入具有相应的深度时,此标志用于返回16位/32位图像,否则将其转换为8位。才能将整数值2传递给此标志。 |
cv2.imread_anycolor | 此标志用于以任何可能的颜色格式返回图像。alternative,我们可以为此标志传递整数值4. |
cv2.imread_load_gdal | 此标志用于加载图像的GDAL驱动程序。或者,我们可以将Integer值8传递给此标志。 |
cv2.imread_reduced_grayscale_2 | 此标志用于以灰度格式返回图像,并且图像大小减少到原始图像尺寸的1/2 .alcly,我们可以为此标志传递整数16. |
cv2.imread_reduceed_color_2 | 此标志用于以BGR颜色格式返回图像,并且图像大小减少到原始图像大小的1/2.Allye,我们可以为此标志传递整数17. |
cv2.imread_reduced_grayscale_4 | 此标志用于以灰度格式返回图像,并且图像大小减少到原始图像尺寸的1/4 .alcly,我们可以将整数32传递此标志。 |
cv2.imread_reduced_color_4 | 此标志用于以BGR颜色格式返回图像,并且图像尺寸减少到原始图像大小的1/4.Allye,我们可以为此标志传递整数值33. |
cv2.imread_reduced_grayscale_8 | 此标志用于以灰度格式返回图像,并且图像大小将其减少到原始图像大小的1/8. ,我们可以为此标志传递整数值64. |
cv2.imread_reduced_color_8 | 此标志用于以BGR颜色格式返回图像,并且图像尺寸减少到原始图像大小的1/8.al,我们可以为此标志传递整数值65. |
cv2.imread()方法示例
现在让我们看看Python代码:
示例 1:
读取默认模式下的图像:
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\Users\user\Desktop\flower.jpg" # using imread() method of cv2 image = cv2.imread(img_path) # show the image on the newly created image window cv2.imshow('image window',image)
示例 2:
在灰度模式下读取图像:
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\Users\user\Desktop\flower.jpg" # using imread() method of cv2 image = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE) # show the image on the newly created image window cv2.imshow('image window',image)
示例 3:
读取图像并打印其维度。
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\Users\user\Desktop\flower.jpg" # using imread() method of cv2 image = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE) # print image dimensions print('Image Dimensions :', image.shape)
image.shape
返回包含(高度,宽度,通道数)的元组,所以在前输出中,500像素为高度,300像素是宽度,4个是通道的数量。
示例 4:
读取具有透明频道的图像,我们可以使用 cv2.IMREAD_UNCHANGED
如果存在,则读取图像中的透明度通道。
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\Users\user\Desktop\flower.jpg" # using imread() method of cv2 image = cv2.imread(img_path,cv2.IMREAD_UNCHANGED) # print image dimensions print('Image Dimensions :', image.shape)
彩色频道
imread()
将图像作为输入和解码成矩阵,其中分别以蓝色,绿色,红色和A的顺序存储的颜色通道。image[:,:,0]
代表蓝色频道image[:,:,1]
代表绿色频道image[:,:,2]
代表红色通道image[:,:,3]
代表透明度通道
我们可以使用iMwrite()编写这些通道并检查差异。