opencv 图像填充

定义pad_image_stride函数,将图像的长和宽填充到可以被指定整数整除。这个整数倍通常定义为stride。

一、opencv关键函数

opencv关键函数:cv2.copyMakeBorder

#im为待填充的图像
#top为图像上方填充像素个数
#bottom为图像下方填充像素个数
#left为图像左侧填充像素个数
#right为图像右侧填充像素个数
#value为填充的颜色
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)

二、函数输入输出

#im为输入图像
#stride=32,表示长宽需要被32整除
#ratios[0],取值为0~1,表示待填充像素在图片上方填充比例
#如果图片高度上需要填充dh个像素,那么图片上方填充dh*ratios[0],下方填充dh*(1-ratios[0])个像素。
#ratios[1],取值为0~1,表示待填充像素在图片左侧填充比例
#如果图片宽度上需要填充dw个像素,那么图片左侧填充dw*ratios[1],右侧填充dw*(1-ratios[1])个像素。
#color为填充的颜色
pad_image_stride(im, stride=32, ratios=[0, 0], color=(0, 0, 0))

三、pad_image_stride代码

def pad_image_stride(im, stride=32, ratios=[0, 0], color=(0, 0, 0)):
    shape = im.shape[:2]
    new_shape = [0, 0]
    new_shape[0] = shape[0] - shape[0] % stride + stride
    new_shape[1] = shape[1] - shape[1] % stride + stride
    dh     = max(0, new_shape[0] - shape[0])
    dw     = max(0, new_shape[1] - shape[1])
    top    = int(dh * ratios[0])
    left   = int(dw * ratios[1])
    bottom = dh - top
    right  = dw - left
    im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  
    return im

def pad_image_stride(im, stride=32, ratios=[0, 0], color=(0, 0, 0)):
    shape = im.shape[:2]
    new_shape = [0, 0]
    new_shape[0] = shape[0] - shape[0] % stride + stride
    new_shape[1] = shape[1] - shape[1] % stride + stride
    dh     = max(0, new_shape[0] - shape[0])
    dw     = max(0, new_shape[1] - shape[1])
    top    = int(dh * ratios[0])
    left   = int(dw * ratios[1])
    bottom = dh - top
    right  = dw - left
    im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  
    return im

更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding的叶子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值