opencv常用函数入参

时间:2022-01-09 20:36:50

img = cv2.imread('messi5.jpg',0)

three flags, you can simply pass integers 1, 0 or -1 respectively

img NumPy.ndarray对象

rows,cols = img.shape只有flag=0(rows,cols = img.shape[:2])

ndarray.ndim:数组的维数

ndarray.shape:数组的维度,当0,长宽,-1有alpha通道(jpg没有alpha通道需要自己添加)

ndarray.size:数组元素的总个数,等于shape属性中元组元素的乘积

ndarray.dtype:表示数组中元素类型的对象

ndarray.itemsize:数组中每个元素的字节大小


图像的平移

H = np.float32([[1,0,100],[0,1,50]])100 x,50 y


图像的扩大与缩小

# 插值:interpolation

# None本应该是放图像大小的位置的,后面设置了缩放比例,

#所有就不要了

res1 = cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)

#直接规定缩放大小,这个时候就不需要缩放因子

height,width = img.shape[:2]

res2 = cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)



cv2.warpAffine()

这个函数需要三个参数,原始图片,操作矩阵(2x3,最终与0,0,1组成3x3),变换后的图像大小(一般是一个二维元组)


图像的旋转

cv2.getRotationMatrix2D()

这个函数需要三个参数,旋转中心,旋转角度,旋转后图像的缩放比例

#第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例

M = cv2.getRotationMatrix2D((cols/2,rows/2),45,1)


图像的仿射

cv2.getAffineTransform(pos1,pos2),其中两个位置就是变换前后的对应位置关系。输出的就是仿射矩阵


pts1 = np.float32([[50,50],[200,50],[50,200]])

pts2 = np.float32([[10,100],[200,50],[100,250]])

M = cv2.getAffineTransform(pts1,pts2)


图像的透射

 cv2.getPerspectiveTransform(pts1,pts2),其中pts需要变换前后的4个点对应位置。得到M后在通过函数cv2.warpPerspective(img,M,(200,200))进行。形象化的图如下(引用参考的) 



#第三个参数:变换后的图像大小

res = cv2.warpAffine(img,M,(rows,cols))