From b0e6606b9833791defb6be6d82d8ea9c309e1e32 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Tue, 9 Apr 2013 13:10:54 +0400 Subject: [PATCH] Cleanup core module API * Drop some low level API * Remove outdated overloads * Utilize Input/OutputArray --- modules/core/include/opencv2/core.hpp | 61 ++++--------------- modules/core/src/arithm.cpp | 11 ---- modules/core/src/convert.cpp | 29 ++++++--- modules/core/src/lapack.cpp | 20 ++---- modules/core/src/matmul.cpp | 5 -- modules/core/src/matrix.cpp | 7 ++- modules/core/src/precomp.hpp | 8 +++ modules/core/src/rand.cpp | 5 -- modules/core/test/test_eigen.cpp | 6 +- .../src/org/opencv/test/core/CoreTest.java | 14 +---- modules/java/generator/gen_java.py | 5 ++ modules/python/src2/gen2.py | 14 +++++ modules/video/src/bgfg_gaussmix2.cpp | 8 +-- 13 files changed, 77 insertions(+), 116 deletions(-) diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index d9ecbfc2ae..2e3a98c4ed 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -109,15 +109,6 @@ public: CV_EXPORTS void error( const Exception& exc ); -typedef void (*BinaryFunc)(const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, Size sz, - void*); - -CV_EXPORTS BinaryFunc getConvertFunc(int sdepth, int ddepth); -CV_EXPORTS BinaryFunc getConvertScaleFunc(int sdepth, int ddepth); -CV_EXPORTS BinaryFunc getCopyMaskFunc(size_t esz); - //! swaps two matrices CV_EXPORTS void swap(Mat& a, Mat& b); @@ -153,8 +144,7 @@ CV_EXPORTS_W void convertScaleAbs(InputArray src, OutputArray dst, double alpha = 1, double beta = 0); //! transforms array of numbers using a lookup table: dst(i)=lut(src(i)) -CV_EXPORTS_W void LUT(InputArray src, InputArray lut, OutputArray dst, - int interpolation = 0); +CV_EXPORTS_W void LUT(InputArray src, InputArray lut, OutputArray dst); //! computes sum of array elements CV_EXPORTS_AS(sumElems) Scalar sum(InputArray src); @@ -227,11 +217,11 @@ CV_EXPORTS_W void split(InputArray m, OutputArrayOfArrays mv); CV_EXPORTS void mixChannels(const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs); -CV_EXPORTS void mixChannels(const std::vector& src, std::vector& dst, - const int* fromTo, size_t npairs); //TODO: use arrays +CV_EXPORTS void mixChannels(InputArrayOfArrays src, InputOutputArrayOfArrays dst, + const int* fromTo, size_t npairs); -CV_EXPORTS_W void mixChannels(InputArrayOfArrays src, InputArrayOfArrays dst, - const std::vector& fromTo); //TODO: InputOutputArrayOfArrays +CV_EXPORTS_W void mixChannels(InputArrayOfArrays src, InputOutputArrayOfArrays dst, + const std::vector& fromTo); //! extracts a single channel from src (coi is 0-based index) CV_EXPORTS_W void extractChannel(InputArray src, OutputArray dst, int coi); @@ -291,15 +281,12 @@ CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst); //! computes per-element maximum of two arrays (dst = max(src1, src2)) CV_EXPORTS_W void max(InputArray src1, InputArray src2, OutputArray dst); -//TODO: can we drop these versions? +// the following overloads are needed to avoid conflicts with +// const _Tp& std::min(const _Tp&, const _Tp&, _Compare) //! computes per-element minimum of two arrays (dst = min(src1, src2)) CV_EXPORTS void min(const Mat& src1, const Mat& src2, Mat& dst); -//! computes per-element minimum of array and scalar (dst = min(src1, src2)) -CV_EXPORTS void min(const Mat& src1, double src2, Mat& dst); //! computes per-element maximum of two arrays (dst = max(src1, src2)) CV_EXPORTS void max(const Mat& src1, const Mat& src2, Mat& dst); -//! computes per-element maximum of array and scalar (dst = max(src1, src2)) -CV_EXPORTS void max(const Mat& src1, double src2, Mat& dst); //! computes square root of each matrix element (dst = src**0.5) CV_EXPORTS_W void sqrt(InputArray src, OutputArray dst); @@ -393,17 +380,9 @@ CV_EXPORTS_W int solveCubic(InputArray coeffs, OutputArray roots); //! finds real and complex roots of a polynomial CV_EXPORTS_W double solvePoly(InputArray coeffs, OutputArray roots, int maxIters = 300); -//! finds eigenvalues of a symmetric matrix -CV_EXPORTS bool eigen(InputArray src, OutputArray eigenvalues, int lowindex = -1, - int highindex = -1); - //! finds eigenvalues and eigenvectors of a symmetric matrix -CV_EXPORTS bool eigen(InputArray src, OutputArray eigenvalues, - OutputArray eigenvectors, - int lowindex = -1, int highindex = -1); - -CV_EXPORTS_W bool eigen(InputArray src, bool computeEigenvectors, - OutputArray eigenvalues, OutputArray eigenvectors); +CV_EXPORTS_W bool eigen(InputArray src, OutputArray eigenvalues, + OutputArray eigenvectors = noArray()); enum { @@ -417,10 +396,10 @@ enum //! computes covariation matrix of a set of samples CV_EXPORTS void calcCovarMatrix( const Mat* samples, int nsamples, Mat& covar, Mat& mean, - int flags, int ctype = CV_64F); //TODO: output arrays or drop + int flags, int ctype = CV_64F); //TODO: InputArrayOfArrays //! computes covariation matrix of a set of samples -CV_EXPORTS_W void calcCovarMatrix( InputArray samples, OutputArray covar, //TODO: InputArrayOfArrays +CV_EXPORTS_W void calcCovarMatrix( InputArray samples, OutputArray covar, OutputArray mean, int flags, int ctype = CV_64F); CV_EXPORTS_W void PCACompute(InputArray data, InputOutputArray mean, @@ -445,9 +424,6 @@ CV_EXPORTS_W void SVBackSubst( InputArray w, InputArray u, InputArray vt, //! computes Mahalanobis distance between two vectors: sqrt((v1-v2)'*icovar*(v1-v2)), where icovar is the inverse covariation matrix CV_EXPORTS_W double Mahalanobis(InputArray v1, InputArray v2, InputArray icovar); -//! a synonym for Mahalanobis -CV_EXPORTS double Mahalonobis(InputArray v1, InputArray v2, InputArray icovar); //TODO: check if we can drop it or move to legacy - //! performs forward or inverse 1D or 2D Discrete Fourier Transformation CV_EXPORTS_W void dft(InputArray src, OutputArray dst, int flags = 0, int nonzeroRows = 0); @@ -492,9 +468,7 @@ CV_EXPORTS_W void randu(InputOutputArray dst, InputArray low, InputArray high); CV_EXPORTS_W void randn(InputOutputArray dst, InputArray mean, InputArray stddev); //! shuffles the input array elements -CV_EXPORTS void randShuffle(InputOutputArray dst, double iterFactor = 1., RNG* rng = 0); - -CV_EXPORTS_AS(randShuffle) void randShuffle_(InputOutputArray dst, double iterFactor = 1.); +CV_EXPORTS_W void randShuffle(InputOutputArray dst, double iterFactor = 1., RNG* rng = 0); enum { FILLED = -1, LINE_4 = 4, @@ -601,17 +575,6 @@ CV_EXPORTS_W Size getTextSize(const String& text, int fontFace, double fontScale, int thickness, CV_OUT int* baseLine); -typedef void (*ConvertData)(const void* from, void* to, int cn); -typedef void (*ConvertScaleData)(const void* from, void* to, int cn, double alpha, double beta); - -//! returns the function for converting pixels from one data type to another -CV_EXPORTS ConvertData getConvertElem(int fromType, int toType); - -//! returns the function for converting pixels from one data type to another with the optional scaling -CV_EXPORTS ConvertScaleData getConvertScaleElem(int fromType, int toType); - - - /*! Principal Component Analysis diff --git a/modules/core/src/arithm.cpp b/modules/core/src/arithm.cpp index b76dd7fd40..98b67aabf1 100644 --- a/modules/core/src/arithm.cpp +++ b/modules/core/src/arithm.cpp @@ -1197,17 +1197,6 @@ void cv::min(const Mat& src1, const Mat& src2, Mat& dst) binary_op(src1, src2, _dst, noArray(), minTab, false ); } -void cv::max(const Mat& src1, double src2, Mat& dst) -{ - OutputArray _dst(dst); - binary_op(src1, src2, _dst, noArray(), maxTab, false ); -} - -void cv::min(const Mat& src1, double src2, Mat& dst) -{ - OutputArray _dst(dst); - binary_op(src1, src2, _dst, noArray(), minTab, false ); -} /****************************************************************************************\ * add/subtract * diff --git a/modules/core/src/convert.cpp b/modules/core/src/convert.cpp index d8ef88151a..f5d6e9998f 100644 --- a/modules/core/src/convert.cpp +++ b/modules/core/src/convert.cpp @@ -505,14 +505,30 @@ void cv::mixChannels( const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, cons } -void cv::mixChannels(const std::vector& src, std::vector& dst, +void cv::mixChannels(InputArrayOfArrays src, InputOutputArrayOfArrays dst, const int* fromTo, size_t npairs) { - mixChannels(!src.empty() ? &src[0] : 0, src.size(), - !dst.empty() ? &dst[0] : 0, dst.size(), fromTo, npairs); + if(npairs == 0) + return; + bool src_is_mat = src.kind() != _InputArray::STD_VECTOR_MAT && + src.kind() != _InputArray::STD_VECTOR_VECTOR; + bool dst_is_mat = dst.kind() != _InputArray::STD_VECTOR_MAT && + dst.kind() != _InputArray::STD_VECTOR_VECTOR; + int i; + int nsrc = src_is_mat ? 1 : (int)src.total(); + int ndst = dst_is_mat ? 1 : (int)dst.total(); + + CV_Assert(nsrc > 0 && ndst > 0); + cv::AutoBuffer _buf(nsrc + ndst); + Mat* buf = _buf; + for( i = 0; i < nsrc; i++ ) + buf[i] = src.getMat(src_is_mat ? -1 : i); + for( i = 0; i < ndst; i++ ) + buf[nsrc + i] = dst.getMat(dst_is_mat ? -1 : i); + mixChannels(&buf[0], nsrc, &buf[nsrc], ndst, fromTo, npairs); } -void cv::mixChannels(InputArrayOfArrays src, InputArrayOfArrays dst, +void cv::mixChannels(InputArrayOfArrays src, InputOutputArrayOfArrays dst, const std::vector& fromTo) { if(fromTo.empty()) @@ -1027,7 +1043,7 @@ BinaryFunc getConvertFunc(int sdepth, int ddepth) return cvtTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)]; } -BinaryFunc getConvertScaleFunc(int sdepth, int ddepth) +static BinaryFunc getConvertScaleFunc(int sdepth, int ddepth) { return cvtScaleTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)]; } @@ -1173,10 +1189,9 @@ static LUTFunc lutTab[] = } -void cv::LUT( InputArray _src, InputArray _lut, OutputArray _dst, int interpolation ) +void cv::LUT( InputArray _src, InputArray _lut, OutputArray _dst ) { Mat src = _src.getMat(), lut = _lut.getMat(); - CV_Assert( interpolation == 0 ); int cn = src.channels(); int lutcn = lut.channels(); diff --git a/modules/core/src/lapack.cpp b/modules/core/src/lapack.cpp index 35b08582c4..9821de8879 100644 --- a/modules/core/src/lapack.cpp +++ b/modules/core/src/lapack.cpp @@ -1464,7 +1464,7 @@ bool cv::solve( InputArray _src, InputArray _src2arg, OutputArray _dst, int meth /////////////////// finding eigenvalues and eigenvectors of a symmetric matrix /////////////// -bool cv::eigen( InputArray _src, bool computeEvects, OutputArray _evals, OutputArray _evects ) +bool cv::eigen( InputArray _src, OutputArray _evals, OutputArray _evects ) { Mat src = _src.getMat(); int type = src.type(); @@ -1474,7 +1474,7 @@ bool cv::eigen( InputArray _src, bool computeEvects, OutputArray _evals, OutputA CV_Assert (type == CV_32F || type == CV_64F); Mat v; - if( computeEvects ) + if( _evects.needed() ) { _evects.create(n, n, type); v = _evects.getMat(); @@ -1494,16 +1494,6 @@ bool cv::eigen( InputArray _src, bool computeEvects, OutputArray _evals, OutputA return ok; } -bool cv::eigen( InputArray src, OutputArray evals, int, int ) -{ - return eigen(src, false, evals, noArray()); -} - -bool cv::eigen( InputArray src, OutputArray evals, OutputArray evects, int, int) -{ - return eigen(src, true, evals, evects); -} - namespace cv { @@ -1705,13 +1695,13 @@ cvSolve( const CvArr* Aarr, const CvArr* barr, CvArr* xarr, int method ) CV_IMPL void cvEigenVV( CvArr* srcarr, CvArr* evectsarr, CvArr* evalsarr, double, - int lowindex, int highindex) + int, int ) { cv::Mat src = cv::cvarrToMat(srcarr), evals0 = cv::cvarrToMat(evalsarr), evals = evals0; if( evectsarr ) { cv::Mat evects0 = cv::cvarrToMat(evectsarr), evects = evects0; - eigen(src, evals, evects, lowindex, highindex); + eigen(src, evals, evects); if( evects0.data != evects.data ) { uchar* p = evects0.data; @@ -1720,7 +1710,7 @@ cvEigenVV( CvArr* srcarr, CvArr* evectsarr, CvArr* evalsarr, double, } } else - eigen(src, evals, lowindex, highindex); + eigen(src, evals); if( evals0.data != evals.data ) { uchar* p = evals0.data; diff --git a/modules/core/src/matmul.cpp b/modules/core/src/matmul.cpp index 6b075a5b8e..b1fcdc9b06 100644 --- a/modules/core/src/matmul.cpp +++ b/modules/core/src/matmul.cpp @@ -2308,11 +2308,6 @@ double cv::Mahalanobis( InputArray _v1, InputArray _v2, InputArray _icovar ) return std::sqrt(result); } -double cv::Mahalonobis( InputArray _v1, InputArray _v2, InputArray _icovar ) -{ - return Mahalanobis(_v1, _v2, _icovar); -} - /****************************************************************************************\ * MulTransposed * \****************************************************************************************/ diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index c48fbc6a66..93dd50aef6 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -3448,7 +3448,10 @@ convertScaleData_(const void* _from, void* _to, int cn, double alpha, double bet to[i] = saturate_cast(from[i]*alpha + beta); } -ConvertData getConvertElem(int fromType, int toType) +typedef void (*ConvertData)(const void* from, void* to, int cn); +typedef void (*ConvertScaleData)(const void* from, void* to, int cn, double alpha, double beta); + +static ConvertData getConvertElem(int fromType, int toType) { static ConvertData tab[][8] = {{ convertData_, convertData_, @@ -3493,7 +3496,7 @@ ConvertData getConvertElem(int fromType, int toType) return func; } -ConvertScaleData getConvertScaleElem(int fromType, int toType) +static ConvertScaleData getConvertScaleElem(int fromType, int toType) { static ConvertScaleData tab[][8] = {{ convertScaleData_, convertScaleData_, diff --git a/modules/core/src/precomp.hpp b/modules/core/src/precomp.hpp index 2f3ece1bf1..8b39d7a157 100644 --- a/modules/core/src/precomp.hpp +++ b/modules/core/src/precomp.hpp @@ -69,6 +69,14 @@ namespace cv { +typedef void (*BinaryFunc)(const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, Size sz, + void*); + +BinaryFunc getConvertFunc(int sdepth, int ddepth); +BinaryFunc getCopyMaskFunc(size_t esz); + /* default memory block for sparse array elements */ #define CV_SPARSE_MAT_BLOCK (1<<12) diff --git a/modules/core/src/rand.cpp b/modules/core/src/rand.cpp index 3ba7fd684d..74eba60066 100644 --- a/modules/core/src/rand.cpp +++ b/modules/core/src/rand.cpp @@ -861,11 +861,6 @@ void cv::randShuffle( InputOutputArray _dst, double iterFactor, RNG* _rng ) func( dst, rng, iterFactor ); } -void cv::randShuffle_( InputOutputArray _dst, double iterFactor ) -{ - randShuffle(_dst, iterFactor); -} - CV_IMPL void cvRandArr( CvRNG* _rng, CvArr* arr, int disttype, CvScalar param1, CvScalar param2 ) { diff --git a/modules/core/test/test_eigen.cpp b/modules/core/test/test_eigen.cpp index 046573d26d..21859e59b8 100644 --- a/modules/core/test/test_eigen.cpp +++ b/modules/core/test/test_eigen.cpp @@ -294,7 +294,7 @@ bool Core_EigenTest::test_pairs(const cv::Mat& src) cv::Mat eigen_values, eigen_vectors; - cv::eigen(src, true, eigen_values, eigen_vectors); + cv::eigen(src, eigen_values, eigen_vectors); if (!check_pair_count(src, eigen_values, eigen_vectors)) return false; @@ -362,8 +362,8 @@ bool Core_EigenTest::test_values(const cv::Mat& src) if (!test_pairs(src)) return false; - cv::eigen(src, true, eigen_values_1, eigen_vectors); - cv::eigen(src, false, eigen_values_2, eigen_vectors); + cv::eigen(src, eigen_values_1, eigen_vectors); + cv::eigen(src, eigen_values_2); if (!check_pair_count(src, eigen_values_2)) return false; diff --git a/modules/java/android_test/src/org/opencv/test/core/CoreTest.java b/modules/java/android_test/src/org/opencv/test/core/CoreTest.java index a6023c7cc6..0916cae0e3 100644 --- a/modules/java/android_test/src/org/opencv/test/core/CoreTest.java +++ b/modules/java/android_test/src/org/opencv/test/core/CoreTest.java @@ -460,7 +460,7 @@ public class CoreTest extends OpenCVTestCase { Mat eigenVals = new Mat(); Mat eigenVecs = new Mat(); - Core.eigen(src, true, eigenVals, eigenVecs); + Core.eigen(src, eigenVals, eigenVecs); Mat expectedEigenVals = new Mat(3, 1, CvType.CV_32FC1) { { @@ -1113,18 +1113,6 @@ public class CoreTest extends OpenCVTestCase { assertMatEqual(gray255, dst); } - public void testLUTMatMatMatInt() { - Mat lut = new Mat(1, 256, CvType.CV_8UC1); - // TODO: ban this overload - try - { - Core.LUT(grayRnd, lut, dst, 1); - fail("Last parameter for LUT was not supported"); - } catch (CvException e) { - // expected - } - } - public void testMagnitude() { Mat x = new Mat(1, 4, CvType.CV_32F); Mat y = new Mat(1, 4, CvType.CV_32F); diff --git a/modules/java/generator/gen_java.py b/modules/java/generator/gen_java.py index d387e9aa7d..e48b682437 100755 --- a/modules/java/generator/gen_java.py +++ b/modules/java/generator/gen_java.py @@ -996,6 +996,11 @@ extern "C" { return for a in fi.args: if a.ctype not in type_dict: + if not a.defval and a.ctype.endswith("*"): + a.defval = 0 + if a.defval: + a.ctype = '' + continue msg = "// Unknown type '%s' (%s), skipping the function\n\n" % (a.ctype, a.out or "I") self.skipped_func_list.append(c_decl + "\n" + msg) j_code.write( " "*4 + msg ) diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index 680be2fd35..5f4f60fce6 100755 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -3,6 +3,8 @@ import hdr_parser, sys, re, os, cStringIO from string import Template +ignored_arg_types = ["RNG*"] + gen_template_check_self = Template(""" if(!PyObject_TypeCheck(self, &pyopencv_${name}_Type)) return failmsgp("Incorrect type of self (must be '${name}' or its derivative)"); $cname* _self_ = ${amp}((pyopencv_${name}_t*)self)->v; @@ -426,6 +428,8 @@ class FuncVariant(object): argno += 1 if a.name in self.array_counters: continue + if a.tp in ignored_arg_types: + continue if a.returnarg: outlist.append((a.name, argno)) if (not a.inputarg) and a.isbig(): @@ -586,6 +590,16 @@ class FuncInfo(object): # form the function/method call, # for the list of type mappings for a in v.args: + if a.tp in ignored_arg_types: + defval = a.defval + if not defval and a.tp.endswith("*"): + defval = 0 + assert defval + if not code_fcall.endswith("("): + code_fcall += ", " + code_fcall += defval + all_cargs.append([[None, ""], ""]) + continue tp1 = tp = a.tp amp = "" defval0 = "" diff --git a/modules/video/src/bgfg_gaussmix2.cpp b/modules/video/src/bgfg_gaussmix2.cpp index 68344d62fd..1337a1f5be 100644 --- a/modules/video/src/bgfg_gaussmix2.cpp +++ b/modules/video/src/bgfg_gaussmix2.cpp @@ -486,8 +486,6 @@ public: tau = _tau; detectShadows = _detectShadows; shadowVal = _shadowVal; - - cvtfunc = src->depth() != CV_32F ? getConvertFunc(src->depth(), CV_32F) : 0; } void operator()(const Range& range) const @@ -501,8 +499,8 @@ public: for( int y = y0; y < y1; y++ ) { const float* data = buf; - if( cvtfunc ) - cvtfunc( src->ptr(y), src->step, 0, 0, (uchar*)data, 0, Size(ncols*nchannels, 1), 0); + if( src->depth() != CV_32F ) + src->row(y).convertTo(Mat(1, ncols, CV_32FC(nchannels), (void*)data), CV_32F); else data = src->ptr(y); @@ -685,8 +683,6 @@ public: bool detectShadows; uchar shadowVal; - - BinaryFunc cvtfunc; }; void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask, double learningRate)