Skip to content

Commit a75efa4

Browse files
committed
整理
1 parent 70cc18f commit a75efa4

File tree

52 files changed

+631
-33
lines changed

Some content is hidden

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

52 files changed

+631
-33
lines changed

.idea/markdown-navigator-enh.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown-navigator.xml

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### 目标
2+
- 访问和修改图片像素点的值
3+
- 获取图片的宽、高、通道数等属性
4+
- 了解感兴趣区域ROI(Region of Interest)
5+
- 分离和合并图像通道

04-Basic-Operations/cv2_basic_operations.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
img = cv2.imread('lena.jpg')
44

5-
# 1.获取像素的值
5+
# 1.获取像素的值 y = 100, x = 90
66
px = img[100, 90]
7-
print(px) # [103 98 197]
7+
print(px) # [105 98 197] [B G R]
88

9-
# 只获取蓝色blue通道的值
9+
# 只获取蓝色 blue通道的值 img[100, 90, 0]
10+
# 只获取蓝色 green 通道的值 img[100, 90, 1]
11+
# 只获取蓝色 red 通道的值 img[100, 90, 2]
1012
px_blue = img[100, 90, 0]
11-
print(px_blue) # 103
13+
print(px_blue) # 105
1214

1315

1416
# 2.修改像素的值
17+
# 只操作了内存的像素点,原图没有保存没有修改
1518
img[100, 90] = [255, 255, 255]
1619
print(img[100, 90]) # [255 255 255]
1720

@@ -37,7 +40,11 @@
3740
# 5.通道分割与合并
3841
b, g, r = cv2.split(img)
3942
img = cv2.merge((b, g, r))
40-
# 更推荐的获取某一通道方式
43+
# split 比较耗时, 更推荐的获取某一通道方式
4144
b = img[:, :, 0]
45+
g = img[:, :, 1]
46+
r = img[:, :, 2]
4247
cv2.imshow('b', b)
48+
cv2.imshow('g', g)
49+
cv2.imshow('r', r)
4350
cv2.waitKey(0)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### 目标
2+
- 颜色空间转换,如BGR ↔ Gray,BGR ↔ HSV等
3+
- 追踪视频中特定颜色的物体
4+
- OpenCV函数:cv2.cvtColor(), cv2.inRange()
5+
6+
- 经验之谈:颜色转换其实是数学运算,如灰度化最常用的是:gray=R*0.299+G*0.587+B*0.114
7+
8+
#### 视频中特定颜色物体追踪
9+
HSV是一个常用于颜色识别的模型,相比BGR更易区分颜色,转换模式用COLOR_BGR2HSV表示。
10+
色调H:[0, 179] 理论是 0-360
11+
12+
饱和度S:[0, 255]
13+
14+
明度V:[0, 255]
15+

05-Changing-Colorspaces/cv2_changing_color_space.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616
print(flags)
1717

1818
# 蓝色的HSV值
19-
2019
blue = np.uint8([[[255, 0, 0]]])
2120
hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
2221
print(hsv_blue) # [[[120 255 255]]]
2322

2423

2524
# 3.追踪蓝色物体
26-
capture = cv2.VideoCapture(0)
25+
# capture = cv2.VideoCapture(0)
26+
capture = cv2.VideoCapture("http://admin:admin@192.168.1.100:8081")
2727

2828
# 蓝色的范围,不同光照条件下不一样,可灵活调整
29+
# H +- 10, 饱和度和明度灵活调整
2930
lower_blue = np.array([100, 110, 110])
3031
upper_blue = np.array([130, 255, 255])
3132

05-Changing-Colorspaces/cv2_exercises1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
# Green:[[[ 60 255 255]]]
77
# Red:[[[ 0 255 255]]]
88

9-
capture = cv2.VideoCapture(1)
9+
# capture = cv2.VideoCapture(1)
10+
capture = cv2.VideoCapture("http://admin:admin@192.168.1.117:8081")
11+
# cap = cv2.VideoCapture("rtsp://admin:admin@192.168.1.100:8554/live")
1012

1113
# 蓝色的范围
1214
lower_blue = np.array([100, 110, 110])

