SimpleTensor<T> median3x3(const SimpleTensor<T> &src, BorderMode border_mode, T constant_border_value) { SimpleTensor<T> dst(src.shape(), src.data_type()); const int size_tot_filter = filter_size * filter_size; for(int src_idx = 0; src_idx < src.num_elements(); ++src_idx) { std::array<T, size_tot_filter> filter_elems = { { 0 } }; Coordinates id = index2coord(src.shape(), src_idx); const int x = id.x(); const int y = id.y(); for(int j = y - static_cast<int>(border_size.top), index = 0; j <= y + static_cast<int>(border_size.bottom); ++j) { for(int i = x - static_cast<int>(border_size.left); i <= x + static_cast<int>(border_size.right); ++i, ++index) { id.set(0, i); id.set(1, j); filter_elems[index] = tensor_elem_at(src, id, border_mode, constant_border_value); } } std::sort(filter_elems.begin(), filter_elems.end()); dst[src_idx] = filter_elems[size_tot_filter / 2]; } return dst; }
SimpleTensor<T> warp_perspective(const SimpleTensor<T> &src, SimpleTensor<T> &valid_mask, const float *matrix, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value) { SimpleTensor<T> dst(src.shape(), src.data_type()); // x0 = M00 * x + M01 * y + M02 // y0 = M10 * x + M11 * y + M12 // z0 = M20 * x + M21 * y + M22 // xn = x0 / z0 // yn = y0 / z0 const float M00 = matrix[0]; const float M10 = matrix[1]; const float M20 = matrix[2]; const float M01 = matrix[0 + 1 * 3]; const float M11 = matrix[1 + 1 * 3]; const float M21 = matrix[2 + 1 * 3]; const float M02 = matrix[0 + 2 * 3]; const float M12 = matrix[1 + 2 * 3]; const float M22 = matrix[2 + 2 * 3]; const int width = src.shape().x(); const int height = src.shape().y(); for(int element_idx = 0; element_idx < src.num_elements(); ++element_idx) { valid_mask[element_idx] = 1; Coordinates id = index2coord(src.shape(), element_idx); const int idx = id.x(); const int idy = id.y(); const float z0 = M20 * idx + M21 * idy + M22; const float x0 = (M00 * idx + M01 * idy + M02); const float y0 = (M10 * idx + M11 * idy + M12); const float xn = x0 / z0; const float yn = y0 / z0; id.set(0, static_cast<int>(std::floor(xn))); id.set(1, static_cast<int>(std::floor(yn))); if((0 <= yn) && (yn < height) && (0 <= xn) && (xn < width)) { switch(policy) { case InterpolationPolicy::NEAREST_NEIGHBOR: dst[element_idx] = tensor_elem_at(src, id, border_mode, constant_border_value); break; case InterpolationPolicy::BILINEAR: (valid_bilinear_policy(xn, yn, width, height, border_mode)) ? dst[element_idx] = bilinear_policy(src, id, xn, yn, border_mode, constant_border_value) : valid_mask[element_idx] = 0; break; case InterpolationPolicy::AREA: default: ARM_COMPUTE_ERROR("Interpolation not supported"); break; } } else { if(border_mode == BorderMode::UNDEFINED) { valid_mask[element_idx] = 0; } else { switch(policy) { case InterpolationPolicy::NEAREST_NEIGHBOR: if(border_mode == BorderMode::CONSTANT) { dst[element_idx] = constant_border_value; } else if(border_mode == BorderMode::REPLICATE) { id.set(0, std::max(0, std::min(static_cast<int>(xn), width - 1))); id.set(1, std::max(0, std::min(static_cast<int>(yn), height - 1))); dst[element_idx] = src[coord2index(src.shape(), id)]; } break; case InterpolationPolicy::BILINEAR: dst[element_idx] = bilinear_policy(src, id, xn, yn, border_mode, constant_border_value); break; case InterpolationPolicy::AREA: default: ARM_COMPUTE_ERROR("Interpolation not supported"); break; } } } } return dst; }
SimpleTensor<T> non_linear_filter(const SimpleTensor<T> &src, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value) { SimpleTensor<T> dst(src.shape(), src.data_type()); ARM_COMPUTE_ERROR_ON(pattern == MatrixPattern::OTHER && mask == nullptr); ARM_COMPUTE_UNUSED(pattern); using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type; const int sq_mask_size = mask_size * mask_size; const int half_mask_size = mask_size / 2; std::vector<intermediate_type> vals(sq_mask_size); intermediate_type current_value = 0; const ValidRegion valid_region = shape_to_valid_region(src.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(half_mask_size)); for(int element_idx = 0, count = 0, index = 0; element_idx < src.num_elements(); ++element_idx, count = 0, index = 0) { Coordinates id = index2coord(src.shape(), element_idx); if(is_in_valid_region(valid_region, id)) { int idx = id.x(); int idy = id.y(); for(int y = idy - half_mask_size; y <= idy + half_mask_size; ++y) { for(int x = idx - half_mask_size; x <= idx + half_mask_size; ++x, ++index) { id.set(0, x); id.set(1, y); current_value = tensor_elem_at(src, id, border_mode, constant_border_value); if(mask[index] == 255) { vals[count] = static_cast<intermediate_type>(current_value); ++count; } } } std::sort(vals.begin(), vals.begin() + count); ARM_COMPUTE_ERROR_ON(count == 0); switch(function) { case NonLinearFilterFunction::MIN: dst[element_idx] = saturate_cast<T>(vals[0]); break; case NonLinearFilterFunction::MAX: dst[element_idx] = saturate_cast<T>(vals[count - 1]); break; case NonLinearFilterFunction::MEDIAN: dst[element_idx] = saturate_cast<T>(vals[count / 2]); break; default: ARM_COMPUTE_ERROR("Unsupported NonLinearFilter function."); } } } return dst; }