first, the main function
1, Cv2.imread (): Read into the picture, a total of two parameters, the first parameter is to read the picture file name, the second parameter is how to read the picture, including Cv2. Imread_color: Read in a color picture;Cv2. Imread_grayscale: Read into the image in grayscale mode;Cv2. Imread_unchanged: Read in a picture and include its alpha channel.
2,cv2.imshow (): Create a window to display pictures, a total of two parameters, the first parameter represents the window name, you can create more than one window, but each window cannot have duplicate names; The second parameter is a read-in picture.
3,Cv2.waitkey (): Keyboard binding function, a total of one parameter, indicating the number of milliseconds to wait, will wait for a specific few milliseconds, see if the keyboard has input, the return value is an ASCII value. If its argument is 0, it represents an indefinite wait for keyboard input.
4,cv2.destroyallwindows (): Delete all created Windows.
5,cv2.destroywindows (): Deletes the specified window.
6,Cv2.imwrite (): Save the picture, a total of two parameters, the first to save the file name, the second for reading into the picture.
Second, examples
1, take the following picture as an example
2. Display and save color pictures
123456789101112131415161718 |
# -*- coding: utf-8 -*-
"""
@xiaowuyi:http://www.cnblogs.com/xiaowuyi
"""
import cv2
img
=
cv2.imread(
‘1.jpg‘
,cv2.IMREAD_COLOR)
# 读入彩色图片
cv2.imshow(
‘image‘
,img)
#建立image窗口显示图片
k
=
cv2.waitKey(
0
)
#无限期等待输入
if k
=
=
27
:
#如果输入ESC退出
cv2.destroyAllWindows()
elif k
=
=
ord
(
‘s‘
):
#如果输入s,保存
cv2.imwrite(
‘test.png‘
,img)
print "OK!"
cv2.destroyAllWindows()
|
Show Results:
3. Display and save black and white pictures
123456789101112131415161718 |
# -*- coding: utf-8 -*-
"""
@xiaowuyi:http://www.cnblogs.com/xiaowuyi
"""
import cv2
img
=
cv2.imread(
‘1.jpg‘
,cv2.IMREAD_GRAYSCALE)
# 读入彩色图片
cv2.imshow(
‘image‘
,img)
#建立image窗口显示图片
k
=
cv2.waitKey(
0
)
#无限期等待输入
if k
=
=
27
:
#如果输入ESC退出
cv2.destroyAllWindows()
elif k
=
=
ord
(
‘s‘
):
cv2.imwrite(
‘test.png‘
,img)
print "OK!"
cv2.destroyAllWindows()
|
Show Results:
Opencv-python Study Note 1: Simple picture processing