Skip to content

Commit 04a1a6d

Browse files
author
Vladislav Vinogradov
committed
switched to Input/Output Array in gpu::compare
1 parent b866890 commit 04a1a6d

File tree

2 files changed

+36
-50
lines changed

2 files changed

+36
-50
lines changed

modules/gpuarithm/include/opencv2/gpuarithm.hpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ CV_EXPORTS void log(InputArray src, OutputArray dst, Stream& stream = Stream::Nu
9292
//! (dst(i,j) = pow(fabs(src(i,j)), power), otherwise
9393
CV_EXPORTS void pow(InputArray src, double power, OutputArray dst, Stream& stream = Stream::Null());
9494

95+
//! compares elements of two arrays (dst = src1 <cmpop> src2)
96+
CV_EXPORTS void compare(InputArray src1, InputArray src2, OutputArray dst, int cmpop, Stream& stream = Stream::Null());
97+
9598
//! computes the weighted sum of two arrays (dst = alpha*src1 + beta*src2 + gamma)
9699
CV_EXPORTS void addWeighted(const GpuMat& src1, double alpha, const GpuMat& src2, double beta, double gamma, GpuMat& dst,
97100
int dtype = -1, Stream& stream = Stream::Null());
@@ -102,10 +105,6 @@ static inline void scaleAdd(const GpuMat& src1, double alpha, const GpuMat& src2
102105
addWeighted(src1, alpha, src2, 1.0, 0.0, dst, -1, stream);
103106
}
104107

105-
//! compares elements of two arrays (c = a <cmpop> b)
106-
CV_EXPORTS void compare(const GpuMat& a, const GpuMat& b, GpuMat& c, int cmpop, Stream& stream = Stream::Null());
107-
CV_EXPORTS void compare(const GpuMat& a, Scalar sc, GpuMat& c, int cmpop, Stream& stream = Stream::Null());
108-
109108
//! performs per-elements bit-wise inversion
110109
CV_EXPORTS void bitwise_not(const GpuMat& src, GpuMat& dst, const GpuMat& mask=GpuMat(), Stream& stream = Stream::Null());
111110

modules/gpuarithm/src/element_operations.cpp

+33-46
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ void cv::gpu::log(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
6969

7070
void cv::gpu::pow(InputArray, double, OutputArray, Stream&) { throw_no_cuda(); }
7171

72-
void cv::gpu::compare(const GpuMat&, const GpuMat&, GpuMat&, int, Stream&) { throw_no_cuda(); }
73-
void cv::gpu::compare(const GpuMat&, Scalar, GpuMat&, int, Stream&) { throw_no_cuda(); }
72+
void cv::gpu::compare(InputArray, InputArray, OutputArray, int, Stream&) { throw_no_cuda(); }
7473

7574
void cv::gpu::bitwise_not(const GpuMat&, GpuMat&, const GpuMat&, Stream&) { throw_no_cuda(); }
7675

@@ -116,11 +115,11 @@ void cv::gpu::polarToCart(const GpuMat&, const GpuMat&, GpuMat&, GpuMat&, bool,
116115

117116
namespace
118117
{
119-
typedef void (*mat_mat_func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream);
120-
typedef void (*mat_scalar_func_t)(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream);
118+
typedef void (*mat_mat_func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream, int op);
119+
typedef void (*mat_scalar_func_t)(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream, int op);
121120

122121
void arithm_op(InputArray _src1, InputArray _src2, OutputArray _dst, InputArray _mask, double scale, int dtype, Stream& stream,
123-
mat_mat_func_t mat_mat_func, mat_scalar_func_t mat_scalar_func)
122+
mat_mat_func_t mat_mat_func, mat_scalar_func_t mat_scalar_func, int op = 0)
124123
{
125124
const int kind1 = _src1.kind();
126125
const int kind2 = _src2.kind();
@@ -175,11 +174,11 @@ namespace
175174
GpuMat dst = _dst.getGpuMat();
176175

177176
if (isScalar1)
178-
mat_scalar_func(src2, val, true, dst, mask, scale, stream);
177+
mat_scalar_func(src2, val, true, dst, mask, scale, stream, op);
179178
else if (isScalar2)
180-
mat_scalar_func(src1, val, false, dst, mask, scale, stream);
179+
mat_scalar_func(src1, val, false, dst, mask, scale, stream, op);
181180
else
182-
mat_mat_func(src1, src2, dst, mask, scale, stream);
181+
mat_mat_func(src1, src2, dst, mask, scale, stream, op);
183182
}
184183
}
185184

@@ -369,7 +368,7 @@ namespace arithm
369368
void addMat(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
370369
}
371370

372-
static void addMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream)
371+
static void addMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int)
373372
{
374373
typedef void (*func_t)(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
375374
static const func_t funcs[7][7] =
@@ -498,7 +497,7 @@ namespace arithm
498497
void addScalar(PtrStepSzb src1, double val, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
499498
}
500499

501-
static void addScalar(const GpuMat& src, Scalar val, bool, GpuMat& dst, const GpuMat& mask, double, Stream& _stream)
500+
static void addScalar(const GpuMat& src, Scalar val, bool, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int)
502501
{
503502
typedef void (*func_t)(PtrStepSzb src1, double val, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
504503
static const func_t funcs[7][7] =
@@ -620,7 +619,7 @@ namespace arithm
620619
void subMat(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
621620
}
622621

623-
static void subMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream)
622+
static void subMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int)
624623
{
625624
typedef void (*func_t)(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
626625
static const func_t funcs[7][7] =
@@ -749,7 +748,7 @@ namespace arithm
749748
void subScalar(PtrStepSzb src1, double val, bool inv, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
750749
}
751750

752-
static void subScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double, Stream& _stream)
751+
static void subScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int)
753752
{
754753
typedef void (*func_t)(PtrStepSzb src1, double val, bool inv, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
755754
static const func_t funcs[7][7] =
@@ -872,7 +871,7 @@ namespace arithm
872871
void mulMat(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, double scale, cudaStream_t stream);
873872
}
874873

875-
static void mulMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& _stream)
874+
static void mulMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& _stream, int)
876875
{
877876
typedef void (*func_t)(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, double scale, cudaStream_t stream);
878877
static const func_t funcs[7][7] =
@@ -966,7 +965,7 @@ namespace arithm
966965
void mulScalar(PtrStepSzb src1, double val, PtrStepSzb dst, cudaStream_t stream);
967966
}
968967

969-
static void mulScalar(const GpuMat& src, Scalar val, bool, GpuMat& dst, const GpuMat&, double scale, Stream& _stream)
968+
static void mulScalar(const GpuMat& src, Scalar val, bool, GpuMat& dst, const GpuMat&, double scale, Stream& _stream, int)
970969
{
971970
typedef void (*func_t)(PtrStepSzb src1, double val, PtrStepSzb dst, cudaStream_t stream);
972971
static const func_t funcs[7][7] =
@@ -1121,7 +1120,7 @@ namespace arithm
11211120
void divMat(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, double scale, cudaStream_t stream);
11221121
}
11231122

1124-
static void divMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& _stream)
1123+
static void divMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& _stream, int)
11251124
{
11261125
typedef void (*func_t)(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, double scale, cudaStream_t stream);
11271126
static const func_t funcs[7][7] =
@@ -1215,7 +1214,7 @@ namespace arithm
12151214
void divScalar(PtrStepSzb src1, double val, bool inv, PtrStepSzb dst, cudaStream_t stream);
12161215
}
12171216

