From 0e14fef96e01a9d343b7bdbd0a5ae93f221e6422 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Sat, 18 Jun 2011 10:56:49 +0000 Subject: [PATCH] fixed warnings in gbt; added insertChannel() and extractChannel(); made the code "rand{u|n}(arr, , )" work properly. --- modules/core/include/opencv2/core/core.hpp | 8 ++++- modules/core/src/convert.cpp | 21 ++++++++++++- modules/core/src/rand.cpp | 34 +++++++++++++++++----- modules/ml/src/gbt.cpp | 10 +++---- 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp index 5b8ffa5663..2c14772fa7 100644 --- a/modules/core/include/opencv2/core/core.hpp +++ b/modules/core/include/opencv2/core/core.hpp @@ -1961,7 +1961,7 @@ CV_EXPORTS Mat cvarrToMat(const CvArr* arr, bool copyData=false, CV_EXPORTS void extractImageCOI(const CvArr* arr, OutputArray coiimg, int coi=-1); //! inserts single-channel cv::Mat into a multi-channel CvMat or IplImage CV_EXPORTS void insertImageCOI(InputArray coiimg, CvArr* arr, int coi=-1); - + //! adds one matrix to another (dst = src1 + src2) CV_EXPORTS_W void add(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1); @@ -2039,6 +2039,12 @@ CV_EXPORTS void mixChannels(const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts CV_EXPORTS void mixChannels(const vector& src, vector& dst, const int* fromTo, size_t npairs); +//! extracts a single channel from src (coi is 0-based index) +CV_EXPORTS_W void extractChannel(InputArray src, OutputArray dst, int coi); + +//! inserts a single channel to dst (coi is 0-based index) +CV_EXPORTS_W void insertChannel(InputArray src, InputOutputArray dst, int coi); + //! reverses the order of the rows, columns or both in a matrix CV_EXPORTS_W void flip(InputArray src, OutputArray dst, int flipCode); diff --git a/modules/core/src/convert.cpp b/modules/core/src/convert.cpp index 14e2538088..73b13ad717 100644 --- a/modules/core/src/convert.cpp +++ b/modules/core/src/convert.cpp @@ -501,7 +501,26 @@ void cv::mixChannels(const vector& src, vector& dst, { mixChannels(!src.empty() ? &src[0] : 0, src.size(), !dst.empty() ? &dst[0] : 0, dst.size(), fromTo, npairs); -} +} + +void cv::extractChannel(InputArray _src, OutputArray _dst, int coi) +{ + Mat src = _src.getMat(); + CV_Assert( 0 <= coi && coi < src.channels() ); + _dst.create(src.dims, &src.size[0], src.depth()); + Mat dst = _dst.getMat(); + int ch[] = { coi, 0 }; + mixChannels(&src, 1, &dst, 1, ch, 1); +} + +void cv::insertChannel(InputArray _src, InputOutputArray _dst, int coi) +{ + Mat src = _src.getMat(), dst = _dst.getMat(); + CV_Assert( src.size == dst.size && src.depth() == dst.depth() ); + CV_Assert( 0 <= coi && coi < dst.channels() && src.channels() == 1 ); + int ch[] = { 0, coi }; + mixChannels(&src, 1, &dst, 1, ch, 1); +} /****************************************************************************************\ * convertScale[Abs] * diff --git a/modules/core/src/rand.cpp b/modules/core/src/rand.cpp index 7ee7f69a48..fb68d38847 100644 --- a/modules/core/src/rand.cpp +++ b/modules/core/src/rand.cpp @@ -453,11 +453,11 @@ void RNG::fill( InputOutputArray _mat, int disttype, InputArray _param1arg, Inpu RandnScaleFunc scaleFunc = 0; CV_Assert(_param1.channels() == 1 && (_param1.rows == 1 || _param1.cols == 1) && - (_param1.rows + _param1.cols - 1 == cn || + (_param1.rows + _param1.cols - 1 == cn || _param1.rows + _param1.cols - 1 == 1 || (_param1.size() == Size(1, 4) && _param1.type() == CV_64F && cn <= 4))); CV_Assert( _param2.channels() == 1 && (((_param2.rows == 1 || _param2.cols == 1) && - (_param2.rows + _param2.cols - 1 == cn || + (_param2.rows + _param2.cols - 1 == cn || _param2.rows + _param2.cols - 1 == 1 || (_param1.size() == Size(1, 4) && _param1.type() == CV_64F && cn <= 4))) || (_param2.rows == cn && _param2.cols == cn && disttype == NORMAL))); @@ -468,26 +468,34 @@ void RNG::fill( InputOutputArray _mat, int disttype, InputArray _param1arg, Inpu uchar* mean = 0; uchar* stddev = 0; bool stdmtx = false; + int n1 = (int)_param1.total(); + int n2 = (int)_param2.total(); if( disttype == UNIFORM ) { - _parambuf.allocate(cn*8); + _parambuf.allocate(cn*8 + n1 + n2); double* parambuf = _parambuf; - const double* p1 = (const double*)_param1.data; - const double* p2 = (const double*)_param2.data; + double* p1 = (double*)_param1.data; + double* p2 = (double*)_param2.data; - if( !_param1.isContinuous() || _param1.type() != CV_64F ) + if( !_param1.isContinuous() || _param1.type() != CV_64F || n1 != cn ) { Mat tmp(_param1.size(), CV_64F, parambuf); _param1.convertTo(tmp, CV_64F); p1 = parambuf; + if( n1 < cn ) + for( j = n1; j < cn; j++ ) + p1[j] = p1[j-n1]; } - if( !_param2.isContinuous() || _param2.type() != CV_64F ) + if( !_param2.isContinuous() || _param2.type() != CV_64F || n2 != cn ) { Mat tmp(_param2.size(), CV_64F, parambuf + cn); _param2.convertTo(tmp, CV_64F); p2 = parambuf + cn; + if( n2 < cn ) + for( j = n2; j < cn; j++ ) + p2[j] = p2[j-n2]; } if( depth <= CV_32S ) @@ -559,10 +567,12 @@ void RNG::fill( InputOutputArray _mat, int disttype, InputArray _param1arg, Inpu } else if( disttype == CV_RAND_NORMAL ) { - _parambuf.allocate(_param1.total() + _param2.total()); + _parambuf.allocate(MAX(n1, cn) + MAX(n2, cn)); double* parambuf = _parambuf; int ptype = depth == CV_64F ? CV_64F : CV_32F; + int esz = (int)CV_ELEM_SIZE(ptype); + if( _param1.isContinuous() && _param1.type() == ptype ) mean = _param1.data; else @@ -572,6 +582,10 @@ void RNG::fill( InputOutputArray _mat, int disttype, InputArray _param1arg, Inpu mean = (uchar*)parambuf; } + if( n1 < cn ) + for( j = n1*esz; j < cn*esz; j++ ) + mean[j] = mean[j - n1*esz]; + if( _param2.isContinuous() && _param2.type() == ptype ) stddev = _param2.data; else @@ -581,6 +595,10 @@ void RNG::fill( InputOutputArray _mat, int disttype, InputArray _param1arg, Inpu stddev = (uchar*)(parambuf + cn); } + if( n1 < cn ) + for( j = n1*esz; j < cn*esz; j++ ) + stddev[j] = stddev[j - n1*esz]; + stdmtx = _param2.rows == cn && _param2.cols == cn; scaleFunc = randnScaleTab[depth]; CV_Assert( scaleFunc != 0 ); diff --git a/modules/ml/src/gbt.cpp b/modules/ml/src/gbt.cpp index 60b1469b7e..002f240b38 100644 --- a/modules/ml/src/gbt.cpp +++ b/modules/ml/src/gbt.cpp @@ -919,17 +919,17 @@ public: Tree_predictor() : weak(0), sum(0), k(0), sample(0), missing(0), shrinkage(1.0f) {} Tree_predictor(pCvSeq* _weak, const int _k, const float _shrinkage, const CvMat* _sample, const CvMat* _missing, float* _sum ) : - weak(_weak), k(_k), sample(_sample), - missing(_missing), sum(_sum), shrinkage(_shrinkage) + weak(_weak), sum(_sum), k(_k), sample(_sample), + missing(_missing), shrinkage(_shrinkage) {} Tree_predictor( const Tree_predictor& p, cv::Split ) : - weak(p.weak), k(p.k), sample(p.sample), - missing(p.missing), sum(p.sum), shrinkage(p.shrinkage) + weak(p.weak), sum(p.sum), k(p.k), sample(p.sample), + missing(p.missing), shrinkage(p.shrinkage) {} Tree_predictor& operator=( const Tree_predictor& ) - {} + { return *this; } virtual void operator()(const cv::BlockedRange& range) const {