05-Changing-Colorspaces/exercises2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import cv2
2+
import numpy as np
3+
4+
# blue HSV
5+
blue = np.uint8([[[255, 0, 0]]])
6+
hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
7+
print(hsv_blue)
8+
9+
# green HSV
10+
green = np.uint8([[[0, 255, 0]]])
11+
hsv_green = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
12+
print(hsv_green)
13+
14+
# red HSV
15+
red = np.uint8([[[0, 0, 255]]])
16+
hsv_red = cv2.cvtColor(red, cv2.COLOR_BGR2HSV)
17+
print(hsv_red)
18+
19+
# blue [[[120 255 255]]]
20+
# green [[[ 60 255 255]]]
21+
# red [[[ 0 255 255]]]
22+
23+
# 蓝色的范围
24+
lower_blue = np.array([100, 110, 110])
25+
upper_blue = np.array([130, 255, 255])
26+
27+
# 绿色的范围
28+
lower_green = np.array([40, 90, 90])
29+
upper_green = np.array([70, 255, 255])
30+
31+
# 红色的范围
32+
lower_red = np.array([160, 120, 120])
33+
upper_red = np.array([179, 255, 255])
34+

05-Changing-Colorspaces/test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cv2
2+
# cap = cv2.VideoCapture("rtsp://admin:admin@192.168.1.102:554//Streaming/Channels/1")
3+
# cap = cv2.VideoCapture("http://admin:admin@192.168.1.102:8554")
4+
cap = cv2.VideoCapture("http://admin:admin@192.168.42.129:8081")
5+
# cap = cv2.VideoCapture("rtsp://admin:admin@192.168.1.102:8554/live")
6+
ret,frame = cap.read()
7+
print(ret)
8+
while ret:
9+
ret,frame = cap.read()
10+
cv2.imshow("frame",frame)
11+
if cv2.waitKey(1) & 0xFF == ord('q'):
12+
break
13+
cv2.destroyAllWindows()
14+
cap.release()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### 目标
2+
- 使用固定阈值、自适应阈值和Otsu阈值法"二值化"图像
3+
- OpenCV函数:cv2.threshold(), cv2.adaptiveThreshold()
4+
5+
6+
#### 固定阈值分割
7+
固定阈值分割很直接,一句话说就是像素点值大于阈值变成一类值,小于阈值变成另一类值
8+
9+
#### 自适应阈值
10+
看得出来固定阈值是在整幅图片上应用一个阈值进行分割,_它并不适用于明暗分布不均的图片_``cv2.adaptiveThreshold()``自适应阈值会每次取图片的一小部分计算阈值,这样图片不同区域的阈值就不尽相同
11+
12+
#### Otsu阈值
13+
在前面固定阈值中,我们是随便选了一个阈值如127,那如何知道我们选的这个阈值效果好不好呢?答案是:不断尝试,所以这种方法在很多文献中都被称为经验阈值。Otsu阈值法就提供了一种自动高效的二值化方法,不过我们直方图还没学,这里暂时略过
14+
15+
16+
#### 小结
17+
- cv2.threshold()用来进行固定阈值分割。固定阈值不适用于光线不均匀的图片,所以用 cv2.adaptiveThreshold()进行自适应阈值分割。
18+
- **二值化跟阈值分割并不等同**。针对不同的图片,可以采用不同的阈值方法。

