1
1
# -*- coding: utf-8 -*-
2
+ '''
3
+ 透视变换
4
+ 对于透视变换 ,我们需要一个 3x3 变换矩 。
5
+ 在变换前后直线 是直线。
6
+ 构建 个变换矩 你需要在输入图像上找 4 个点, 以及他们在输出图 像上对应的位置。
7
+ 四个点中的任意三个都不能共线。这个变换矩阵可以用函数 cv2.getPerspectiveTransform() 构建。
8
+ 然后把这个矩阵传给函数 cv2.warpPerspective。
9
+
10
+ '''
2
11
3
12
import cv2
4
13
import numpy as np
5
14
from matplotlib import pyplot as plt
6
- img = cv2 .imread ('sudokusmall.png' )
7
- rows ,cols ,ch = img .shape
8
15
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' )
13
33
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 ()
0 commit comments