def rotationImg(img_file1,ra): #ra旋转角度
# 获取图片尺寸并计算图片中心点
#img = cv2.imread(img_file1, cv2.IMREAD_GRAYSCALE)
img = np.array(img_file1,dtype=float)
(h, w) = img.shape[:2]
center = (w/2, h/2)
ra=np.random.randint(0,90)
M = cv2.getRotationMatrix2D(center, ra, 1.0)#第三个参数缩放比例
rotated = cv2.warpAffine(img, M, (w, h),borderMode=cv2.BORDER_REFLECT)#边界填充模式
'''
填充模式参数:
BORDER_CONSTANT #恒像素值填充
Python: cv.BORDER_CONSTANT
iiiiii|abcdefgh|iiiiiii with some specified i
BORDER_REPLICATE #边界像素值填充
Python: cv.BORDER_REPLICATE
aaaaaa|abcdefgh|hhhhhhh
BORDER_REFLECT #翻转像素值填充
Python: cv.BORDER_REFLECT
fedcba|abcdefgh|hgfedcb
BORDER_WRAP #对称像素值填充
Python: cv.BORDER_WRAP
cdefgh|abcdefgh|abcdefg
BORDER_REFLECT_101 #翻转像素值(去掉边界值)填充
Python: cv.BORDER_REFLECT_101
gfedcb|abcdefgh|gfedcba
BORDER_TRANSPARENT #透明填充
Python: cv.BORDER_TRANSPAREN
'''
#cv2.imshow("rotated", rotated)
#cv2.waitKey(0)
#cv2.imwrite(out_file, rotated)
return rotated
OpenCV4 Python 图像旋转 之warpAffine仿射变换参数解释
最新推荐文章于 2024-05-13 18:50:30 发布