06-Image-Thresholding/cv2_thresholding.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
# 灰度图读入
55
img = cv2.imread('gradient.jpg', 0)
66
# 阈值分割,ret:return value缩写,代表当前的阈值,暂时不用理会
7+
# 参数1:要处理的原图,一般是灰度图
8+
# 参数2:设定的阈值
9+
# 参数3:对于THRESH_BINARY、THRESH_BINARY_INV阈值方法所选用的最大阈值,一般为255
10+
# 参数4:阈值的方式,主要有5种,详情:ThresholdTypes
11+
# 0 是黑色
712
ret, th = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
813
cv2.imshow('thresh', th)
914
cv2.waitKey(0)
@@ -33,6 +38,14 @@
3338
# 固定阈值
3439
ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
3540
# 自适应阈值
41+
# 参数1:要处理的原图
42+
# 参数2:最大阈值,一般为255
43+
# 参数3:小区域阈值的计算方式
44+
# ADAPTIVE_THRESH_MEAN_C:小区域内取均值
45+
# ADAPTIVE_THRESH_GAUSSIAN_C:小区域内加权求和,权重是个高斯核
46+
# 参数4:阈值方法,只能使用THRESH_BINARY、THRESH_BINARY_INV,具体见前面所讲的阈值方法
47+
# 参数5:小区域的面积,如11就是11*11的小块
48+
# 参数6:最终阈值等于小区域计算出的阈值再减去此值
3649
th2 = cv2.adaptiveThreshold(
3750
img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 4)
3851
th3 = cv2.adaptiveThreshold(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### 目标
2+
- 实现旋转、平移和缩放图片
3+
- OpenCV函数:cv2.resize(), cv2.flip(), cv2.warpAffine()
4+
5+
### 小结
6+
- ``cv2.resize()`` 缩放图片,可以按指定大小缩放,也可以按比例缩放。
7+
- ``cv2.flip()`` 翻转图片,可以指定水平/垂直/水平垂直翻转三种方式。
8+
- 平移/旋转是靠仿射变换 ``cv2.warpAffine()`` 实现的。

07-Image-Geometric-Transformation/cv2_geometric_transformations.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
# 2.翻转图片
1616

17-
# 参数2=0:垂直翻转(沿x轴),参数2>0: 水平翻转(沿y轴)
17+
# 参数2=0:垂直翻转(沿x轴),
18+
# 参数2>0: 水平翻转(沿y轴)
1819
# 参数2<0: 水平垂直翻转
1920
dst = cv2.flip(img, -1)
2021
# np.hstack: 横向并排,对比显示
@@ -24,8 +25,9 @@
2425

2526
# 3.平移图片
2627
rows, cols = img.shape[:2]
27-
# 定义平移矩阵,需要是numpy的float32类型
28+
# 定义平移矩阵,需要是 numpy 的 float32 类型
2829
# x轴平移100,y轴平移50
30+
# 仿射变换函数 cv2.warpAffine
2931
M = np.float32([[1, 0, 100], [0, 1, 50]])
3032
dst = cv2.warpAffine(img, M, (cols, rows))
3133

@@ -34,6 +36,9 @@
3436

3537

3638
# 4.45°顺时针旋转图片并缩小一半
39+
# 参数1:图片的旋转中心
40+
# 参数2:旋转角度(正:逆时针,负:顺时针)
41+
# 参数3:缩放比例,0.5表示缩小一半
3742
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -45, 0.5)
3843
dst = cv2.warpAffine(img, M, (cols, rows))
3944

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#### 目标
2+
- 绘制各种几何形状、添加文字
3+
- OpenCV函数:cv2.line(), cv2.circle(), cv2.rectangle(), cv2.ellipse(), cv2.putText()
4+
5+
6+
#### 小结
7+
- cv2.line()画直线,cv2.circle()画圆,cv2.rectangle()画矩形,cv2.ellipse()画椭圆,cv2.polylines()画多边形,cv2.putText()添加文字。
8+
- 画多条直线时,cv2.polylines()要比cv2.line()高效很多。

08-Drawing-Function/cv2_drawing_functions.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,46 @@
22
import numpy as np
33

44
# 创建一副黑色的图片
5+
# 大小是 512x512
56
img = np.zeros((512, 512, 3), np.uint8)
7+
68
# 1.画一条线宽为5的蓝色直线,参数2:起点,参数3:终点
9+
# img, 起点, 终点, 颜色, 宽度
710
cv2.line(img, (0, 0), (512, 512), (255, 0, 0), 5)
811

912

1013
# 2.画一个绿色边框的矩形,参数2:左上角坐标,参数3:右下角坐标
14+
# img, 左上角坐标, 右下角坐标, 颜色, 宽度
1115
cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
1216

1317

1418
# 3.画一个填充红色的圆,参数2:圆心坐标,参数3:半径
19+
# img, 圆心坐标, 半径, 颜色, 宽度(-1是填充)
1520
cv2.circle(img, (447, 63), 63, (0, 0, 255), -1)
1621