1218-
static void divScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat&, double scale, Stream& _stream)
1217+
static void divScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat&, double scale, Stream& _stream, int)
12191218
{
12201219
typedef void (*func_t)(PtrStepSzb src1, double val, bool inv, PtrStepSzb dst, cudaStream_t stream);
12211220
static const func_t funcs[7][7] =
@@ -1379,7 +1378,7 @@ namespace arithm
13791378
void absDiffMat(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, cudaStream_t stream);
13801379
}
13811380

1382-
static void absDiffMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& _stream)
1381+
static void absDiffMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& _stream, int)
13831382
{
13841383
typedef void (*func_t)(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, cudaStream_t stream);
13851384
static const func_t funcs[] =
@@ -1451,7 +1450,7 @@ namespace arithm
14511450
void absDiffScalar(PtrStepSzb src1, double val, PtrStepSzb dst, cudaStream_t stream);
14521451
}
14531452

1454-
static void absDiffScalar(const GpuMat& src, Scalar val, bool, GpuMat& dst, const GpuMat&, double, Stream& stream)
1453+
static void absDiffScalar(const GpuMat& src, Scalar val, bool, GpuMat& dst, const GpuMat&, double, Stream& stream, int)
14551454
{
14561455
typedef void (*func_t)(PtrStepSzb src1, double val, PtrStepSzb dst, cudaStream_t stream);
14571456
static const func_t funcs[] =
@@ -1755,7 +1754,7 @@ namespace arithm
17551754
template <typename T> void cmpMatLe(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, cudaStream_t stream);
17561755
}
17571756

