Skip to content

Commit 3d8ca01

Browse files
author
Vladislav Vinogradov
committed
switched to Input/Output Array in split/merge operations
1 parent 58c4d0e commit 3d8ca01

File tree

3 files changed

+49
-59
lines changed

3 files changed

+49
-59
lines changed

modules/gpuarithm/include/opencv2/gpuarithm.hpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ CV_EXPORTS void cartToPolar(InputArray x, InputArray y, OutputArray magnitude, O
162162
//! supports only floating-point source
163163
CV_EXPORTS void polarToCart(InputArray magnitude, InputArray angle, OutputArray x, OutputArray y, bool angleInDegrees = false, Stream& stream = Stream::Null());
164164

165+
//! makes multi-channel array out of several single-channel arrays
166+
CV_EXPORTS void merge(const GpuMat* src, size_t n, OutputArray dst, Stream& stream = Stream::Null());
167+
CV_EXPORTS void merge(const std::vector<GpuMat>& src, OutputArray dst, Stream& stream = Stream::Null());
168+
169+
//! copies each plane of a multi-channel array to a dedicated array
170+
CV_EXPORTS void split(InputArray src, GpuMat* dst, Stream& stream = Stream::Null());
171+
CV_EXPORTS void split(InputArray src, std::vector<GpuMat>& dst, Stream& stream = Stream::Null());
172+
165173
//! implements generalized matrix product algorithm GEMM from BLAS
166174
CV_EXPORTS void gemm(const GpuMat& src1, const GpuMat& src2, double alpha,
167175
const GpuMat& src3, double beta, GpuMat& dst, int flags = 0, Stream& stream = Stream::Null());
@@ -179,18 +187,6 @@ CV_EXPORTS void flip(const GpuMat& a, GpuMat& b, int flipCode, Stream& stream =
179187
//! supports CV_8UC1, CV_8UC3 types
180188
CV_EXPORTS void LUT(const GpuMat& src, const Mat& lut, GpuMat& dst, Stream& stream = Stream::Null());
181189

182-
//! makes multi-channel array out of several single-channel arrays
183-
CV_EXPORTS void merge(const GpuMat* src, size_t n, GpuMat& dst, Stream& stream = Stream::Null());
184-
185-
//! makes multi-channel array out of several single-channel arrays
186-
CV_EXPORTS void merge(const std::vector<GpuMat>& src, GpuMat& dst, Stream& stream = Stream::Null());
187-
188-
//! copies each plane of a multi-channel array to a dedicated array
189-
CV_EXPORTS void split(const GpuMat& src, GpuMat* dst, Stream& stream = Stream::Null());
190-
191-
//! copies each plane of a multi-channel array to a dedicated array
192-
CV_EXPORTS void split(const GpuMat& src, std::vector<GpuMat>& dst, Stream& stream = Stream::Null());
193-
194190
//! scales and shifts array elements so that either the specified norm (alpha) or the minimum (alpha) and maximum (beta) array values get the specified values
195191
CV_EXPORTS void normalize(const GpuMat& src, GpuMat& dst, double alpha = 1, double beta = 0,
196192
int norm_type = NORM_L2, int dtype = -1, const GpuMat& mask = GpuMat());

modules/gpuarithm/src/core.cpp

+39-45
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ using namespace cv::gpu;
4747

4848
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
4949

50-
void cv::gpu::merge(const GpuMat* /*src*/, size_t /*count*/, GpuMat& /*dst*/, Stream& /*stream*/) { throw_no_cuda(); }
51-
void cv::gpu::merge(const std::vector<GpuMat>& /*src*/, GpuMat& /*dst*/, Stream& /*stream*/) { throw_no_cuda(); }
50+
void cv::gpu::merge(const GpuMat*, size_t, OutputArray, Stream&) { throw_no_cuda(); }
51+
void cv::gpu::merge(const std::vector<GpuMat>&, OutputArray, Stream&) { throw_no_cuda(); }
5252

53-
void cv::gpu::split(const GpuMat& /*src*/, GpuMat* /*dst*/, Stream& /*stream*/) { throw_no_cuda(); }
54-
void cv::gpu::split(const GpuMat& /*src*/, std::vector<GpuMat>& /*dst*/, Stream& /*stream*/) { throw_no_cuda(); }
53+
void cv::gpu::split(InputArray, GpuMat*, Stream&) { throw_no_cuda(); }
54+
void cv::gpu::split(InputArray, std::vector<GpuMat>&, Stream&) { throw_no_cuda(); }
5555

5656
void cv::gpu::transpose(const GpuMat&, GpuMat&, Stream&) { throw_no_cuda(); }
5757

@@ -70,66 +70,60 @@ namespace cv { namespace gpu { namespace cudev
7070
{
7171
namespace split_merge
7272
{
73-
void merge_caller(const PtrStepSzb* src, PtrStepSzb& dst, int total_channels, size_t elem_size, const cudaStream_t& stream);
74-
void split_caller(const PtrStepSzb& src, PtrStepSzb* dst, int num_channels, size_t elem_size1, const cudaStream_t& stream);
73+
void merge(const PtrStepSzb* src, PtrStepSzb& dst, int total_channels, size_t elem_size, const cudaStream_t& stream);
74+
void split(const PtrStepSzb& src, PtrStepSzb* dst, int num_channels, size_t elem_size1, const cudaStream_t& stream);
7575
}
7676
}}}
7777

7878
namespace
7979
{
80-
void merge(const GpuMat* src, size_t n, GpuMat& dst, const cudaStream_t& stream)
80+
void merge_caller(const GpuMat* src, size_t n, OutputArray _dst, Stream& stream)
8181
{
82-
using namespace ::cv::gpu::cudev::split_merge;
82+
CV_Assert( src != 0 );
83+
CV_Assert( n > 0 && n <= 4 );
8384

84-
CV_Assert(src);
85-
CV_Assert(n > 0);
85+
const int depth = src[0].depth();
86+
const Size size = src[0].size();
8687

87-
int depth = src[0].depth();
88-
Size size = src[0].size();
88+
for (size_t i = 0; i < n; ++i)
89+
{
90+
CV_Assert( src[i].size() == size );
91+
CV_Assert( src[i].depth() == depth );
92+
CV_Assert( src[i].channels() == 1 );
93+
}
8994

9095
if (depth == CV_64F)
9196
{
9297
if (!deviceSupports(NATIVE_DOUBLE))
9398
CV_Error(cv::Error::StsUnsupportedFormat, "The device doesn't support double");
9499
}
95100

96-
bool single_channel_only = true;
97-
int total_channels = 0;
98-
99-
for (size_t i = 0; i < n; ++i)
101+
if (n == 1)
100102
{
101-
CV_Assert(src[i].size() == size);
102-
CV_Assert(src[i].depth() == depth);
103-
single_channel_only = single_channel_only && src[i].channels() == 1;
104-
total_channels += src[i].channels();
103+
src[0].copyTo(_dst, stream);
105104
}
106-
107-
CV_Assert(single_channel_only);
108-
CV_Assert(total_channels <= 4);
109-
110-
if (total_channels == 1)
111-
src[0].copyTo(dst);
112105
else
113106
{
114-
dst.create(size, CV_MAKETYPE(depth, total_channels));
107+
_dst.create(size, CV_MAKE_TYPE(depth, (int)n));
108+
GpuMat dst = _dst.getGpuMat();
115109

116110
PtrStepSzb src_as_devmem[4];
117111
for(size_t i = 0; i < n; ++i)
118112
src_as_devmem[i] = src[i];
119113

120114
PtrStepSzb dst_as_devmem(dst);
121-
merge_caller(src_as_devmem, dst_as_devmem, total_channels, CV_ELEM_SIZE(depth), stream);
115+
cv::gpu::cudev::split_merge::merge(src_as_devmem, dst_as_devmem, (int)n, CV_ELEM_SIZE(depth), StreamAccessor::getStream(stream));
122116
}
123117
}
124118

125-
void split(const GpuMat& src, GpuMat* dst, const cudaStream_t& stream)
119+
void split_caller(const GpuMat& src, GpuMat* dst, Stream& stream)
126120
{
127-
using namespace ::cv::gpu::cudev::split_merge;
121+
CV_Assert( dst != 0 );
128122

129-
CV_Assert(dst);
123+
const int depth = src.depth();
124+
const int num_channels = src.channels();
130125

131-
int depth = src.depth();
132-
int num_channels = src.channels();
126+
CV_Assert( num_channels <= 4 );
133127

134128
if (depth == CV_64F)
135129
{
@@ -139,45 +133,45 @@ namespace
139133

140134
if (num_channels == 1)
141135
{
142-
src.copyTo(dst[0]);
136+
src.copyTo(dst[0], stream);
143137
return;
144138
}
145139

146140
for (int i = 0; i < num_channels; ++i)
147141
dst[i].create(src.size(), depth);
148142

149-
CV_Assert(num_channels <= 4);
150-
151143
PtrStepSzb dst_as_devmem[4];
152144
for (int i = 0; i < num_channels; ++i)
153145
dst_as_devmem[i] = dst[i];
154146

155147
PtrStepSzb src_as_devmem(src);
156-
split_caller(src_as_devmem, dst_as_devmem, num_channels, src.elemSize1(), stream);
148+
cv::gpu::cudev::split_merge::split(src_as_devmem, dst_as_devmem, num_channels, src.elemSize1(), StreamAccessor::getStream(stream));
157149
}
158150
}
159151

160-
void cv::gpu::merge(const GpuMat* src, size_t n, GpuMat& dst, Stream& stream)
152+
void cv::gpu::merge(const GpuMat* src, size_t n, OutputArray dst, Stream& stream)
161153
{
162-
::merge(src, n, dst, StreamAccessor::getStream(stream));
154+
merge_caller(src, n, dst, stream);
163155
}
164156

165157

166-
void cv::gpu::merge(const std::vector<GpuMat>& src, GpuMat& dst, Stream& stream)
158+
void cv::gpu::merge(const std::vector<GpuMat>& src, OutputArray dst, Stream& stream)
167159
{
168-
::merge(&src[0], src.size(), dst, StreamAccessor::getStream(stream));
160+
merge_caller(&src[0], src.size(), dst, stream);
169161
}
170162

171-
void cv::gpu::split(const GpuMat& src, GpuMat* dst, Stream& stream)
163+
void cv::gpu::split(InputArray _src, GpuMat* dst, Stream& stream)
172164
{
173-
::split(src, dst, StreamAccessor::getStream(stream));
165+
GpuMat src = _src.getGpuMat();
166+
split_caller(src, dst, stream);
174167
}
175168

176-
void cv::gpu::split(const GpuMat& src, std::vector<GpuMat>& dst, Stream& stream)
169+
void cv::gpu::split(InputArray _src, std::vector<GpuMat>& dst, Stream& stream)
177170
{
171+
GpuMat src = _src.getGpuMat();
178172
dst.resize(src.channels());
179173
if(src.channels() > 0)
180-
::split(src, &dst[0], StreamAccessor::getStream(stream));
174+
split_caller(src, &dst[0], stream);
181175
}
182176

183177
////////////////////////////////////////////////////////////////////////

modules/gpuarithm/src/cuda/split_merge.cu

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ namespace cv { namespace gpu { namespace cudev
278278
}
279279

280280

281-
void merge_caller(const PtrStepSzb* src, PtrStepSzb& dst,
281+
void merge(const PtrStepSzb* src, PtrStepSzb& dst,
282282
int total_channels, size_t elem_size,
283283
const cudaStream_t& stream)
284284
{
@@ -487,7 +487,7 @@ namespace cv { namespace gpu { namespace cudev
487487
}
488488

489489

490-
void split_caller(const PtrStepSzb& src, PtrStepSzb* dst, int num_channels, size_t elem_size1, const cudaStream_t& stream)
490+
void split(const PtrStepSzb& src, PtrStepSzb* dst, int num_channels, size_t elem_size1, const cudaStream_t& stream)
491491
{
492492
static SplitFunction split_func_tbl[] =
493493
{

0 commit comments

Comments
 (0)