37 SimpleTensor<float> scale_image(
const SimpleTensor<float> &in,
const TensorShape &out_shape,
InterpolationPolicy policy,
float extrapolation_value)
43 const auto wr =
static_cast<float>(in.shape()[1]) /
static_cast<float>(out_shape[1]);
44 const auto hr =
static_cast<float>(in.shape()[2]) /
static_cast<float>(out_shape[2]);
46 const auto width =
static_cast<int>(in.shape().y());
47 const auto height =
static_cast<int>(in.shape().z());
50 win.use_tensor_dimensions(out_shape);
53 Coordinates in_id(out_id);
62 float x_src = std::floor(idw * wr);
63 float y_src = std::floor(idh * hr);
74 *
reinterpret_cast<float *
>(out(out_id)) = extrapolation_value;
80 float x_src = idw * wr;
81 float y_src = idh * hr;
82 in_id.set(1, std::floor(x_src));
83 in_id.set(2, std::floor(y_src));
86 const int id_w = in_id[1];
87 const int id_h = in_id[2];
89 const float dx = x_src - id_w;
90 const float dy = y_src - id_h;
91 const float dx_1 = 1.0f - dx;
92 const float dy_1 = 1.0f - dy;
97 in_id.set(1, id_w + 1);
101 in_id.set(2, id_h + 1);
103 in_id.set(1, id_w + 1);
104 in_id.set(2, id_h + 1);
107 *
reinterpret_cast<float *
>(out(out_id)) = tl * (dx_1 * dy_1) + tr * (dx * dy_1) + bl * (dx_1 * dy) + br * (dx * dy);
111 *
reinterpret_cast<float *
>(out(out_id)) = extrapolation_value;
123 template <
typename T>
124 SimpleTensor<float> crop_image(
const SimpleTensor<T> &
src, Coordinates start, Coordinates
end, int32_t batch_index,
float extrapolation_value)
126 TensorShape out_shape(
src.shape()[0],
static_cast<uint32_t
>(abs(
end[0] - start[0])) + 1,
static_cast<uint32_t
>(abs(
end[1] - start[1])) + 1);
131 win.use_tensor_dimensions(out_shape);
134 bool out_of_bounds =
false;
135 Coordinates
offset(
id[0], 0, 0, batch_index);
136 for(uint32_t i = 1; i < 3; ++i)
138 offset.set(i,
end[i - 1] < start[i - 1] ? start[i - 1] -
id[i] : start[i - 1] +
id[i]);
139 if(
offset[i] < 0 ||
static_cast<uint32_t
>(
offset[i]) >
src.shape()[i] - 1)
141 out_of_bounds =
true;
147 *
reinterpret_cast<float *
>(out(
id)) =
static_cast<float>(*
reinterpret_cast<const T *
>(
src(
offset)));
151 *
reinterpret_cast<float *
>(out(
id)) = extrapolation_value;
159 template <
typename T>
169 const TensorShape scaled_image_shape(
src.shape()[0], crop_size.
x, crop_size.
y);
171 for(uint32_t i = 0; i < boxes.
shape()[1]; ++i)
174 std::floor((*
reinterpret_cast<const float *
>(boxes(
Coordinates(0, i)))) * (
src.shape()[2] - 1) + 0.5f));
176 std::floor((*
reinterpret_cast<const float *
>(boxes(
Coordinates(2, i)))) * (
src.shape()[2] - 1) + 0.5f));
178 SimpleTensor<float> scaled = scale_image(cropped, scaled_image_shape, method, extrapolation_value);
179 std::copy_n(
reinterpret_cast<float *
>(scaled.
data()), scaled.
num_elements(),
reinterpret_cast<float *
>(out(
Coordinates(0, 0, 0, i))));