编程语言
首页 > 编程语言> > python-opencv实现图像仿射变换

python-opencv实现图像仿射变换

作者:互联网

python-opencv实现图像仿射变换的代码:
所需实现的功能:
对一幅二值图像中值为255的轮廓内的部分进行放大缩小操作,需保证中心点位置不变。


groundtruth = cv2.imread(groundtruth_path)[:, :, 0]
h1, w1 = groundtruth.shape
# 缩放系数
for r in range(0.8, 1.2, 0.1):
    contours, cnt = cv2.findContours(groundtruth.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    if len(contours) != 1:#仅包含单个轮廓
       continue
    M = cv2.moments(contours[0])  # 计算第一条轮廓的各阶矩,字典形式
    center_x = int(M["m10"] / M["m00"])
    center_y = int(M["m01"] / M["m00"])#轮廓的中心位置
    angle = 0.0#旋转角度
    Mcenter = cv2.getRotationMatrix2D((center_x, center_y), angle, r)#注意设置中心位置与变换系数
    mask = cv2.warpAffine(groundtruth, Mcenter, (w1, h1))#mask即为变换后的图像

     

                          原图                                                          变换后

注意,中心位置坐标一样。

标签:center,python,图像,cv2,opencv,contours,groundtruth,轮廓,仿射变换
来源: https://blog.csdn.net/u013925378/article/details/90671676