8000 Merge pull request #13879 from chacha21:REDUCE_SUM2 · thewoz/opencv@f497cbd · GitHub 10000
[go: up one dir, main page]

Skip to content

Commit f497cbd

Browse files
chacha21thewoz
authored andcommitted
Merge pull request opencv#13879 from chacha21:REDUCE_SUM2
add REDUCE_SUM2 opencv#13879 proposal to add REDUCE_SUM2 to cv::reduce, an operation that sums up the square of elements
1 parent d6c6b73 commit f497cbd

File tree

9 files changed

+254
-92
lines changed

9 files changed

+254
-92
lines changed

modules/core/include/opencv2/core.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ enum KmeansFlags {
230230
enum ReduceTypes { REDUCE_SUM = 0, //!< the output is the sum of all rows/columns of the matrix.
231231
REDUCE_AVG = 1, //!< the output is the mean vector of all rows/columns of the matrix.
232232
REDUCE_MAX = 2, //!< the output is the maximum (column/row-wise) of all rows/columns of the matrix.
233-
REDUCE_MIN = 3 //!< the output is the minimum (column/row-wise) of all rows/columns of the matrix.
233+
REDUCE_MIN = 3, //!< the output is the minimum (column/row-wise) of all rows/columns of the matrix.
234+
REDUCE_SUM2 = 4 //!< the output is the sum of all squared rows/columns of the matrix.
234235
};
235236

236237
//! @} core_array
@@ -903,7 +904,7 @@ The function #reduce reduces the matrix to a vector by treating the matrix rows/
903904
1D vectors and performing the specified operation on the vectors until a single row/column is
904905
obtained. For example, the function can be used to compute horizontal and vertical projections of a
905906
raster image. In case of #REDUCE_MAX and #REDUCE_MIN , the output image should have the same type as the source one.
906-
In case of #REDUCE_SUM and #REDUCE_AVG , the output may have a larger element bit-depth to preserve accuracy.
907+
In case of #REDUCE_SUM, #REDUCE_SUM2 and #REDUCE_AVG , the output may have a larger element bit-depth to preserve accuracy.
907908
And multi-channel arrays are also supported in these two reduction modes.
908909
909910
The following code demonstrates its usage for a single channel matrix.

modules/core/perf/opencl/perf_arithm.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ OCL_PERF_TEST_P(ReduceMinMaxFixture, Reduce,
11501150
SANITY_CHECK(dst, eps);
11511151
}
11521152

1153-
CV_ENUM(ReduceAccOp, CV_REDUCE_SUM, CV_REDUCE_AVG)
1153+
CV_ENUM(ReduceAccOp, REDUCE_SUM, REDUCE_AVG, REDUCE_SUM2)
11541154

11551155
typedef tuple<Size, std::pair<MatType, MatType>, int, ReduceAccOp> ReduceAccParams;
11561156
typedef TestBaseWithParam<ReduceAccParams> ReduceAccFixture;
@@ -1168,7 +1168,6 @@ OCL_PERF_TEST_P(ReduceAccFixture, Reduce,
11681168
dim = get<2>(params), op = get<3>(params);
11691169
const Size srcSize = get<0>(params),
11701170
dstSize(dim == 0 ? srcSize.width : 1, dim == 0 ? 1 : srcSize.height);
1171-
const double eps = CV_MAT_DEPTH(dtype) <= CV_32S ? 1 : 3e-4;
11721171

11731172
checkDeviceMaxMemoryAllocSize(srcSize, stype);
11741173
checkDeviceMaxMemoryAllocSize(srcSize, dtype);
@@ -1178,7 +1177,7 @@ OCL_PERF_TEST_P(ReduceAccFixture, Reduce,
11781177

11791178
OCL_TEST_CYCLE() cv::reduce(src, dst, dim, op, dtype);
11801179

1181-
SANITY_CHECK(dst, eps);
1180+
SANITY_CHECK_NOTHING();
11821181
}
11831182

11841183
} } // namespace opencv_test::ocl

modules/core/perf/perf_reduce.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace opencv_test
55
{
66
using namespace perf;
77

8-
CV_ENUM(ROp, CV_REDUCE_SUM, CV_REDUCE_AVG, CV_REDUCE_MAX, CV_REDUCE_MIN)
8+
CV_ENUM(ROp, REDUCE_SUM, REDUCE_AVG, REDUCE_MAX, REDUCE_MIN, REDUCE_SUM2)
99
typedef tuple<Size, MatType, ROp> Size_MatType_ROp_t;
1010
typedef perf::TestBaseWithParam<Size_MatType_ROp_t> Size_MatType_ROp;
1111

@@ -23,7 +23,7 @@ PERF_TEST_P(Size_MatType_ROp, reduceR,
2323
int reduceOp = get<2>(GetParam());
2424

2525
int ddepth = -1;
26-
if( CV_MAT_DEPTH(matType) < CV_32S && (reduceOp == REDUCE_SUM || reduceOp == REDUCE_AVG) )
26+
if( CV_MAT_DEPTH(matType) < CV_32S && (reduceOp == REDUCE_SUM || reduceOp == REDUCE_AVG || reduceOp == REDUCE_SUM2) )
2727
ddepth = CV_32S;
2828

2929
Mat src(sz, matType);
@@ -35,7 +35,7 @@ PERF_TEST_P(Size_MatType_ROp, reduceR,
3535
int runs = 15;
3636
TEST_CYCLE_MULTIRUN(runs) reduce(src, vec, 0, reduceOp, ddepth);
3737

38-
SANITY_CHECK(vec, 1);
38+
SANITY_CHECK_NOTHING();
3939
}
4040

4141
PERF_TEST_P(Size_MatType_ROp, reduceC,
@@ -51,7 +51,7 @@ PERF_TEST_P(Size_MatType_ROp, reduceC,
5151
int reduceOp = get<2>(GetParam());
5252

5353
int ddepth = -1;
54-
if( CV_MAT_DEPTH(matType)< CV_32S && (reduceOp == REDUCE_SUM || reduceOp == REDUCE_AVG) )
54+
if( CV_MAT_DEPTH(matType)< CV_32S && (reduceOp == REDUCE_SUM || reduceOp == REDUCE_AVG || reduceOp == REDUCE_SUM2) )
5555
ddepth = CV_32S;
5656

5757
Mat src(sz, matType);
@@ -62,7 +62,7 @@ PERF_TEST_P(Size_MatType_ROp, reduceC,
6262

6363
TEST_CYCLE() reduce(src, vec, 1, reduceOp, ddepth);
6464

65-
SANITY_CHECK(vec, 1);
65+
SANITY_CHECK_NOTHING();
6666
}
6767

6868
typedef tuple<Size, MatType, int> Size_MatType_RMode_t;

0 commit comments

Comments
 (0)
0