可以使用创建蒙版 tf.pad 。
tf.pad
crop = tf.ones([3, 3]) # "before_axis_x" how many padding will be added before cropping zone over the axis x # "after_axis_x" how many padding will be added after cropping zone over the axis x mask = tf.pad(crop, [[before_axis_0, after_axis_0], [before_axis_1, after_axis_1]] tf.mask(image, mask) # creates the extracted image
为了与tf.image.crop_and_resize具有相同的行为,这里有一个函数,它将获取一个框数组,并将返回带有填充的提取图像数组。
def extract_with_padding(image, boxes): """ boxes: tensor of shape [num_boxes, 4]. boxes are the coordinates of the extracted part box is an array [y1, x1, y2, x2] where [y1, x1] (respectively [y2, x2]) are the coordinates of the top left (respectively bottom right ) part of the image image: tensor containing the initial image """ extracted = [] shape = tf.shape(image) for b in boxes: crop = tf.ones([3, 3]) mask = tf.pad(crop, [[b[0], shape[0] - b[2]], [b[1] , shape[1] - b[3]]]) extracted.append(tf.boolean_mask(image, mask)) return extracted
谢谢你非常好的功能@edkevekeh。我不得不稍微修改它以使它做我想要的。一,我无法迭代作为Tensor对象的盒子。此外,裁剪尺寸由方框决定,并不总是3x3。此外,tf.boolean_mask返回裁剪,但我想保留裁剪,但在裁剪外部替换为0.所以我用乘法替换了tf.boolean_mask。
对于我的用例,num_boxes可能很大,所以我想知道是否有比for循环更有效的方法,猜测不是。我修改过的@ edkevekeh解决方案,如果有人需要的话。
def extract_with_padding(image, boxes): """ boxes: tensor of shape [num_boxes, 4]. boxes are the coordinates of the extracted part box is an array [y1, x1, y2, x2] where [y1, x1] (respectively [y2, x2]) are the coordinates of the top left (respectively bottom right ) part of the image image: tensor containing the initial image """ extracted = [] shape = tf.shape(image) for i in range(boxes.shape[0]): b = boxes[i] crop = tf.ones([b[2] - b[0], b[3] - b[1]]) mask = tf.pad(crop, [[b[0], shape[0] - b[2]], [b[1] , shape[1] - b[3]]]) extracted.append(image*mask) return extracted