OpenCV in Python I

本文介绍python下opencv环境的搭建

1 什么是OpenCV?

OpenCV是Open Source Computer VisionLibrary的缩写,翻译成中文就是开源计算机视觉库,下面摘录官网(https://opencv.org/about/)的相关描述

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. 
OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products.
Being an Apache 2 licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

1.1 为什么使用Python学习OpenCV

OpenCV支持诸如C++、Python、Java等大量主流编程语言,并且支持不同的操作系统(Window、Linux、Android,etc.)。Python编程简易,可以快速搭建应用原型,降低了学习OpenCV的学习难度。

##2 工具安装及使用
###2.1 安装Anaconda
安装Anaconda没什么好说的,直接官网下载软件包,按照提示一步一步Next执行就可以了。这里需要注意的是,请安装本地环境python版本对应的Anaconda软件包,否则安装过程中会遇到Failed to create menus的问题。还好有好心人已经在
下面网页中给我们整理了各版本python和Anaconda版本的对应关系
https://github.com/koverholt/anaconda-version-map 然后我们可以在 https://repo.anaconda.com/archive/ 下载相应的Anaconda版本即可。

###2.2 安装opencv
打开Anaconda的Anaconda Prompt(切忌不要打开windows默认的prompt,否则opencv包不会安装到Anaconda环境下),执行以下命令

(base) D:\anaconda3>pip install opencv-contrib-python -i https://pypi.tuna.tsinghua.edu.cn/simple

#为了避免下载龟速,上述安装指定了清华的镜像源
#以下验证安装是否成功
(base) D:\anaconda3>python
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.6.0

上述安装OpenCV如遇问题可参考详细文档1,此处补充一个python离线包的资源网址:Archived: Unofficial Windows Binaries for Python Extension Packages

###2.3 Jupyter Notebook使用
启动Jupyter Notebook后台服务,终端显示
Notebook服务
正常情况下浏览器会自动跳转到上述标黄链接,否则请手动浏览器中输入http://localhost:8888 即可打开以下的Notebook页面
Notebook浏览器页面

###2.4 相关reference
OpenCV文档 https://docs.opencv.org/4.x/index.html
Numpy文档 https://www.numpy.org.cn/reference/
matplotlib https://matplotlib.org/stable/api/index.html