Python 如何将TensorFlow 2中的裁剪到边界框应用于符号张量?

Python 如何将TensorFlow 2中的裁剪到边界框应用于符号张量?,python,tensorflow2.0,tensor,Python,Tensorflow2.0,Tensor,我有以下代码,用于查找像素强度>=25的第一个和最后一个像素,然后裁剪到该边界框: white_pixels = tf.where(input_img >= 25) first_white_pixel = white_pixels[:, 0] first_white_pixel = tf.cast(first_white_pixel, dtype=tf.int32) last_white_pixel = white_pixels[:, -1] last_white_pixel = tf.c

我有以下代码,用于查找像素强度>=25的第一个和最后一个像素,然后裁剪到该边界框:

white_pixels = tf.where(input_img >= 25)
first_white_pixel = white_pixels[:, 0]
first_white_pixel = tf.cast(first_white_pixel, dtype=tf.int32)
last_white_pixel = white_pixels[:, -1]
last_white_pixel = tf.cast(last_white_pixel, dtype=tf.int32)
cropped = tf.image.crop_to_bounding_box(input_img, first_white_pixel[0], 0, last_white_pixel[0] - first_white_pixel[0], 299)

但是,我一直收到一个错误,即tf.image.crop\u to\u bounding\u框中的目标高度必须大于0。在我所有的图像中,最后一个白色像素[0]-第一个白色像素[0]的结果肯定在0以上。该代码在TensorFlow 2.3中作为符号张量执行,在非符号设置下运行良好(因为缺少更好的术语)。

我的错误似乎是切片错误。而不是写作

first_white_pixel = white_pixels[:, 0]
last_white_pixel = white_pixels[:, -1]
我应该写信的

first_white_pixel = white_pixels[0, :]
last_white_pixel = white_pixels[-1, :]