1722

1823
# 4.在图中心画一个填充的半圆
24+
# 画椭圆
25+
# img, 圆心坐标, x/y长度, 旋转角度, 起始角度,结束角度,颜色,宽度
1926
cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, (255, 0, 0), -1)
2027

2128

2229
# 5.画一个闭合的四边形
2330
# 定义四个顶点坐标
2431
pts = np.array([[10, 5], [50, 10], [70, 20], [20, 30]], np.int32)
25-
# 顶点个数:4,矩阵变成顶点数*1*2维(注意numpy中-1的用法)
32+
print(pts.shape)
33+
print(pts)
34+
# 顶点个数:4,矩阵变成 4*1*2 维(注意 numpy 中 -1 的用法)
35+
# 1. numpy中 -1 是让数组横向平铺
36+
# 2. OpenCV中需要先将多边形的顶点坐标需要变成顶点数×1×2维的矩阵,再来绘制
2637
pts = pts.reshape((-1, 1, 2))
38+
print(pts.shape)
39+
print(pts)
40+
41+
# 如果第三个参数是 False,多边形就不闭合
2742
cv2.polylines(img, [pts], True, (0, 255, 255))
2843

44+
2945
# 使用cv2.polylines()画多条直线
3046
line1 = np.array([[100, 20], [300, 20]], np.int32).reshape((-1, 1, 2))
3147
line2 = np.array([[100, 60], [300, 60]], np.int32).reshape((-1, 1, 2))
@@ -34,9 +50,10 @@
3450

3551

3652
# 6.添加文字
53+
# img, 文本, 起始坐标, 字体, 大小,颜色, 粗细, LINE_AA 抗锯齿
3754
font = cv2.FONT_HERSHEY_SIMPLEX
3855
cv2.putText(img, 'ex2tron', (10, 500), font,
39-
4, (255, 255, 255), 2, lineType=cv2.LINE_AA)
56+
4, (255, 255, 255), 5, lineType=cv2.LINE_AA)
4057

4158
cv2.imshow('img', img)
4259
cv2.waitKey(0)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import cv2
2+
import numpy as np
3+
# 创建一副黑色的图片
4+
# 大小是 512x512
5+
img = np.zeros((256, 256, 3), np.uint8)
6+
7+
# 画椭圆
8+
# img, 圆心坐标, x/y长度, 旋转角度, 起始角度,结束角度,颜色,宽度
9+
cv2.ellipse(img, (128, 50), (50, 50), 120, 0, 300, (0, 0, 255), -1,lineType=cv2.LINE_AA)
10+
cv2.circle(img, (128, 50), 25, (0, 0, 0), -1,lineType=cv2.LINE_AA)
11+
12+
cv2.ellipse(img, (50, 206), (50, 50), 0, 0, 300, (0, 255, 0), -1,lineType=cv2.LINE_AA)
13+
cv2.circle(img, (50, 206), 25, (0, 0, 0), -1,lineType=cv2.LINE_AA)
14+
15+
cv2.ellipse(img, (256-52, 206), (50, 50), 300, 0, 300, (255, 0, 0), -1,lineType=cv2.LINE_AA)
16+
cv2.circle(img, (256-52, 206), 25, (0, 0, 0), -1,lineType=cv2.LINE_AA)
17+
18+
cv2.imshow('img', img)
19+
cv2.waitKey(0)

09-Image-Blending/09 图像混合.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#### 目标
2+
- 图片间的数学运算,如相加、按位运算等
3+
- OpenCV函数:cv2.add(), cv2.addWeighted(), cv2.bitwise_and()
4+
5+
#### 小结
6+
- cv2.add()用来叠加两幅图片,cv2.addWeighted()也是叠加两幅图片,但两幅图片的权重不一样。
7+
- cv2.bitwise_and(), cv2.bitwise_not(), cv2.bitwise_or(), cv2.bitwise_xor()分别执行按位与/或/非/异或运算。掩膜就是用来对图片进行全局或局部的遮挡。

0 commit comments

Comments
 (0)