基于重映射实现图像的旋转

基于重映射实现图像的旋转

def img_rotation(self,img_src,direction=90):
        # 1.随机创建图片
        height, width = img_src.shape[:2]
        # print("img width:%d height:%d" % (width, height))
        # 2.创建X,Y map
        map_x = np.zeros([width, height], np.float32)
        map_y = np.zeros([width, height], np.float32)
        # 3.执行重映射 调整 X Y map位置
        for i in range(width):
            for j in range(height):
                if direction==-90:
                    map_x.itemset((i, j), i)  # 1
                    map_y.itemset((i, j), j)# 2
                elif direction==90:
                    map_x.itemset((i, j), i)  # 1
                    map_y.itemset((i, height - 1 - j), j)  # 3
                else:
                    pass
                '''map_x.itemset((i, j), i) # 1
                # map_y.itemset((i, j), j)# 2
                map_y.itemset((i, height-1-j), j)# 3'''
                # 1和2是逆时针90,1和3是顺时针90
        # 4.执行重映射处理
        img_dst = cv2.remap(img_src, map_x, map_y, cv2.INTER_LINEAR)
        # cv2.imwrite('rp1.jpg',img_dst)
        return img_dst

猜你喜欢

转载自blog.csdn.net/a272881819/article/details/124923924