python+opencv图片旋转函数-保持图像不被裁剪,且去除黑边
·
# 图片旋转函数-保持图像不被裁剪---顺时针
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)#顺时针
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)