opencv图像经过裁剪旋转镜像后的坐标映射

我们标注完数据之后,因为数据量太小经常会做一些数据增广,但是经过旋转等操作之后,原先标注的坐标肯定会变,这时候计算出经过处理的图像的坐标是很有用的,省的我们拿处理后的图像再去标注.下面将介绍图像旋转,裁剪,镜像之后,对应的图像坐标算法.

# 1.坐标处理函数封装
# 图像镜像后坐标(height,width=原图高度,原图宽度,x,y=原图上某点的坐标(x,y),
# 返回经过纵轴镜像后的坐标(x,y))
def mirror_position(height,width,x,y):
    x_center,y_center = width/2,height/2
    y0 = y
    x0 = 2 * x_center - x
    return round(x0),round(y0)

# 图像裁剪后坐标(height,width=原图高度,原图宽度,x,y=原图上某点的坐标(x,y),
# pro=裁剪比例,返回经过比例裁剪后的坐标(x,y))
def crop_position(height,width,x,y,pro):
    crop_size_x = width/pro
    crop_size_y = height/pro
    x0 = x-crop_size_x
    y0 = y-crop_size_y
    return round(x0),round(y0)

# 图像裁剪后坐标(height,width=原图高度,原图宽度,ori_x,ori_y=原图上某点的坐标(x,y),
# crop_x,crop_y=x轴裁剪大小,y轴裁剪大小,返回经过比例裁剪后的坐标(x,y))
def crop_position_map(ori_x,ori_y,crop_x,crop_y):
    new_x = ori_x - crop_x
    new_y = ori_y - crop_y
    return new_x,new_y

# 图像镜像后坐标(h,w=原图高度,原图宽度,angle旋转角度(注意顺逆时针旋转),
# x1,y1=原图上某点的坐标(x,y),x2,y2=原图中点坐标(x,y),返回经过纵轴镜像后的坐标(x,y))
def rotate_position(w,h,angle,x1,y1,x2,y2):
    y1 = h - y1
    y2 = h - y2
    x = (x1 - x2) * math.cos(math.pi / 180.0 * angle) - (y1 - y2) * math.sin(math.pi / 180.0 * angle) + x2
    y = (x1 - x2) * math.sin(math.pi / 180.0 * angle) + (y1 - y2) * math.cos(math.pi / 180.0 * angle) + y2
    y = h - y
    return round(x),round(y)


# 2. 图像处理
image_path = 'xxx.jpg'
image_np = cv2.imread(image_path)
img_h, img_w = image_np.shape[:2]
# 图像中点坐标
x0,y0 = img_w / 2, img_h / 2
# 旋转一定角度
# angle  = 20
# M = cv2.getRotationMatrix2D((x0,y0), angle, 1.0)
# image_np = cv2.warpAffine(image_np, M, (img_w, img_h))

# 水平镜像
image_np = cv2.flip(image_np, 1, dst=None)

# 裁剪
# proportion = 15   # 裁剪比例
#image_np= image_np[round(img_h/proportion):img_h,round(img_w/proportion):round(img_w-img_w/proportion)]

# 例子:设点(x,y),返回的x1,y1即为新的坐标
x1,y1 = mirror_position(img_h,img_w,x,y)

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值