Opencv Python 简明教程

OpenCV Python - Reading an image

CV2 程序包(OpenCV-Python 库的名称)提供了 imread() 函数来读取图像。

The CV2 package (name of OpenCV-Python library) provides the imread() function to read an image.

读取图像的命令如下 −

The command to read an image is as follows −

img=cv2.imread(filename, flags)

flags 参数是对以下常量的枚举 −

The flags parameters are the enumeration of following constants −

  1. cv2.IMREAD_COLOR (1) − Loads a color image.

  2. cv2.IMREAD_GRAYSCALE (0) − Loads image in grayscale mode

  3. cv2.IMREAD_UNCHANGED (-1) − Loads image as such including alpha channel

该函数将返回一个图像对象,可以使用 imshow() 函数来渲染该对象。imshow() 函数的命令如下 −

The function will return an image object, which can be rendered using imshow() function. The command for using imshow() function is given below −

cv2.imshow(window-name, image)

图像将在一个已命名窗口中显示。使用 AUTOSIZE 标志集创建一个新的窗口。 WaitKey() 是一种键盘绑定函数。它的参数以毫秒为单位的时间。

The image is displayed in a named window. A new window is created with the AUTOSIZE flag set. The WaitKey() is a keyboard binding function. Its argument is the time in milliseconds.

该函数将等待指定的时间并保持窗口显示,直到按下某个键。最后,我们可以销毁所有已创建的窗口。

The function waits for specified milliseconds and keeps the window on display till a key is pressed. Finally, we can destroy all the windows thus created.

该函数将等待指定的时间并保持窗口显示,直到按下某个键。最后,我们可以销毁所有已创建的窗口。

The function waits for specified milliseconds and keeps the window on display till a key is pressed. Finally, we can destroy all the windows thus created.

显示 OpenCV 徽标的程序如下 −

The program to display the OpenCV logo is as follows −

import numpy as np
import cv2
# Load a color image in grayscale
img = cv2.imread('OpenCV_Logo.png',1)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

上述程序将按如下方式显示 OpenCV 徽标 −

The above program displays the OpenCV logo as follows −

opencv logo