1. Python 安装 OpenCV
Python 安装 OpenCV 的命令很简单,就一条命令,如下(前置条件是已安装 pip):
pip install opencv-python
2. Python 代码中用 OpenCV 读取图片并显示
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2 as cv
img = cv.imread('demo.png', cv.IMREAD_UNCHANGED) # 按原始颜色格式读取图片
# img = cv.imread('demo.png', cv.IMREAD_GRAYSCALE) # 按灰度图读取图片
print img.shape # 输出:(120, 240, 3)
cv.namedWindow("image")
cv.imshow('image', img)
cv.waitKey (0)
cv.destroyAllWindows()