opencv学习笔记(1)

1.计算仿射矩阵

1)方程法

  cv2.getAffineTransform(src, dst), src和dst分别代表原坐标和变换后的坐标,且均为3行2列的二维ndarray。

1 import cv2
2 import numpy as np
3 src = np.array([[0,0],[200,0],[0,200]],np.float32)
4 dst = np.array([[0,0],[100, 0], [0,100]], np.float32)
5 A = cv2.getAffineTransform(src, dst)
6 print(A)
7 #array([[0.5, 0. , 0. ],
8 #       [0. , 0.5, 0. ]])

2) 矩阵法

  cv2.getRotationMatrix2D(center, angle, scale),center为变换中心点的坐标,scale是等比例缩放的系数,angle是逆时针旋转的角度(angle以角度为单位,非弧度)。

1 import numpy as np
2 import cv2
3 A = cv2.getRotationMatrix2D((40, 50), 30, 0.5)
4 print(A.dtype)
5 #dtype('float64')
6 print(A)
7 #[[ 0.4330127   0.25       10.17949192]
8 # [-0.25        0.4330127  38.34936491]]

 2.插值算法

  cv2.warpAffine(src, M, dsize[, dst[, flages[, borderMode[, borderValue ]]])

参数 解释
src 输入图像矩阵
M 2行3列的仿射变换矩阵
dsize 二元元组(宽,高),输出图像的大小
flags 插值法:INTE_NEAREST、INTE_LINEAR(默认)等
borderMode 填充模式:BORDER_CONSTANT
borderValue

当borderModer=BRDER_CONSTANT时的填充值

 

 

 

 1 # coding=utf-8
 2 # 通过改变仿射矩阵完成队图像的缩小、平移、旋转等操作
 3 import numpy as np
 4 import cv2
 5 
 6 
 7 def warp_affine(imgPath):
 8     img = cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE)
 9     h, w = img.shape[:2]
10     A1 = np.array([[0.5, 0, 0], [0, 0.5, 0]], dtype=np.float32)
11     # 缩小两倍
12     d1 = cv2.warpAffine(img, A1, (w, h), borderValue=125)
13     A2 = np.array([[0.5, 0, 2 / 4], [0, 0.5, h / 4]], dtype=np.float32)
14     # 先缩小两倍,再平移
15     d2 = cv2.warpAffine(img, A2, (w, h), borderValue=125)
16     # 在d2基础上,绕图像的中心点旋转
17     A3 = cv2.getRotationMatrix2D((w / 2.0, h / 2.0), 30, 1)
18     d3 = cv2.warpAffine(d2, A3, (w, h), borderValue=125)
19     cv2.imshow("img", img)
20     cv2.imshow("d1", d1)
21     cv2.imshow("d2", d2)
22     cv2.imshow("d3", d3)
23     cv2.waitKey(0)
24     cv2.destroyAllWindows()
25 
26 
27 if __name__ == '__main__':
28    warp_affine("../img/stick_5.jpg")

 3.线性极坐标函数 linearPolar

dst = linearPolar(src, center, maxRadius, flags)
该函数有两个缺点:第一,极坐标变换的步长是不可控制的,导致得到的图可能不是很理想;第二,该函数只能对整个圆内区域,而无法对指定的圆环区域进行极坐标变换。
 1 # coding=utf-8
 2 # 线性极坐标函数 linearPolar
 3 # dst = linearPolar(src, center, maxRadius, flags)
 4 # dst,输出图像矩阵,其尺寸和src是相同的
 5 # src,输入图像矩阵
 6 # center,极坐标变化中心
 7 # maxRadius,极坐标变换的最大距离
 8 # flags,插值算法,同函数resize、warAffine的插值算法
 9 import cv2
10 
11 
12 def linear_polar(imgPath):
13     src = cv2.imread(imgPath, cv2.IMREAD_ANYCOLOR)
14     cv2.imshow("src", src)
15     print src.shape
16     w, h, _ = src.shape
17     dst = cv2.linearPolar(src, (w / 2, h / 2), 500, cv2.INTER_LINEAR)
18     cv2.imshow("dst", dst)
19     cv2.waitKey(0)
20     cv2.destroyAllWindows()
21 
22 
23 if __name__ == '__main__':
24     linear_polar("../img/cartToPolar.jpg")

 4.对数极坐标函数

 1 # coding=utf-8
 2 # 对数极坐标函数logPolar
 3 # dst = logPolar(src, center, M, flags)
 4 # dst,输出图像矩阵,尺寸与src相同
 5 # src,输入图像矩阵(单或多通道矩阵0
 6 # M,系数,该值大一点效果会好一点
 7 # flags, WARP_FILL_OUTLIERS:笛卡尔坐标系对数极坐标变换
 8 # WARP_INVERSE_MAP:对数极坐标向笛卡尔坐标变换
 9 import cv2
10 
11 
12 def log_polar(imgPath):
13     src = cv2.imread(imgPath, cv2.IMREAD_ANYCOLOR)
14     w, h, _ = src.shape
15     cv2.imshow("src", src)
16     M = 100
17     dst = cv2.logPolar(src, (w / 2, h / 2), M, cv2.WARP_FILL_OUTLIERS)
18     cv2.imshow("dst", dst)
19     cv2.waitKey(0)
20     cv2.destroyAllWindows()
21 
22 
23 if __name__ == '__main__':
24     log_polar("../img/cartToPolar.jpg")

 

posted @ 2019-07-30 14:28  Coodyzのblog  阅读(714)  评论(0编辑  收藏  举报