参考:
1、http://docs.opencv.org/3.3.0/ 官方文档api
2、http://docs.opencv.org/3.3.0/d6/d00/tutorial_py_root.html 官方英文教程
3、https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html
4、https://github.com/makelove/OpenCV-Python-Tutorial# 进阶教程
5、https://docs.opencv.org/3.3.0/index.html 官方英文教程
6、https://github.com/abidrahmank/OpenCV2-Python-Tutorials
7、https://www.learnopencv.com/
8、http://answers.opencv.org/questions/ OpenCV论坛
注:安装的版本 opencv_python-3.3.0-cp36-cp36m-win_amd64.whl
参考:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html
图像入门
目的
- Here, you will learn how to read an image, how to display it and how to save it back
- You will learn these functions : cv2.imread(), cv2.imshow() , cv2.imwrite()
- Optionally, you will learn how to display images with Matplotlib
使用OpenCV
读取一张图像
Use the function cv2.imread() to read an image.
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.默认彩色加载 (1)
- cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode灰度 (0)
- cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel包括透明度通道 (-1)
import numpy as np import cv2 import matplotlib.pyplot as plt # Load an color image in grayscale img = cv2.imread('messi5.jpg',0) # 以灰度图加载 print(img.shape) cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() img2=plt.imread('messi5.jpg') plt.subplot(2,1,1) plt.imshow(img2) plt.axis('off') plt.title('img2') plt.subplot(2,1,2) plt.imshow(img) plt.axis('off') plt.title('img') plt.show()
显示一张图像
Use the function
cv2.imshow()
to display an image in a window. The window automatically fits to the image size.
#!/usr/bin/python3 # -*- coding: utf-8 -*- import cv2 # Load an color image in grayscale img = cv2.imread('messi5.jpg',0) # 以灰度图加载 print(img.shape)cv2.namedWindow('image', cv2.WINDOW_NORMAL) # By default, the flag is cv2.WINDOW_AUTOSIZE.cv2.imshow('image',img) #默认会创建一个窗体cv2.waitKey(0) # 等待键盘响应时间(单位为 ms),0表示无限等待# cv2.destroyAllWindows() # 摧毁所有创建的窗口cv2.destroyWindow('image') # 指明要摧毁的窗口
写一张图
Use the function
cv2.imwrite()
to save an image.
# 第一个参数保存的文件名, 第二个参数要保存的图像. cv2.imwrite('messigray.png',img)
This will save the image in PNG format in the working directory.
总结一下
import numpy as np import cv2 img = cv2.imread('messi5.jpg',0) # 加载成灰度图 cv2.imshow('image',img) k = cv2.waitKey(0) # 返回按键的ASCII值 64位机器 要改成 k = cv2.waitKey(0) & 0xFF if k == 27: # wait for ESC key to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for 's' key to save and exit ord 转成ASCII值 cv2.imwrite('messigray.png',img) cv2.destroyAllWindows()
使用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') # to hide tick values on X and Y axis 相当于 plt.axis('off') plt.xticks([]), plt.yticks([]) plt.show()
注:Color image loaded by OpenCV is in BGR mode. But Matplotlib displays inRGB mode.
Additional Resources
Exercises
- There is some problem when you try to load color image in OpenCV and display it in Matplotlib. Read this discussion and understand it.
import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('messi5.jpg',1) # BGR加载 b,g,r = cv2.split(img) # 通道分离 merged = cv2.merge((r,g,b)) # 合并成RGB # img2=np.append(img[:,:,-1],img[:,:,1],axis=2) # BGR 转成 RGB # img2=np.append(img2,img[:,:,0],axis=2) # BGR 转成 RGB plt.subplot(121) plt.imshow(img) # to hide tick values on X and Y axis 相当于 plt.axis('off') plt.xticks([]), plt.yticks([]) plt.subplot(122) plt.imshow(merged) plt.axis('off') plt.show()