Skip to content

Commit d07c532

Browse files
author
extronwang
committed
Rearrangement and Correction
1 parent 7c773df commit d07c532

File tree

148 files changed

+346
-469
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+346
-469
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [01: 简介与安装](http://codec.wang/opencv-python-introduction-and-installation/)
2+
3+
![](http://blog.codec.wang/cv2_install_opencv-python.jpg)
4+
5+
相信大部分人知道的OpenCV都是用C++来开发的,那为什么我推荐使用Python呢?<!-- more -->
6+
7+
> 本教程翻译自[OpenCV官方英文教程](http://opencv-python-tutroals.readthedocs.io/en/latest/index.html),我按照使用度和难易度翻译,重新编写了大量原创内容,将不常用和较难的部分写成番外篇,浅显易懂,很easy的辣。每节的源码、图片和练习题答案均可在引用处找到噢(⊙o⊙)
8+
9+
---
10+
11+
## Python照样快!
12+
13+
众所周知,虽然Python语法简洁、编写高效,但相比C/C++运行慢很多。然而Python还有个重要的特性:它是一门胶水语言!Python可以很容易地扩展C/C++。OpenCV-Python就是用Python包装了C++的实现,背后实际就是C++的代码在跑,运行速度非常接近原生。
14+
15+
比如我分别用Python和C++实现读入图片和调整图片的亮度对比度,结果如下:
16+
17+
![](http://blog.codec.wang/cv2_python_vs_cplus_speed.jpg)
18+
19+
**可以看到某些情况下Python的运行速度甚至好于C++,代码行数也直接少一半多!**
20+
21+
另外,图像是矩阵数据,OpenCV-Python原生支持[Numpy](https://baike.baidu.com/item/numpy),相当于Python中的Matlab,为矩阵运算、科学计算提供了极大的便利性。
22+
23+
## 人工智能浪潮
24+
25+
近些年,人工智能AI相关技术的快速发展大家有目共睹。在编程语言方面,更多人希望的是具备高效开发效率、跨平台、高度扩展性的语言,尤其是一些AI巨头优先推出支持Python语言的深度学习框架,如Facebook的[PyTorch](https://pytorch.org/)、Google的[Tensorflow](https://tensorflow.google.cn/)等,可以说Python是名副其实的“网红语言”了。
26+
27+
![](http://blog.codec.wang/cv2_ai_ml_dl2.jpg)
28+
29+
[TIOBE编程语言排行榜](https://www.tiobe.com/tiobe-index/)也可以看到,Python发展迅猛,已经逼近C++的份额。这个排行榜每月更新,就不截图了,我编写时的TOP5:Java/C/C++/Python/C#。
30+
31+
## 人生苦短,我用Python
32+
33+
- 如果你搞科研用,果断放弃C++(Matlab?出门左拐)
34+
- 如果你是快速原型开发,验证方案,果断放弃C++
35+
- 如果你懒的配置OpenCV环境,果断放弃C++
36+
- 如果你的程序是在支持Python的较高硬件环境下运行,果断放弃C++
37+
- 如果你担心Python写不了界面,那是你的问题o_o ....
38+
- 除非你的程序是MFC或已经用C++编写其他模块或是嵌入式设备,那就用C++吧
39+
40+
**"人生苦短,我用Python!!!"**
41+
42+
## 安装
43+
44+
> 本教程编写时使用的相关版本是:OpenCV 4.x,Python 3.x。
45+
46+
### opencv-python
47+
48+
只需终端下的一条指令:
49+
50+
``` bash
51+
pip install opencv-python
52+
```
53+
54+
pip是Python的包管理器,如果你还没安装Python,强烈推荐安装[Anaconda](https://www.anaconda.com/download/),它包含了大量的科学计算包,不用后期一个个安装。
55+
56+
### Anaconda安装
57+
58+
进入Anaconda[官网](https://www.anaconda.com/download/),下载最新版本的安装文件,速度比较慢的话,可以去[清华开源镜像站](https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/)
59+
60+
- Windows版是exe文件,双击直接安装,安装时记得勾选 `Add Anaconda to my PATH environment variable`,添加到环境变量。
61+
- Linux版是sh文件,执行`bash Anaconda3-xx.sh`,Linux版也会提示添加到环境变量,记得输yes就行。
62+
- MAC版是pkg文件,同样直接双击安装即可。
63+
64+
### 安装测试
65+
66+
Python的版本可以在终端中输入`python --version`来查看。对于OpenCV,打开Python的开发环境,输入`import cv2`,运行没有报错说明一切正常。要查看OpenCV的版本,可以:
67+
68+
``` bash
69+
print(cv2.__version__)
70+
```
71+
72+
> 编辑器我习惯用[Visual Studio Code](http://code.visualstudio.com/),也可以用[PyCharm](http://www.jetbrains.com/pycharm/)/[Atom](https://atom.io/)/Jupyter Notebook(Anaconda自带)。
73+
74+
### 常见问题
75+
76+
1. pip识别不了:pip的目录没有添加到环境变量中,添加到用户(或系统)变量的path中。
77+
2. 下载速度很慢:可[到此处](https://pypi.org/search/?q=opencv-python)下载离线版。终端输入`pip install 文件名`安装。
78+
79+
## 学习软件
80+
81+
为了便于学习OpenCV,我编写了一款教学软件[LearnOpenCVEdu](https://github.com/codecwang/LearnOpenCVEdu),目前只开发了一部分功能,欢迎Star支持:smiley:
82+
83+
![](http://blog.codec.wang/cv2_learn_opencv_edu_soft_screenshot.jpg)
84+
85+
> 经验之谈:虽然我推荐大家使用OpenCV-Python进行图像处理,但想要深入理解OpenCV,C++是必须的,尤其是**OpenCV源码**
86+
87+
## 引用
88+
89+
- [本节源码](https://github.com/codecwang/OpenCV-Python-Tutorial/tree/master/01-Introduction-and-Installation)
90+
91+
### 网络资料
92+
93+
- [OpenCV Docs官方文档](https://docs.opencv.org/)
94+
- [OpenCV源码](https://github.com/opencv/opencv)
95+
- [官方英文教程: OpenCV-Python Tutorials](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html)
96+
- [LearnOpenCV](http://www.learnopencv.com)[LearnOpenCV Github](https://github.com/spmallick/learnopencv)
97+
- [OpenCV 中文教程](http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/tutorials.html)
98+
99+
### 书籍
100+
101+
- [Programming Computer Vision with Python](http://programmingcomputervision.com/)[中文书](https://www.amazon.cn/dp/B00L3Y3NEM/ref=sr_1_1?ie=UTF8&qid=1543929834&sr=8-1&keywords=Python+%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89)
102+
- [Practical Python and OpenCV](https://www.pyimagesearch.com/practical-python-opencv/)
103+
104+
### 名校视觉研究所/课程
105+
106+
- [卡内基梅隆大学](http://graphics.cs.cmu.edu/)
107+
- [多伦多大学](https://www.cs.toronto.edu/~guerzhoy/320/)

01. 简介与安装/cv2_contrast_brightness_time.cpp renamed to 01-Introduction-and-Installation/cv2_contrast_brightness_time.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// ex2tron's blog:
2-
// http://ex2tron.wang
3-
41
#include "stdafx.h"
52
#include <opencv2\opencv.hpp>
63

01. 简介与安装/cv2_contrast_brightness_time.py renamed to 01-Introduction-and-Installation/cv2_contrast_brightness_time.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52
import numpy as np
63

01. 简介与安装/cv2_read_image_time.cpp renamed to 01-Introduction-and-Installation/cv2_read_image_time.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// ex2tron's blog:
2-
// http://ex2tron.wang
3-
41
#include "stdafx.h"
52
#include <opencv2\opencv.hpp>
63

01. 简介与安装/cv2_read_image_time.py renamed to 01-Introduction-and-Installation/cv2_read_image_time.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63
# 开始计时

01. 简介与安装/README.md

Lines changed: 0 additions & 104 deletions
This file was deleted.

02. 基本元素-图片/README.md renamed to 02-Basic-Element-Image/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ img = cv2.imread('lena.jpg', 0)
3232

3333
- 参数1:图片的文件名
3434

35-
- 如果图片放在当前文件夹下,直接写文件名就行了,如'lena.jpg'
35+
- 如果图片放在当前文件夹下,直接写文件名就行,如'lena.jpg'
3636
- 否则需要给出绝对路径,如'D:\OpenCVSamples\lena.jpg'
3737

3838
- 参数2:读入方式,省略即采用默认值
@@ -99,5 +99,5 @@ Nice,是不是很简单呐,再接再厉噢(●'◡'●)
9999

100100
## 引用
101101

102-
- [本节源码](https://github.com/ex2tron/OpenCV-Python-Tutorial/tree/master/02.%20%E5%9F%BA%E6%9C%AC%E5%85%83%E7%B4%A0-%E5%9B%BE%E7%89%87)
102+
- [本节源码](https://github.com/codecwang/OpenCV-Python-Tutorial/tree/master/02-Basic-Element-Image)
103103
- [Getting Started with Images](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html)

02. 基本元素-图片/cv2_excises1.py renamed to 02-Basic-Element-Image/cv2_excises1.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63
img = cv2.imread('lena.jpg')

02. 基本元素-图片/cv2_getting_start_with_images.py renamed to 02-Basic-Element-Image/cv2_getting_start_with_images.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63
# 1.灰度图加载一张彩色图
File renamed without changes.

03. 打开摄像头/README.md renamed to 03-Open-Camera/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ while(capture.isOpened()):
115115

116116
## 引用
117117

118-
- [本节源码](https://github.com/ex2tron/OpenCV-Python-Tutorial/tree/master/03.%20%E6%89%93%E5%BC%80%E6%91%84%E5%83%8F%E5%A4%B4)
118+
- [本节源码](https://github.com/codecwang/OpenCV-Python-Tutorial/tree/master/03-Open-Camera)
119119
- [Video Codecs by FOURCC](http://www.fourcc.org/codecs.php)
120120
- [Getting Started with Videos](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html)

03. 打开摄像头/cv2_capture_live_video_from_camera.py renamed to 03-Open-Camera/cv2_capture_live_video_from_camera.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63
# 1.打开摄像头

03. 打开摄像头/cv2_exercise1.py renamed to 03-Open-Camera/cv2_exercise1.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63

03. 打开摄像头/cv2_play_video_from_file.py renamed to 03-Open-Camera/cv2_play_video_from_file.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63
# 播放本地视频

03. 打开摄像头/cv2_save_video_to_file.py renamed to 03-Open-Camera/cv2_save_video_to_file.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63
capture = cv2.VideoCapture(0)
File renamed without changes.

04. 图像基本操作/README.md renamed to 04-Basic-Operations/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,5 @@ cv2.waitKey(0)
122122

123123
## 引用
124124

125-
- [本节源码](https://github.com/ex2tron/OpenCV-Python-Tutorial/tree/master/04.%20%E5%9B%BE%E5%83%8F%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C)
125+
- [本节源码](https://github.com/codecwang/OpenCV-Python-Tutorial/tree/master/04-Basic-Operations)
126126
- [Basic Operations on Images](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_basic_ops/py_basic_ops.html#basic-ops)

04. 图像基本操作/cv2_basic_operations.py renamed to 04-Basic-Operations/cv2_basic_operations.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63
img = cv2.imread('lena.jpg')

04. 图像基本操作/cv2_exercise1.py renamed to 04-Basic-Operations/cv2_exercise1.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52

63
img = cv2.imread('lena.jpg')
File renamed without changes.

05. 颜色空间转换/README.md renamed to 05-Changing-Colorspaces/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ print(hsv_blue) # [[[120 255 255]]]
113113

114114
## 引用
115115

116-
- [本节源码](https://github.com/ex2tron/OpenCV-Python-Tutorial/tree/master/05.%20%E9%A2%9C%E8%89%B2%E7%A9%BA%E9%97%B4%E8%BD%AC%E6%8D%A2)
116+
- [本节源码](https://github.com/codecwang/OpenCV-Python-Tutorial/tree/master/05-Changing-Colorspaces)
117117
- [Changing Colorspaces](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html)

05. 颜色空间转换/cv2_changing_color_space.py renamed to 05-Changing-Colorspaces/cv2_changing_color_space.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
2+
import numpy as np
53

64
img = cv2.imread('lena.jpg')
75

@@ -18,7 +16,6 @@
1816
print(flags)
1917

2018
# 蓝色的HSV值
21-
import numpy as np
2219

2320
blue = np.uint8([[[255, 0, 0]]])
2421
hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)

05. 颜色空间转换/cv2_exercises1.py renamed to 05-Changing-Colorspaces/cv2_exercises1.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52
import numpy as np
63

File renamed without changes.

06. 阈值分割/README.md renamed to 06-Image-Thresholding/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,5 @@ plt.show()
131131

132132
## 引用
133133

134-
- [本节源码](https://github.com/ex2tron/OpenCV-Python-Tutorial/tree/master/06.%20%E9%98%88%E5%80%BC%E5%88%86%E5%89%B2)
134+
- [本节源码](https://github.com/codecwang/OpenCV-Python-Tutorial/tree/master/06-Image-Thresholding)
135135
- [Image Thresholding](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html)

06. 阈值分割/cv2_thresholding.py renamed to 06-Image-Thresholding/cv2_thresholding.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ex2tron's blog:
2-
# http://ex2tron.wang
3-
41
import cv2
52
import matplotlib.pyplot as plt
63

File renamed without changes.
File renamed without changes.

07. 图像几何变换/README.md renamed to 07-Image-Geometric-Transformation/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,5 @@ cv2.waitKey(0)
115115

116116
## 引用
117117

118-
- [本节源码](https://github.com/ex2tron/OpenCV-Python-Tutorial/tree/master/07.%20%E5%9B%BE%E5%83%8F%E5%87%A0%E4%BD%95%E5%8F%98%E6%8D%A2)
118+
- [本节源码](https://github.com/codecwang/OpenCV-Python-Tutorial/tree/master/07-Image-Geometric-Transformation)
119119
- [Geometric Transformations of Images](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html)

0 commit comments

Comments
 (0)