OpenCV-Python教程导读-2 初识图像操作

#主要内容
介绍对图像的基本操作,imread(),imwrite(),imshow()的用法,并结合Matplotlib显示图像。

#总结

1
2
3
4
imread('image_path',format)
imshow('window_name',image_matrix)
imwrtie('image_write_path',image_matrix)
namedWindow('window_name',window_format)

#imread()

1
imread('image_path',format)

Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.

Second argument is a flag which specifies the way image should be read.

cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel

Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.

#imshow()

1
imshow('window_name',image_matrix)

First argument is a window name which is a string. second argument is our image. You can create as many windows as you wish, but with different window names

#imwrite()

1
imwrtie('image_write_path',image_matrix)

First argument is the file name, second argument is the image you want to save.

#waitKey()

cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a key stroke. It can also be set to detect specific key strokes like, if key a is pressed etc which we will discuss below.

waitKey()可以返回按键键值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np
import cv2
img = cv2.imread('city.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
while True:
k = cv2.waitKey(0)
if k == 27: # if 'Esc' is pressed
cv2.destroyAllWindows()
break
elif k == ord('s'): # if 's' is pressed
cv2.imwrite('city.png',img)
cv2.destroyAllWindows()
break

警告 If you are using a 64-bit machine, you will have to modify k = cv2.waitKey(0) line as follows : k = cv2.waitKey(0) & 0xFF

#destroyAllWindows()

cv2.destroyAllWindows() simply destroys all the windows we created. If you want to destroy any specific window, use the function cv2.destroyWindow() where you pass the exact window name as the argument.

#窗口尺寸调整

There is a special case where you can already create a window and load image to it later. In that case, you can specify whether window is resizable or not. It is done with the function cv2.namedWindow(). By default, the flag is cv2.WINDOW_AUTOSIZE. But if you specify flag to be cv2.WINDOW_NORMAL, you can resize window. It will be helpful when image is too large in dimension and adding track bar to windows.

see the code below

1
2
3
4
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

#Using Matplotlib

安装matplotlib
pip install matplotlib

示例代码如下

1
2
3
4
5
6
7
8
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('messi5.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()

警告 Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV. Please see the exercises for more details.

使用Matplotlib显示OpenCV彩色图片可能会遇到一些潜在问题

There is some problem when you try to load color image in OpenCV and display it in Matplotlib. Read this discussion and understand it.

解决方法是一个小技巧调换三个通道的顺序
img2 = img[:,:,::-1]

#参考资料
[1] OpenCV-Python Tutorial:Getting Started with Images
[2] Matplotlib pyplot doc
[3] 关于Matplotlib显示OpenCV图像的stackoverflow的讨论