Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/op_bilateral_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ class BilateralFilter final : public IOperator {
* @param[in] stream The HIP stream to run this operation on.
* @param[in] input Input tensor with image batch data
* @param[out] output Output tensor for storing modified image batch data
* @param[in] diameter bilateral filter diameter.
* @param[in] diameter Bilateral filter neighborhood diameter. If @p diameter is
* greater than zero, the spatial radius is `diameter >> 1`. If @p diameter is
* less than or equal to zero, the radius is
* `static_cast<int>(std::roundf(sigmaSpace * 1.5f))` after clamping non-positive
* @p sigmaSpace to 1.0.
* @param[in] sigmaColor Gaussian exponent for color difference, expected
* to be positive, if it isn't, will be set to 1.0
* @param[in] sigmaSpace Gaussian exponent for position difference expected
Expand Down
8 changes: 3 additions & 5 deletions src/op_bilateral_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ THE SOFTWARE.

#include <hip/hip_runtime.h>

#include <cmath>
#include <functional>
#include <numeric>
#include <unordered_map>
Expand All @@ -45,8 +46,6 @@ void dispatch_bilateral_filter_border_mode(hipStream_t stream, const Tensor &inp
BorderWrapper<T, B> inputWrapper(input, borderValue);
ImageWrapper<T> outputWrapper(output);

int radius = diameter >> 1;

if (outputWrapper.channels() > 4 || outputWrapper.channels() < 1) {
throw Exception("Invalid channel size: cannot be greater than 4 or less than 1.", eStatusType::OUT_OF_BOUNDS);
}
Expand All @@ -62,9 +61,8 @@ void dispatch_bilateral_filter_border_mode(hipStream_t stream, const Tensor &inp
sigmaSpace = 1.0f;
}

if (radius <= 0) {
radius = std::round(sigmaSpace * 1.5f);
}
const int radius =
(diameter <= 0) ? static_cast<int>(std::roundf(sigmaSpace * 1.5f)) : (diameter >> 1);

float spaceCoeff = -1 / (2 * sigmaSpace * sigmaSpace);
float colorCoeff = -1 / (2 * sigmaColor * sigmaColor);
Expand Down
16 changes: 16 additions & 0 deletions tests/roccv/cpp/src/tests/operators/test_op_bilateral_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ int main(int argc, char** argv) {
TEST_CASE((TestCorrectness<uchar, BORDER_TYPE_REPLICATE>(4, 20, 20, FMT_U8, 4, 50.0f, 3.0f, {0.0, 0.0, 0.0, 0.0},
eDeviceType::GPU)));

// diameter <= 0: radius = roundf(sigmaSpace * 1.5f); sigmaSpace 1.2 -> radius 2
TEST_CASE((TestCorrectness<uchar, BORDER_TYPE_CONSTANT>(1, 20, 20, FMT_U8, 0, 50.0f, 1.2f, {0.0, 0.0, 0.0, 0.0},
eDeviceType::GPU)));
TEST_CASE((TestCorrectness<uchar3, BORDER_TYPE_REPLICATE>(2, 20, 20, FMT_RGB8, -1, 50.0f, 1.2f,
{0.0, 0.0, 0.0, 0.0}, eDeviceType::GPU)));
TEST_CASE((TestCorrectness<float1, BORDER_TYPE_WRAP>(1, 24, 24, FMT_F32, 0, 500.0f, 1.2f,
{500.0, 500.0, 0.0, 0.0}, eDeviceType::GPU)));

TEST_CASE((TestCorrectness<uchar3, BORDER_TYPE_CONSTANT>(1, 20, 20, FMT_RGB8, 4, 50.0f, 3.0f, {0.0, 0.0, 0.0, 0.0},
eDeviceType::GPU)));
TEST_CASE((TestCorrectness<uchar3, BORDER_TYPE_REFLECT>(2, 20, 20, FMT_RGB8, 4, 50.0f, 5.0f,
Expand Down Expand Up @@ -276,6 +284,14 @@ int main(int argc, char** argv) {
TEST_CASE((TestCorrectness<uchar, BORDER_TYPE_REPLICATE>(4, 20, 20, FMT_U8, 4, 50.0f, 3.0f, {0.0, 0.0, 0.0, 0.0},
eDeviceType::CPU)));

// diameter <= 0: radius = roundf(sigmaSpace * 1.5f); sigmaSpace 1.2 -> radius 2
TEST_CASE((TestCorrectness<uchar, BORDER_TYPE_CONSTANT>(1, 20, 20, FMT_U8, 0, 50.0f, 1.2f, {0.0, 0.0, 0.0, 0.0},
eDeviceType::CPU)));
TEST_CASE((TestCorrectness<uchar3, BORDER_TYPE_REPLICATE>(2, 20, 20, FMT_RGB8, -1, 50.0f, 1.2f,
{0.0, 0.0, 0.0, 0.0}, eDeviceType::CPU)));
TEST_CASE((TestCorrectness<float1, BORDER_TYPE_WRAP>(1, 24, 24, FMT_F32, 0, 500.0f, 1.2f,
{500.0, 500.0, 0.0, 0.0}, eDeviceType::CPU)));

TEST_CASE((TestCorrectness<uchar3, BORDER_TYPE_CONSTANT>(1, 20, 20, FMT_RGB8, 4, 50.0f, 3.0f, {0.0, 0.0, 0.0, 0.0},
eDeviceType::CPU)));
TEST_CASE((TestCorrectness<uchar3, BORDER_TYPE_REFLECT>(2, 20, 20, FMT_RGB8, 4, 50.0f, 5.0f,
Expand Down
4 changes: 3 additions & 1 deletion tests/roccv/python/test_op_bilateral_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
@pytest.mark.parametrize("border_mode", [rocpycv.eBorderType.CONSTANT])
@pytest.mark.parametrize("border_val", [[0, 0, 0, 1]])
@pytest.mark.parametrize("diameter,sigma_color,sigma_space", [
(3, 1.0, 1.0)
(3, 1.0, 1.0),
(0, 50.0, 1.2),
(-1, 50.0, 1.2),
])
@pytest.mark.parametrize("channels", [1, 3, 4])
@pytest.mark.parametrize("samples,height,width", [
Expand Down