diff --git a/hal/riscv-rvv/src/imgproc/box_filter.cpp b/hal/riscv-rvv/src/imgproc/box_filter.cpp index b8de93df21..8c66fb5540 100644 --- a/hal/riscv-rvv/src/imgproc/box_filter.cpp +++ b/hal/riscv-rvv/src/imgproc/box_filter.cpp @@ -65,9 +65,17 @@ static inline int boxFilterC1(int start, int end, const uchar* src_data, size_t int pj = common::borderInterpolate(offset_x + y - anchor_x, full_width, border_type); return pj < 0 ? noval : pj - offset_x; }; - auto p2idx = [&](int x, int y){ return (x + ksize) % ksize * width + y; }; + const int lower = ksize - 1 - anchor_y; + const bool reflective = border_type == BORDER_REFLECT || border_type == BORDER_REFLECT_101; + // Reflected rows can map across the nominal stripe halo, so keep the + // larger vertical halo on both sides of each independently processed stripe. + const int halo = reflective ? std::max(anchor_y, lower) : 0; + const int before = reflective ? halo : anchor_y; + const int after = reflective ? halo : lower; + const int ring = reflective ? 2 * halo + 1 : ksize; + auto p2idx = [&](int x, int y){ return (x + ring) % ring * width + y; }; - std::vector res(width * ksize); + std::vector res(width * ring); auto process = [&](int x, int y) { WT sum = 0; for (int i = 0; i < ksize; i++) @@ -82,7 +90,7 @@ static inline int boxFilterC1(int start, int end, const uchar* src_data, size_t }; const int left = anchor_x, right = width - (ksize - 1 - anchor_x); - for (int i = start - anchor_y; i < end + (ksize - 1 - anchor_y); i++) + for (int i = start - before; i < end + after; i++) { if (i + offset_y >= 0 && i + offset_y < full_height) { @@ -130,7 +138,7 @@ static inline int boxFilterC1(int start, int end, const uchar* src_data, size_t } } - int cur = i - (ksize - 1 - anchor_y); + int cur = i - after; if (cur >= start) { const WT* row0 = accessX(cur ) == noval ? nullptr : res.data() + p2idx(accessX(cur ), 0); @@ -188,9 +196,15 @@ static inline int boxFilterC3(int start, int end, const uchar* src_data, size_t int pj = common::borderInterpolate(offset_x + y - anchor_x, full_width, border_type); return pj < 0 ? noval : pj - offset_x; }; - auto p2idx = [&](int x, int y){ return ((x + ksize) % ksize * width + y) * 3; }; + const int lower = ksize - 1 - anchor_y; + const bool reflective = border_type == BORDER_REFLECT || border_type == BORDER_REFLECT_101; + const int halo = reflective ? std::max(anchor_y, lower) : 0; + const int before = reflective ? halo : anchor_y; + const int after = reflective ? halo : lower; + const int ring = reflective ? 2 * halo + 1 : ksize; + auto p2idx = [&](int x, int y){ return ((x + ring) % ring * width + y) * 3; }; - std::vector res(width * ksize * 3); + std::vector res(width * ring * 3); auto process = [&](int x, int y) { float sum0, sum1, sum2; sum0 = sum1 = sum2 = 0; @@ -210,7 +224,7 @@ static inline int boxFilterC3(int start, int end, const uchar* src_data, size_t }; const int left = anchor_x, right = width - (ksize - 1 - anchor_x); - for (int i = start - anchor_y; i < end + (ksize - 1 - anchor_y); i++) + for (int i = start - before; i < end + after; i++) { if (i + offset_y >= 0 && i + offset_y < full_height) { @@ -243,7 +257,7 @@ static inline int boxFilterC3(int start, int end, const uchar* src_data, size_t } } - int cur = i - (ksize - 1 - anchor_y); + int cur = i - after; if (cur >= start) { const float* row0 = accessX(cur ) == noval ? nullptr : res.data() + p2idx(accessX(cur ), 0); @@ -308,10 +322,15 @@ static inline int boxFilterFlat8U(int start, int end, const uchar* src_data, siz int pj = common::borderInterpolate(offset_x + y - anchor_x, full_width, border_type); return pj < 0 ? noval : pj - offset_x; }; + const int lower = ksize - 1 - anchor_y; + const bool reflective = border_type == BORDER_REFLECT || border_type == BORDER_REFLECT_101; + const int halo = reflective ? std::max(anchor_y, lower) : 0; + const int before = reflective ? halo : anchor_y; + const int after = reflective ? halo : lower; const int W = width * cn; // ring keeps one extra row so the row leaving the window is still present // for the incremental vertical update (ksize > 3) - constexpr int ring = ksize > 3 ? ksize + 1 : ksize; + const int ring = (reflective ? 2 * halo + 1 : ksize) + (ksize > 3 ? 1 : 0); auto p2idx = [&](int x){ return (x + ring) % ring * W; }; std::vector res(W * ring); @@ -335,7 +354,7 @@ static inline int boxFilterFlat8U(int start, int end, const uchar* src_data, siz }; const int left = anchor_x, right = width - (ksize - 1 - anchor_x); - for (int i = start - anchor_y; i < end + (ksize - 1 - anchor_y); i++) + for (int i = start - before; i < end + after; i++) { if (i + offset_y >= 0 && i + offset_y < full_height) { @@ -365,7 +384,7 @@ static inline int boxFilterFlat8U(int start, int end, const uchar* src_data, siz } } - int cur = i - (ksize - 1 - anchor_y); + int cur = i - after; if (cur >= start) { if (ksize > 3 && vsum_valid) diff --git a/modules/imgproc/test/test_filter.cpp b/modules/imgproc/test/test_filter.cpp index b650de6c1d..0ca934dc06 100644 --- a/modules/imgproc/test/test_filter.cpp +++ b/modules/imgproc/test/test_filter.cpp @@ -1656,8 +1656,9 @@ INSTANTIATE_TEST_CASE_P(Imgproc, Imgproc_BoxFilterInplaceROI, testing::Combine( TEST(Imgproc_BoxFilter, small_roi_parent_border) { const int borders[] = { BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_REFLECT_101 }; - const Rect rois[] = { Rect(7, 4, 1, 4), Rect(0, 6, 2, 2), Rect(6, 7, 2, 1), Rect(3, 3, 2, 4) }; - const Size ksizes[] = { Size(5, 3), Size(3, 5), Size(5, 5), Size(2, 4) }; + const Rect rois[] = { Rect(7, 4, 1, 4), Rect(0, 6, 2, 2), Rect(6, 7, 2, 1), + Rect(3, 0, 2, 4), Rect(3, 3, 2, 4) }; + const Size ksizes[] = { Size(5, 3), Size(3, 5), Size(3, 3), Size(5, 5), Size(2, 4) }; for (int type : { CV_32FC1, CV_32FC3, CV_64FC1 }) for (int border : borders) for (const Rect& r : rois) @@ -1666,6 +1667,9 @@ TEST(Imgproc_BoxFilter, small_roi_parent_border) for (int ax = 0; ax < k.width; ax++) for (int inplace = 0; inplace < 2; inplace++) { + SCOPED_TRACE(testing::Message() << "type=" << typeToString(type) << " border=" << border + << " roi=" << r << " ksize=" << k << " anchor=" << Point(ax, ay) + << " inplace=" << inplace); Mat img(8, 8, type); randu(img, 0, 100); const int cn = img.channels(); @@ -1698,9 +1702,7 @@ TEST(Imgproc_BoxFilter, small_roi_parent_border) boxFilter(img(r), dst, -1, k, Point(ax, ay), false, border); Mat dst64; dst.convertTo(dst64, CV_64F); - EXPECT_LE(cvtest::norm(expected, dst64, NORM_INF), 1e-3) - << "type=" << typeToString(type) << " border=" << border << " roi=" << r << " ksize=" << k - << " anchor=" << Point(ax, ay) << " inplace=" << inplace; + EXPECT_LE(cvtest::norm(expected, dst64, NORM_INF), 1e-3); } }