为什么tf.image.sample_distorted_bounding_box无法产生正确的边界框?

时间:2018-08-31 16:01:18

标签: tensorflow

我认为将Arg min_object_covered分配为1时,返回的bbox应该完全覆盖发送给函数的所有bounding_box。

但是,当我根据bboxes [0,0]裁剪图像并绘制图像及其边界框时,只有一部分边界框位于裁剪后的图像内部。

因此,我想问问min_object_covered参数的确切定义是什么?

如果我希望裁剪后的图像覆盖所有边框,我应该如何使用该功能?

这是我的代码

from __future__ import division
import tensorflow as tf
import numpy as np

import cv2

def bboxes_resize(bbox_ref, bboxes, name=None):
    with tf.name_scope(name, 'bboxes_resize'):
        # Translate.
        v = tf.stack([bbox_ref[0], bbox_ref[1], bbox_ref[0], bbox_ref[1]])
        bboxes = bboxes - v
        # Scale.
        s = tf.stack([bbox_ref[2] - bbox_ref[0],
                    bbox_ref[3] - bbox_ref[1],
                    bbox_ref[2] - bbox_ref[0],
                    bbox_ref[3] - bbox_ref[1]])
        bboxes = bboxes / s
        return bboxes


img = cv2.imread('lena.jpg')
img = cv2.resize(img,(500,500))
img = img.astype(np.float32)
tf_img = tf.convert_to_tensor(img)
bbox1 = tf.random_uniform([1,4,2],minval = 0.,maxval = 0.5)
bbox2 = tf.random_uniform([1,4,2],minval = 0.5,maxval = 1.)
bbox = tf.concat([bbox1,bbox2],axis = -1)
image_with_bbox = tf.image.draw_bounding_boxes(
            tf.expand_dims(tf_img,0),
            bbox,
            name='lena')
bbox_begin,bbox_size, distort_bbox = tf.image.sample_distorted_bounding_box(
            tf.shape(tf_img),
            bounding_boxes = bbox, 
            min_object_covered = 1.,
            aspect_ratio_range=(0.5,1.5),
            area_range=(0.1,1),
            max_attempts=200)
dis = distort_bbox
distort_bbox = distort_bbox[0, 0]
cropped_image = tf.slice(tf_img, bbox_begin, bbox_size)
        # Restore the shape since the dynamic slice loses 3rd dimension.
cropped_image.set_shape([None, None, 3])
bboxes = bboxes_resize(distort_bbox, bbox)
image_with_bbox_distorted = tf.image.draw_bounding_boxes(
            tf.expand_dims(cropped_image,0),
            bboxes,
            name='lena_d')

with tf.Session() as sess:
    while (True):
        sess.run(tf.global_variables_initializer())
        img1,img2,distort_bbox_1, dis_1,bbox_begin_1,bbox_size_1 = sess.run([image_with_bbox,image_with_bbox_distorted,
                                                                    distort_bbox,dis,
                                                                    bbox_begin,bbox_size])
        cv2.imshow('img1',img1[0]/256)
        cv2.imshow('img2',img2[0]/256)
        print(distort_bbox_1)
        print(dis_1)
        print(bbox_begin_1)
        print(bbox_size_1)

        cv2.imwrite('bbox_image.jpg',img1[0].astype(np.int64))
        cv2.imwrite('bbox_image_cropped.jpg',img2[0].astype(np.int64))

        cv2.waitKey()

0 个答案:

没有答案