给定图像:
(a)从图像中获取感兴趣的区域((440240),(5353380)):
roi_slice = tf.slice(
image_in,
[top_left_x, top_left_y, top_left_z],
[roi_len_x, roi_len_y, bottom_right_z]
)
获取与图像大小相同的ROI的布尔掩码
roi_mask = tf.ones_like(roi_slice)
mask_canvas = tf.image.pad_to_bounding_box(
[roi_mask],
top_left_x,
top_left_y,
np_image.shape[0],
np_image.shape[1]
)
bool_mask = tf.cast(mask_canvas, tf.bool)
(b)在本例中,我使用的是假渐变,但您可以用真实渐变替换。
fake_gradients = tf.ones_like(image_in) * 0.2
(c)屏蔽梯度,以获得ROI所在的梯度,否则为0。
masked_gradients = tf.where(bool_mask[0], fake_gradients, mask_canvas[0])
(d)制作图像的可编辑副本,并使用遮罩的渐变进行更新
# Make an editable copy of the image
editable_image = tf.get_variable(
name='editable_image', shape=image_in.shape, dtype=tf.float32)
init_op = tf.assign(editable_image, image_in)
# Make sure we don't update the image before we've set its initial value.
with tf.control_dependencies([init_op]):
update_roi_op = tf.assign_add(editable_image, masked_gradients)
你可以找到一个完全有效的Colab示例
on GitHub
.