使⽤opencv对图像进⾏crop,resize,pad
使⽤opencv-python 对图像进⾏resize和填充
在图像输⼊神经⽹络之前,需要进⾏⼀定的处理,假设神经⽹络的图像输⼊是256 256然后进⾏了224 224的random crop。我们需要进⾏如下处理:
resized读⼊原始图像
image = cv2.imread("img.jpg")
截取图像中有价值的部分
region = image[y1:y2, x1:x2]
确定图⽚的长边和短边,然后把长边resize到224,保持纵横⽐的情况下resize短边
w, h = x2 - x1, y2 - y1 # h, w = image.shape
m = max(w, h)
ratio = 224.0 / m
new_w, new_h = int(ratio * w), int(ratio *h)
assert new_w > 0and new_h > 0
resized = size(region, (new_w, new_h))
把图⽚进⾏填充,填充到256 256
W, H = 256, 256
top = (H - new_h) // 2
bottom = (H - new_h) // 2
if top + bottom + h < H:
bottom += 1
left = (W - new_w) // 2
right = (W - new_w) // 2
if left + right + w < W:
right += 1
pad_image = pyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value = self.white)
图⽚在输⼊⽹络之后,训练的时候进⾏random crop,就会发⽣有⼀部分被截取掉的情况,⽽这正是我们想要的图像增强在test阶段,是进⾏centre crop,⽽正好把整个图像都截取出来,⽽这正是我们想要的
值得注意的是,image.shape,size和pyMakeBorder⼏个函数
image.shape的输出是(H, W, C)
opencv中以左上⾓为原点,W⽅向为x,H⽅向为y

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。