1758-
void cv::gpu::compare(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, int cmpop, Stream& s)
1757+
static void cmpMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& _stream, int cmpop)
17591758
{
17601759
using namespace arithm;
17611760

@@ -1780,19 +1779,7 @@ void cv::gpu::compare(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, int c
17801779
const int depth = src1.depth();
17811780
const int cn = src1.channels();
17821781

1783-
CV_Assert( depth <= CV_64F );
1784-
CV_Assert( src2.size() == src1.size() && src2.type() == src1.type() );
1785-
CV_Assert( cmpop >= CMP_EQ && cmpop <= CMP_NE );
1786-
1787-
if (depth == CV_64F)
1788-
{
1789-
if (!deviceSupports(NATIVE_DOUBLE))
1790-
CV_Error(cv::Error::StsUnsupportedFormat, "The device doesn't support double");
1791-
}
1792-
1793-
dst.create(src1.size(), CV_MAKE_TYPE(CV_8U, cn));
1794-
1795-
cudaStream_t stream = StreamAccessor::getStream(s);
1782+
cudaStream_t stream = StreamAccessor::getStream(_stream);
17961783

17971784
static const int codes[] =
17981785
{
@@ -1859,7 +1846,7 @@ namespace
18591846
}
18601847
}
18611848

1862-
void cv::gpu::compare(const GpuMat& src, Scalar sc, GpuMat& dst, int cmpop, Stream& stream)
1849+
static void cmpScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat&, double, Stream& stream, int cmpop)
18631850
{
18641851
using namespace arithm;
18651852

@@ -1881,24 +1868,24 @@ void cv::gpu::compare(const GpuMat& src, Scalar sc, GpuMat& dst, int cmpop, Stre
18811868
castScalar<unsigned char>, castScalar<signed char>, castScalar<unsigned short>, castScalar<short>, castScalar<int>, castScalar<float>, castScalar<double>
18821869
};
18831870

1884-
const int depth = src.depth();
1885-
const int cn = src.channels();
1886-
1887-
CV_Assert( depth <= CV_64F );
1888-
CV_Assert( cn <= 4 );
1889-
CV_Assert( cmpop >= CMP_EQ && cmpop <= CMP_NE );
1890-
1891-
if (depth == CV_64F)
1871+
if (inv)
18921872
{
1893-
if (!deviceSupports(NATIVE_DOUBLE))
1894-
CV_Error(cv::Error::StsUnsupportedFormat, "The device doesn't support double");
1873+
// src1 is a scalar; swap it with src2
1874+
cmpop = cmpop == CMP_LT ? CMP_GT : cmpop == CMP_LE ? CMP_GE :
1875+
cmpop == CMP_GE ? CMP_LE : cmpop == CMP_GT ? CMP_LT : cmpop;
18951876
}
18961877

1897-
dst.create(src.size(), CV_MAKE_TYPE(CV_8U, cn));
1878+
const int depth = src.depth();
1879+
const int cn = src.channels();
1880+
1881+
cast_func[depth](val);
18981882

1899-
cast_func[depth](sc);
1883+
funcs[depth][cmpop](src, cn, val.val, dst, StreamAccessor::getStream(stream));
1884+
}
19001885

1901-
funcs[depth][cmpop](src, cn, sc.val, dst, StreamAccessor::getStream(stream));
1886+
void cv::gpu::compare(InputArray src1, InputArray src2, OutputArray dst, int cmpop, Stream& stream)
1887+
{
1888+
arithm_op(src1, src2, dst, noArray(), 1.0, CV_8U, stream, cmpMat, cmpScalar, cmpop);
19021889
}
19031890

19041891
//////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)