opencv学习笔记1:图片的读、写、存
目标:实现图片读、写、存
1、Here, you will learn how to read an image, how to display it and how to save it back
2、You will learn these functions : cv2.imread(), cv2.imshow() , cv2.imwrite()
3、Optionally, you will learn how to display images with Matplotlib
读取 cv2.imread()
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
除了上面三个flag,我们可以使用-1~1来表示
代码如下:
import numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)
Even if the image path is wrong, it won’t throw any error, but print img will give you None
显示图片
imshow()
Use the function cv2.imshow() to display an image in a window. The window automatically fits to the image size.
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.
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
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.
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.
显示可调整大小的window
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.
写图片 cv2.imwrite
First argument is the file name, second argument is the image you want to save.
cv2.imwrite('messigray.png',img)
这回存成PNG模式的图片
总结:读、写、存例子
下面的代码,以灰度的形式载入图片,显示,如果你按下了s,存储图片,或者直接退出当你按下EXE按键。
import numpy as np
import cv2
img = cv2.imread('messi5.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
使用Matplotlib
Matplotlib是一个绘图库,有很多方法
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()
Matplotlib中有很多方法,请参考文档。
Oencv读BGR,Matplotlib读RGB 格式差异
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.
更多资料参考matplotlkb文档