Skip to content

Commit 96b079f

Browse files
committed
update ch14
1 parent 653b5b0 commit 96b079f

File tree

10 files changed

+169
-47
lines changed

10 files changed

+169
-47
lines changed

ch04-图片/4.1_imread_imshow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717
rows,cols,ch=img.shape
1818
print('行/高:',rows,'列/宽:',cols,'通道:',ch)
19-
19+
#图像的宽对应的是列数, 高对应的是行数。
2020

2121
cv2.namedWindow('image', cv2.WINDOW_NORMAL)#可以调整窗口大小
2222
# cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)#自动调整

ch05-视频/5.VideoCapture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
cv2.imshow('frame', gray)
4444
# if cv2.waitKey(1) & 0xFF == ord('q'):#不行
4545
# break
46-
key = cv2.waitKey(1)
46+
key = cv2.waitKey(delay=1)
4747
if key == ord("q"):
4848
break
4949

ch14-几何变换/14.2平移-2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/12 下午12:31
3+
# @Author : play4fun
4+
# @File : 14.2平移-2.py
5+
# @Software: PyCharm
6+
7+
"""
8+
14.2平移-2.py:
9+
http://docs.opencv.org/3.2.0/da/d6e/tutorial_py_geometric_transformations.html
10+
函数 cv2.warpAffine() 的第三个参数的是 出图像的大小 ,它的格式 应 是图像的(宽,高) 。
11+
图像的宽对应的是列数, 高对应的是行数。
12+
"""
13+
14+
import cv2
15+
import numpy as np
16+
17+
img = cv2.imread('../data/messi5.jpg', 0)
18+
rows, cols = img.shape
19+
20+
M = np.float32([[1, 0, 100], [0, 1, 50]])
21+
dst = cv2.warpAffine(img, M, (cols, rows))
22+
23+
cv2.imshow('img', dst)
24+
cv2.waitKey(0)
25+
cv2.destroyAllWindows()

ch14-几何变换/14.2平移.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/12 下午12:21
3+
# @Author : play4fun
4+
# @File : 平移.py
5+
# @Software: PyCharm
6+
7+
"""
8+
平移.py:平移就是将对 换一个位置。如果你 沿 (x, y) 方向移动
9+
移动的距离 是 (tx,ty)
10+
"""
11+
12+
import cv2
13+
import numpy as np
14+
15+
cap = cv2.VideoCapture(0)
16+
while True:
17+
# 获取每一帧
18+
ret, frame = cap.read()
19+
20+
# 换到 HSV
21+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
22+
# 定蓝色的阈值
23+
lower_blue = np.array([110, 50, 50])
24+
upper_blue = np.array([130, 255, 255])
25+
# 根据阈值构建掩模
26+
mask = cv2.inRange(hsv, lower_blue, upper_blue)
27+
# 对原图像和掩模 进行位运算
28+
res = cv2.bitwise_and(frame, frame, mask=mask)
29+
30+
# 显示图像
31+
cv2.imshow('frame', frame)
32+
cv2.imshow('mask', mask)
33+
cv2.imshow('res', res)
34+
35+
k = cv2.waitKey(0) # & 0xFF
36+
if k == ord('q'):
37+
break
38+
# 关 窗口
39+
cv2.destroyAllWindows()
Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
# -*- coding: utf-8 -*-
2+
'''
3+
仿射变换
4+
在仿射变换中 原图中所有的平行线在结果图像中同样平行。
5+
为了创建 这个矩阵,我们需要从原图像中找到三个点以及他们在 出图像中的位置。
6+
然后 cv2.getAffineTransform 会创建一个 2x3 的矩 最后 个矩 会 传给 函数 cv2.warpAffine。
7+
'''
28

39
import cv2
410
import numpy as np
511
from matplotlib import pyplot as plt
6-
img=cv2.imread('drawing.png')
7-
rows,cols,ch=img.shape
8-
pts1=np.float32([[50,50],[200,50],[50,200]])
9-
pts2=np.float32([[10,100],[200,50],[100,250]])
10-
M=cv2.getAffineTransform(pts1,pts2)
11-
dst=cv2.warpAffine(img,M,(cols,rows))
12-
plt.subplot(121,plt.imshow(img),plt.title('Input'))
13-
plt.subplot(121,plt.imshow(img),plt.title('Output'))
14-
plt.show()
12+
13+
img = cv2.imread('drawing.png')
14+
rows, cols, ch = img.shape
15+
print(img.shape)
16+
17+
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
18+
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
19+
20+
M = cv2.getAffineTransform(pts1, pts2)
21+
dst = cv2.warpAffine(img, M, (cols, rows))
22+
23+
# plt.subplot(121, plt.imshow(img), plt.title('Input'))
24+
# plt.subplot(122, plt.imshow(dst), plt.title('Output'))
25+
26+
plt.figure(figsize=(8, 7), dpi=98)
27+
p1 = plt.subplot(211)
28+
p1.imshow(img)
29+
p1.set_title('Input')
30+
31+
p2 = plt.subplot(212)
32+
p2.imshow(dst)
33+
p2.set_title('Output')
34+
35+
plt.show()
Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
# -*- coding: utf-8 -*-
2+
'''
3+
透视变换
4+
对于透视变换 ,我们需要一个 3x3 变换矩 。
5+
在变换前后直线 是直线。
6+
构建 个变换矩 你需要在输入图像上找 4 个点, 以及他们在输出图 像上对应的位置。
7+
四个点中的任意三个都不能共线。这个变换矩阵可以用函数 cv2.getPerspectiveTransform() 构建。
8+
然后把这个矩阵传给函数 cv2.warpPerspective。
9+
10+
'''
211

