mirror of
https://github.com/opencv/opencv.git
synced 2026-11-22 09:23:44 -06:00
Use MatShape instead of MatSize inside cv::Mat/cv::UMat #27757 **Merge together with https://github.com/opencv/opencv_contrib/pull/3996** --- This PR continues cv::Mat/cv::UMat refactoring. See #26056, where `MatShape` was introduced. Now it's put inside cv::Mat/cv::UMat instead of a weird `MatSize`. MatSize is now an alias for MatShape: **before:** ``` struct MatShape { ... }; struct MatSize { ... }; struct Mat { ... int dims; int rows; int cols; ... MatShape shape() const { ... /* constructs MatShape out of MatSize and returns it; layout is always 'unknown', because we don't store it */ } MatSize size; // size is not valid without the parent cv::Mat, // because size.p may point to Mat::rows or to Mat::cols, // depending on the dimensionality, and dims() returns Mat::dims. MatStep step; // may allocate memory, depending on the dimensionality. ... }; ``` **after:** ``` struct MatShape { ... }; typedef MatShape MatSize; // they are now synonyms struct Mat { ... int dims; int rows; int cols; ... MatShape shape() const { return size; } // just return the embedded shape (including the proper layout information) MatSize size; // size is self-contained data structure that can be used without the parent cv::Mat. // size.dims is now a copy of dims; size.p[*] contains copies of Mat::rows and Mat::cols when dims <= 2. MatStep step; // does not allocate extra memory buffers. ... }; ``` There are several reasons to do that: 1. the main reason is to be able to store data layout (MatShape::layout) inside each cv::Mat/cv::UMat. This is necessary for the proper shape inference in DNN module. In particular, it's necessary for the next step of DNN inference optimization where we introduce block-layout-optimized convolution and other operations. Later on, we can use layout information to support non-interleaved images (e.g. RRR...GGG...BBB...) or even batches of such images in core/imgproc modules. 2. the other reason is to represent 3D/4D/5D etc. tensors as cv::Mat/cv::UMat instances more conveniently, without extra dynamic memory allocation. Before this patch we allocated some memory buffers dynamically to store shape & steps for more than 2D arrays. Now the whole cv::Mat/cv::UMat header can be stored completely on stack/in a container. Creating another copy of Mat/UMat header is now done more efficiently. 3. the third reason is to introduce the new coding pattern: `dst.create(src.size, <dst_type>);`. The pattern is suitable for most of element-wise (including cloning) and filtering operations. This pattern does not only look crisp and self-documenting, it will also automatically copy shape (including layout) from the source tensor into the destination matrix/tensor. 4. in the future we might add `colorspace` member to MatShape that will allow to distinguish RGB from BGR or NV12. `dst.create(src.size, <dst_type>);` will then copy the colorspace information as well. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
120 lines
4.1 KiB
C++
120 lines
4.1 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html
|
|
|
|
|
|
#include "precomp.hpp"
|
|
#include "opencl_kernels_core.hpp"
|
|
|
|
#include "convert_scale.simd.hpp"
|
|
#include "convert_scale.simd_declarations.hpp" // defines CV_CPU_DISPATCH_MODES_ALL=AVX2,...,BASELINE based on CMakeLists.txt content
|
|
|
|
namespace cv
|
|
{
|
|
|
|
static BinaryFunc getCvtScaleAbsFunc(int depth)
|
|
{
|
|
CV_INSTRUMENT_REGION();
|
|
CV_CPU_DISPATCH(getCvtScaleAbsFunc, (depth),
|
|
CV_CPU_DISPATCH_MODES_ALL);
|
|
}
|
|
|
|
BinaryFunc getConvertScaleFunc(int sdepth, int ddepth)
|
|
{
|
|
CV_INSTRUMENT_REGION();
|
|
CV_CPU_DISPATCH(getConvertScaleFunc, (sdepth, ddepth),
|
|
CV_CPU_DISPATCH_MODES_ALL);
|
|
}
|
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
static bool ocl_convertScaleAbs( InputArray _src, OutputArray _dst, double alpha, double beta )
|
|
{
|
|
const ocl::Device & d = ocl::Device::getDefault();
|
|
|
|
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
|
bool doubleSupport = d.doubleFPConfig() > 0;
|
|
if (!doubleSupport && depth == CV_64F)
|
|
return false;
|
|
|
|
_dst.createSameSize(_src, CV_8UC(cn));
|
|
int kercn = 1;
|
|
if (d.isIntel())
|
|
{
|
|
static const int vectorWidths[] = {4, 4, 4, 4, 4, 4, 4, -1};
|
|
kercn = ocl::checkOptimalVectorWidth( vectorWidths, _src, _dst,
|
|
noArray(), noArray(), noArray(),
|
|
noArray(), noArray(), noArray(),
|
|
noArray(), ocl::OCL_VECTOR_MAX);
|
|
}
|
|
else
|
|
kercn = ocl::predictOptimalVectorWidthMax(_src, _dst);
|
|
|
|
int rowsPerWI = d.isIntel() ? 4 : 1;
|
|
char cvt[2][50];
|
|
int wdepth = std::max(depth, CV_32F);
|
|
String build_opt = format("-D OP_CONVERT_SCALE_ABS -D UNARY_OP -D dstT=%s -D DEPTH_dst=%d -D srcT1=%s"
|
|
" -D workT=%s -D wdepth=%d -D convertToWT1=%s -D convertToDT=%s"
|
|
" -D workT1=%s -D rowsPerWI=%d%s",
|
|
ocl::typeToStr(CV_8UC(kercn)), CV_8U,
|
|
ocl::typeToStr(CV_MAKE_TYPE(depth, kercn)),
|
|
ocl::typeToStr(CV_MAKE_TYPE(wdepth, kercn)), wdepth,
|
|
ocl::convertTypeStr(depth, wdepth, kercn, cvt[0], sizeof(cvt[0])),
|
|
ocl::convertTypeStr(wdepth, CV_8U, kercn, cvt[1], sizeof(cvt[1])),
|
|
ocl::typeToStr(wdepth), rowsPerWI,
|
|
doubleSupport ? " -D DOUBLE_SUPPORT" : "");
|
|
ocl::Kernel k("KF", ocl::core::arithm_oclsrc, build_opt);
|
|
if (k.empty())
|
|
return false;
|
|
|
|
UMat src = _src.getUMat();
|
|
UMat dst = _dst.getUMat();
|
|
|
|
ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
|
|
dstarg = ocl::KernelArg::WriteOnly(dst, cn, kercn);
|
|
|
|
if (wdepth == CV_32F)
|
|
k.args(srcarg, dstarg, (float)alpha, (float)beta);
|
|
else if (wdepth == CV_64F)
|
|
k.args(srcarg, dstarg, alpha, beta);
|
|
|
|
size_t globalsize[2] = { (size_t)src.cols * cn / kercn, ((size_t)src.rows + rowsPerWI - 1) / rowsPerWI };
|
|
return k.run(2, globalsize, NULL, false);
|
|
}
|
|
|
|
#endif
|
|
|
|
void convertScaleAbs(InputArray _src, OutputArray _dst, double alpha, double beta)
|
|
{
|
|
CV_INSTRUMENT_REGION();
|
|
|
|
CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
|
|
ocl_convertScaleAbs(_src, _dst, alpha, beta))
|
|
|
|
Mat src = _src.getMat();
|
|
int cn = src.channels();
|
|
double scale[] = {alpha, beta};
|
|
_dst.create( src.size, CV_8UC(cn) );
|
|
Mat dst = _dst.getMat();
|
|
BinaryFunc func = getCvtScaleAbsFunc(src.depth());
|
|
CV_Assert( func != 0 );
|
|
|
|
if( src.dims <= 2 )
|
|
{
|
|
Size sz = getContinuousSize2D(src, dst, cn);
|
|
func( src.ptr(), src.step, 0, 0, dst.ptr(), dst.step, sz, scale );
|
|
}
|
|
else
|
|
{
|
|
const Mat* arrays[] = {&src, &dst, 0};
|
|
uchar* ptrs[2] = {};
|
|
NAryMatIterator it(arrays, ptrs);
|
|
Size sz((int)it.size*cn, 1);
|
|
|
|
for( size_t i = 0; i < it.nplanes; i++, ++it )
|
|
func( ptrs[0], 0, 0, 0, ptrs[1], 0, sz, scale );
|
|
}
|
|
}
|
|
|
|
} // namespace
|