前言
在学习 OpenCV 的时候,需要搭建 OpenCV 的环境并安装一些库,本文就准备了 OpenCV for Python,换而言之就是 OpenCV 的 python 的 API 接口。它拥有 OpenCV C++ API 的功能,同时也拥有 Python 语言的特性。
视频教程
准备工作
安装 Anaconda 配置好国内镜像源
版本信息
Python 版本 3.6
OpenCV 版本 3.4.1.15
OpenCV 扩展包版本 3.4.1.15
dlib 版本 19.6.1
开始搭建
Anaconda 创建 Python 虚拟环境
conda create -n OpenCV3.6 python=3.6 # 通过 anaconda 创建虚拟环境
# 点 y 即可

激活虚拟环境
activate OpenCV3.6 # 激活虚拟环境

安装所需资源库
pip install opencv-python==3.4.1.15
pip install opencv-contrib-python==3.4.1.15
pip install dlib==19.6.1
pip install numpy
pip install matplotlib

检查是否成功
显示 3.4.1 就没问题了
# 检查是否成功
python
import cv2
cv2.__version__

程序测试
# 导入cv模块
import cv2
# 读取图像,支持 bmp、jpg、png、tiff 等常用格式
img = cv2.imread('img\cat.jpg')
# 创建窗口并显示图像
cv2.imshow('image', img)
cv2.waitKey(0)
# 释放窗口
cv2.destroyAllWindows()