312
import cv2
413
import numpy as np
514
from matplotlib import pyplot as plt
6-
img=cv2.imread('sudokusmall.png')
7-
rows,cols,ch=img.shape
815

9-
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
10-
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
11-
M=cv2.getPerspectiveTransform(pts1,pts2)
12-
dst=cv2.warpPerspective(img,M,(300,300))
16+
img = cv2.imread('../data/sudoku.jpg')
17+
rows, cols, ch = img.shape
18+
19+
pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
20+
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
21+
22+
M = cv2.getPerspectiveTransform(pts1, pts2)
23+
dst = cv2.warpPerspective(img, M, (300, 300))
24+
25+
plt.figure(figsize=(8, 7), dpi=98)
26+
p1 = plt.subplot(211)
27+
p1.imshow(img)
28+
p1.set_title('Input')
29+
30+
p2 = plt.subplot(212)
31+
p2.imshow(dst)
32+
p2.set_title('Output')
1333

14-
plt.subplot(121,plt.imshow(img),plt.title('Input'))
15-
plt.subplot(121,plt.imshow(img),plt.title('Output'))
16-
plt.show()
34+
plt.show()
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
# -*- coding: utf-8 -*-
22

3+
# 旋转
34
import cv2
45
import numpy as np
56

6-
img=cv2.imread('../data/messi5.jpg',0)
7-
rows,cols=img.shape
8-
#的第一个参数为旋 中心 第二个为旋 度 第三个为旋 后的缩放因子
9-
# 可以 置旋 中心 缩放因子 以及窗口大小来 止旋 后 出 界的
10-
M=cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)
11-
# 第三个参数是 出图像的尺寸中心
12-
dst=cv2.warpAffine(img,M,(2*cols,2*rows))
13-
while(1):
14-
cv2.imshow('img',dst)
15-
if cv2.waitKey(1)&0xFF==27:
16-
break
17-
cv2.destroyAllWindows()
7+
img = cv2.imread('../data/messi5.jpg', 0)
8+
rows, cols = img.shape
9+
10+
# 的第一个参数为旋转中心 第二个为旋转角度
11+
# 第三个为旋转后的缩放因子
12+
# 可以通过设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题
13+
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 0.6)
14+
15+
# 第三个参数是输出图像的尺寸中心
16+
dst = cv2.warpAffine(img, M, (2 * cols, 2 * rows))
17+
18+
cv2.imshow('img', dst)
19+
20+
cv2.waitKey(0) # & 0xFF
21+
cv2.destroyAllWindows()

ch14-几何变换/14.resize.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
# -*- coding: utf-8 -*-
22

3+
'''
4+
扩展缩放
5+
6+
在缩放时我们推荐使用 cv2.INTER_AREA
7+
在扩展时我们推荐使用 v2.INTER_CUBIC 慢) 和 v2.INTER_LINEAR。
8+
默认情况下所有改变图像尺寸大小的操作使用的插值方法 是 cv2.INTER_LINEAR。
9+
10+
Resize(src, dst, interpolation=CV_INTER_LINEAR)
11+
'''
312

413
import cv2
514
import numpy as np
6-
img=cv2.imread('../data/messi5.jpg')
7-
# 下mian的 None 本应 是 出图像的尺寸 但是因为后 我们 置了缩放因子
8-
# 因此 为 None
9-
res=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
10-
#OR
11-
# 呢 我们直接 置 出图像的尺寸 所以不用 置缩放因子
12-
height,width=img.shape[:2]
13-
res=cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)
14-
15-
while(1):
16-
cv2.imshow('resize',res)
17-
cv2.imshow('src img',img)
18-
if cv2.waitKey(1) & 0xFF == 27:
15+
16+
img = cv2.imread('../data/messi5.jpg')
17+
# 下面的 None 本应 是 出图像的尺寸 但是因为后边我们设置了缩放因子
18+
# 因此这里为 None
19+
res = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
20+
21+
# OR
22+
# 我们直接设置输出图像的尺寸 所以不用设置缩放因子
23+
height, width = img.shape[:2]
24+
res = cv2.resize(img, (2 * width, 2 * height), interpolation=cv2.INTER_CUBIC)
25+
26+
while True:
27+
cv2.imshow('resize', res)
28+
cv2.imshow('src img', img)
29+
30+
key = cv2.waitKey(1)
31+
if key == ord("q"):
1932
break
20-
cv2.destroyAllWindows()
33+
cv2.destroyAllWindows()

ch14-几何变换/14.wrapAffine.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# -*- coding: utf-8 -*-
22
import cv2
33
import numpy as np
4-
#移动了100,50 个像素。
4+
5+
# 移动了100,50 个像素。
56
img = cv2.imread('../data/messi5.jpg', 0)
67
rows, cols = img.shape
8+
79
M = np.float32([[1, 0, 100], [0, 1, 50]])
810
dst = cv2.warpAffine(img, M, (cols, rows))
911

ch14-几何变换/drawing.png

40.8 KB
Loading

0 commit comments

Comments
 (0)