retval 是返回值,其值是读取到的图像,如果未读取到图像,则返回“None”
filename表示要读取的图像的完整文件名
flags 是读取标记用来控制读取文件的类型
import cv2
# 自定义显示图像函数
from matplotlib.pyplot import imshow, show, xticks, yticks
def show_cv2_img(img):
img_shape = img.shape
if len(img_shape)==3:
img = img[:,:,::-1]
imshow(img)
xticks([])
yticks([])
show()
else:
imshow(img)
xticks([])
yticks([])
show()
pic = 'data/dog.jpg'
img = cv2.imread(pic)
print(img)
[[[ 54 52 74]
[ 55 53 75]
[ 56 54 76]
...
[ 61 59 81]
[ 61 59 81]
[ 60 58 80]]
[[ 54 52 74]
[ 55 53 75]
[ 56 54 76]
...
[ 62 60 82]
[ 62 60 82]
[ 62 60 82]]
[[ 55 53 75]
[ 56 54 76]
[ 57 55 77]
...
[ 63 61 83]
[ 63 61 83]
[ 64 62 84]]
...
[[138 184 208]
[137 183 207]
[137 183 207]
...
[140 187 215]
[138 185 213]
[137 184 212]]
[[140 186 210]
[139 185 209]
[139 185 209]
...
[141 188 216]
[139 186 214]
[138 185 213]]
[[140 186 210]
[139 185 209]
[139 185 209]
...
[142 189 217]
[140 187 215]
[139 186 214]]]
cv2.namedWindow('lesson')
cv2.destroyAllWindows()
cv2.imshow('dog', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imshow('PressA', img)
key = cv2.waitKey()
cv2.destroyAllWindows()
if key==ord('A'):
print('PressA')
elif key==ord('B'):
print('PressB')
PressA
cv2.imshow('PressA', img)
key = cv2.waitKey(2000) # 设置延迟2s(注意单位为毫秒)
cv2.destroyAllWindows()
if key != -1:
print('触发了按键')
触发了按键
cv2.imshow('dog', img)
key = cv2.waitKey()
cv2.destroyWindow('dog')
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite('tmp/tmp.jpg', img)
True