python-opencv有效随机角度旋转

imgb=cv2.imread(r"F:\Data_library\steel\Treadimage2\big.png")
imgs=cv2.imread(r"F:\Data_library\steel\Treadimage2\samll.png")
tmp_rot=np.zeros_like(imgs)
#固定角度旋转
tmp_rot=cv2.rotate(imgs,cv2.ROTATE_90_CLOCKWISE,tmp_rot)
#翻转0,1,-1
cv2.flip(imgs,-1,tmp_rot)
#自由旋转角度设置
angle=60
new_h=int(abs(imgs.shape[0]*np.cos(angle/180.*np.pi))+abs(imgs.shape[1]*np.sin(angle/180.*np.pi)))
new_w=int(abs(imgs.shape[0]*np.sin(angle/180.*np.pi))+abs(imgs.shape[1]*np.cos(angle/180.*np.pi)))
M1 = cv2.getRotationMatrix2D((int((imgs.shape[1])/2.),int((imgs.shape[0])/2.)),angle,1)
//#np.random.randint(0,360) (int(imgs.shape[1]/2.),int(imgs.shape[0]/2.)
M2 =np.float32([[0,0,(new_w-imgs.shape[1])/2.],[0,0,(new_h-imgs.shape[0])/2.]])
//# M2 =np.float32([[1,0,new_w-imgs.shape[1]],[0,1,new_h-imgs.shape[0]]])
//# M=np.matmul(M1,M2.T)
M=M1+M2
dst = cv2.warpAffine(imgs, M, (new_w,new_h))
cv2.namedWindow(“randb”)
cv2.imshow(“randb”,dst)
cv2.waitKey()

你可能感兴趣的:(opencv基础,OPENCV,opencv)