# 图片旋转函数-保持图像不被裁剪---顺时针 def ImageRotate(img, angle): # img:输入图片;newIm:输出图片;angle:旋转角度(°) height, width = img.shape[:2] # 输入(H,W,C),取 H,W 的值 center = (width // 2, height // 2) # 绕图片中心进行旋转 M = cv2.getRotationMatrix2D(center, -angle, 1.0) cos = np.abs(M[0, 0]) sin = np.abs(M[0, 1]) nW = int((height * sin) + (width * cos)) nH = int((height * cos) + (width * sin)) M[0, 2] += (nW / 2) - center[0] M[1, 2] += (nH / 2) - center[1] #注意我的图为二值shape=[w,h]的,而且我的图背景为白色,物体为黑色,所以borderValue=255,请结合自己的图和物体进行边缘像素处理--去除黑边 image_rotation = cv2.warpAffine(img, M, (nW, nH),borderValue=255) return image_rotation
templeimg = ImageRotate(templeimg, 180)#顺时针