4 #include "opencv2/imgproc/types_c.h"
11 const char* getTypeName( int type )
13 static const char* type_names[] = { "8u", "8s", "16u", "16s", "32s", "32f", "64f", "ptr" };
14 return type_names[CV_MAT_DEPTH(type)];
17 int typeByName( const char* name )
20 for( i = 0; i < CV_DEPTH_MAX; i++ )
21 if( strcmp(name, getTypeName(i)) == 0 )
26 string vec2str( const string& sep, const int* v, size_t nelems )
30 for( size_t i = 0; i < nelems; i++ )
32 sprintf(buf, "%d", v[i]);
33 result += string(buf);
41 Size randomSize(RNG& rng, double maxSizeLog)
43 double width_log = rng.uniform(0., maxSizeLog);
44 double height_log = rng.uniform(0., maxSizeLog - width_log);
45 if( (unsigned)rng % 2 != 0 )
46 std::swap(width_log, height_log);
48 sz.width = cvRound(exp(width_log));
49 sz.height = cvRound(exp(height_log));
53 void randomSize(RNG& rng, int minDims, int maxDims, double maxSizeLog, vector<int>& sz)
55 int i, dims = rng.uniform(minDims, maxDims+1);
57 for( i = 0; i < dims; i++ )
59 double v = rng.uniform(0., maxSizeLog);
61 sz[i] = cvRound(exp(v));
63 for( i = 0; i < dims; i++ )
65 int j = rng.uniform(0, dims);
66 int k = rng.uniform(0, dims);
67 std::swap(sz[j], sz[k]);
71 int randomType(RNG& rng, int typeMask, int minChannels, int maxChannels)
73 int channels = rng.uniform(minChannels, maxChannels+1);
75 CV_Assert((typeMask & _OutputArray::DEPTH_MASK_ALL) != 0);
78 depth = rng.uniform(CV_8U, CV_64F+1);
79 if( ((1 << depth) & typeMask) != 0 )
82 return CV_MAKETYPE(depth, channels);
85 double getMinVal(int depth)
87 depth = CV_MAT_DEPTH(depth);
88 double val = depth == CV_8U ? 0 : depth == CV_8S ? SCHAR_MIN : depth == CV_16U ? 0 :
89 depth == CV_16S ? SHRT_MIN : depth == CV_32S ? INT_MIN :
90 depth == CV_32F ? -FLT_MAX : depth == CV_64F ? -DBL_MAX : -1;
95 double getMaxVal(int depth)
97 depth = CV_MAT_DEPTH(depth);
98 double val = depth == CV_8U ? UCHAR_MAX : depth == CV_8S ? SCHAR_MAX : depth == CV_16U ? USHRT_MAX :
99 depth == CV_16S ? SHRT_MAX : depth == CV_32S ? INT_MAX :
100 depth == CV_32F ? FLT_MAX : depth == CV_64F ? DBL_MAX : -1;
101 CV_Assert(val != -1);
105 Mat randomMat(RNG& rng, Size size, int type, double minVal, double maxVal, bool useRoi)
110 size0.width += std::max(rng.uniform(0, 10) - 5, 0);
111 size0.height += std::max(rng.uniform(0, 10) - 5, 0);
116 rng.fill(m, RNG::UNIFORM, minVal, maxVal);
119 return m(Rect((size0.width-size.width)/2, (size0.height-size.height)/2, size.width, size.height));
122 Mat randomMat(RNG& rng, const vector<int>& size, int type, double minVal, double maxVal, bool useRoi)
124 int i, dims = (int)size.size();
125 vector<int> size0(dims);
126 vector<Range> r(dims);
128 for( i = 0; i < dims; i++ )
134 size0[i] += std::max(rng.uniform(0, 5) - 2, 0);
135 r[i] = Range((size0[i] - size[i])/2, (size0[i] - size[i])/2 + size[i]);
137 eqsize = eqsize && size[i] == size0[i];
140 Mat m(dims, &size0[0], type);
142 rng.fill(m, RNG::UNIFORM, minVal, maxVal);
148 void add(const Mat& _a, double alpha, const Mat& _b, double beta,
149 Scalar gamma, Mat& c, int ctype, bool calcAbs)
152 if( a.empty() || alpha == 0 )
154 // both alpha and beta can be 0, but at least one of a and b must be non-empty array,
155 // otherwise we do not know the size of the output (and may be type of the output, when ctype<0)
156 CV_Assert( !a.empty() || !b.empty() );
165 if( b.empty() || beta == 0 )
171 CV_Assert(a.size == b.size);
175 ctype = CV_MAKETYPE(CV_MAT_DEPTH(ctype), a.channels());
176 c.create(a.dims, &a.size[0], ctype);
177 const Mat *arrays[] = {&a, &b, &c, 0};
178 Mat planes[3], buf[3];
180 NAryMatIterator it(arrays, planes);
181 size_t i, nplanes = it.nplanes;
183 int total = (int)planes[0].total(), maxsize = std::min(12*12*std::max(12/cn, 1), total);
185 CV_Assert(planes[0].rows == 1);
186 buf[0].create(1, maxsize, CV_64FC(cn));
188 buf[1].create(1, maxsize, CV_64FC(cn));
189 buf[2].create(1, maxsize, CV_64FC(cn));
190 scalarToRawData(gamma, buf[2].ptr(), CV_64FC(cn), (int)(maxsize*cn));
192 for( i = 0; i < nplanes; i++, ++it)
194 for( int j = 0; j < total; j += maxsize )
196 int j2 = std::min(j + maxsize, total);
197 Mat apart0 = planes[0].colRange(j, j2);
198 Mat cpart0 = planes[2].colRange(j, j2);
199 Mat apart = buf[0].colRange(0, j2 - j);
201 apart0.convertTo(apart, apart.type(), alpha);
202 size_t k, n = (j2 - j)*cn;
203 double* aptr = apart.ptr<double>();
204 const double* gptr = buf[2].ptr<double>();
208 for( k = 0; k < n; k++ )
213 Mat bpart0 = planes[1].colRange((int)j, (int)j2);
214 Mat bpart = buf[1].colRange(0, (int)(j2 - j));
215 bpart0.convertTo(bpart, bpart.type(), beta);
216 const double* bptr = bpart.ptr<double>();
218 for( k = 0; k < n; k++ )
219 aptr[k] += bptr[k] + gptr[k];
222 for( k = 0; k < n; k++ )
223 aptr[k] = fabs(aptr[k]);
224 apart.convertTo(cpart0, cpart0.type(), 1, 0);
230 template<typename _Tp1, typename _Tp2> inline void
231 convert_(const _Tp1* src, _Tp2* dst, size_t total, double alpha, double beta)
234 if( alpha == 1 && beta == 0 )
235 for( i = 0; i < total; i++ )
236 dst[i] = saturate_cast<_Tp2>(src[i]);
238 for( i = 0; i < total; i++ )
239 dst[i] = saturate_cast<_Tp2>(src[i]*alpha);
241 for( i = 0; i < total; i++ )
242 dst[i] = saturate_cast<_Tp2>(src[i]*alpha + beta);
245 template<typename _Tp> inline void
246 convertTo(const _Tp* src, void* dst, int dtype, size_t total, double alpha, double beta)
248 switch( CV_MAT_DEPTH(dtype) )
251 convert_(src, (uchar*)dst, total, alpha, beta);
254 convert_(src, (schar*)dst, total, alpha, beta);
257 convert_(src, (ushort*)dst, total, alpha, beta);
260 convert_(src, (short*)dst, total, alpha, beta);
263 convert_(src, (int*)dst, total, alpha, beta);
266 convert_(src, (float*)dst, total, alpha, beta);
269 convert_(src, (double*)dst, total, alpha, beta);
276 void convert(const Mat& src, cv::OutputArray _dst, int dtype, double alpha, double beta)
278 if (dtype < 0) dtype = _dst.depth();
280 dtype = CV_MAKETYPE(CV_MAT_DEPTH(dtype), src.channels());
281 _dst.create(src.dims, &src.size[0], dtype);
282 Mat dst = _dst.getMat();
285 set( dst, Scalar::all(beta) );
288 if( dtype == src.type() && alpha == 1 && beta == 0 )
294 const Mat *arrays[]={&src, &dst, 0};
297 NAryMatIterator it(arrays, planes);
298 size_t total = planes[0].total()*planes[0].channels();
299 size_t i, nplanes = it.nplanes;
301 for( i = 0; i < nplanes; i++, ++it)
303 const uchar* sptr = planes[0].ptr();
304 uchar* dptr = planes[1].ptr();
306 switch( src.depth() )
309 convertTo((const uchar*)sptr, dptr, dtype, total, alpha, beta);
312 convertTo((const schar*)sptr, dptr, dtype, total, alpha, beta);
315 convertTo((const ushort*)sptr, dptr, dtype, total, alpha, beta);
318 convertTo((const short*)sptr, dptr, dtype, total, alpha, beta);
321 convertTo((const int*)sptr, dptr, dtype, total, alpha, beta);
324 convertTo((const float*)sptr, dptr, dtype, total, alpha, beta);
327 convertTo((const double*)sptr, dptr, dtype, total, alpha, beta);
334 void copy(const Mat& src, Mat& dst, const Mat& mask, bool invertMask)
336 dst.create(src.dims, &src.size[0], src.type());
340 const Mat* arrays[] = {&src, &dst, 0};
342 NAryMatIterator it(arrays, planes);
343 size_t i, nplanes = it.nplanes;
344 size_t planeSize = planes[0].total()*src.elemSize();
346 for( i = 0; i < nplanes; i++, ++it )
347 memcpy(planes[1].ptr(), planes[0].ptr(), planeSize);
352 int mcn = mask.channels();
353 CV_Assert( src.size == mask.size && mask.depth() == CV_8U
354 && (mcn == 1 || mcn == src.channels()) );
356 const Mat *arrays[]={&src, &dst, &mask, 0};
359 NAryMatIterator it(arrays, planes);
360 size_t j, k, elemSize = src.elemSize(), maskElemSize = mask.elemSize(), total = planes[0].total();
361 size_t i, nplanes = it.nplanes;
362 size_t elemSize1 = src.elemSize1();
364 for( i = 0; i < nplanes; i++, ++it)
366 const uchar* sptr = planes[0].ptr();
367 uchar* dptr = planes[1].ptr();
368 const uchar* mptr = planes[2].ptr();
369 for( j = 0; j < total; j++, sptr += elemSize, dptr += elemSize, mptr += maskElemSize )
373 if( (mptr[0] != 0) ^ invertMask )
374 for( k = 0; k < elemSize; k++ )
379 for( int c = 0; c < mcn; c++ )
380 if( (mptr[c] != 0) ^ invertMask )
381 for( k = 0; k < elemSize1; k++ )
382 dptr[k + c * elemSize1] = sptr[k + c * elemSize1];
389 void set(Mat& dst, const Scalar& gamma, const Mat& mask)
392 scalarToRawData(gamma, &buf, dst.type(), dst.channels());
393 const uchar* gptr = (const uchar*)&buf[0];
397 const Mat* arrays[] = {&dst, 0};
399 NAryMatIterator it(arrays, &plane);
400 size_t i, nplanes = it.nplanes;
401 size_t j, k, elemSize = dst.elemSize(), planeSize = plane.total()*elemSize;
403 for( k = 1; k < elemSize; k++ )
404 if( gptr[k] != gptr[0] )
406 bool uniform = k >= elemSize;
408 for( i = 0; i < nplanes; i++, ++it )
410 uchar* dptr = plane.ptr();
412 memset( dptr, gptr[0], planeSize );
415 for( j = 0; j < planeSize; j += elemSize, dptr += elemSize )
416 for( k = 0; k < elemSize; k++ )
420 memcpy(dptr, dst.ptr(), planeSize);
425 int cn = dst.channels(), mcn = mask.channels();
426 CV_Assert( dst.size == mask.size && (mcn == 1 || mcn == cn) );
428 const Mat *arrays[]={&dst, &mask, 0};
431 NAryMatIterator it(arrays, planes);
432 size_t j, k, elemSize = dst.elemSize(), maskElemSize = mask.elemSize(), total = planes[0].total();
433 size_t i, nplanes = it.nplanes;
434 size_t elemSize1 = dst.elemSize1();
436 for( i = 0; i < nplanes; i++, ++it)
438 uchar* dptr = planes[0].ptr();
439 const uchar* mptr = planes[1].ptr();
441 for( j = 0; j < total; j++, dptr += elemSize, mptr += maskElemSize )
446 for( k = 0; k < elemSize; k++ )
451 for( int c = 0; c < mcn; c++ )
453 for( k = 0; k < elemSize1; k++ )
454 dptr[k + c * elemSize1] = gptr[k + c * elemSize1];
461 void insert(const Mat& src, Mat& dst, int coi)
463 CV_Assert( dst.size == src.size && src.depth() == dst.depth() &&
464 0 <= coi && coi < dst.channels() );
466 const Mat* arrays[] = {&src, &dst, 0};
468 NAryMatIterator it(arrays, planes);
469 size_t i, nplanes = it.nplanes;
470 size_t j, k, size0 = src.elemSize(), size1 = dst.elemSize(), total = planes[0].total();
472 for( i = 0; i < nplanes; i++, ++it )
474 const uchar* sptr = planes[0].ptr();
475 uchar* dptr = planes[1].ptr() + coi*size0;
477 for( j = 0; j < total; j++, sptr += size0, dptr += size1 )
479 for( k = 0; k < size0; k++ )
486 void extract(const Mat& src, Mat& dst, int coi)
488 dst.create( src.dims, &src.size[0], src.depth() );
489 CV_Assert( 0 <= coi && coi < src.channels() );
491 const Mat* arrays[] = {&src, &dst, 0};
493 NAryMatIterator it(arrays, planes);
494 size_t i, nplanes = it.nplanes;
495 size_t j, k, size0 = src.elemSize(), size1 = dst.elemSize(), total = planes[0].total();
497 for( i = 0; i < nplanes; i++, ++it )
499 const uchar* sptr = planes[0].ptr() + coi*size1;
500 uchar* dptr = planes[1].ptr();
502 for( j = 0; j < total; j++, sptr += size0, dptr += size1 )
504 for( k = 0; k < size1; k++ )
511 void transpose(const Mat& src, Mat& dst)
513 CV_Assert(src.data != dst.data && "Inplace is not support in cvtest::transpose");
514 CV_Assert(src.dims == 2);
515 dst.create(src.cols, src.rows, src.type());
516 int i, j, k, esz = (int)src.elemSize();
518 for( i = 0; i < dst.rows; i++ )
520 const uchar* sptr = src.ptr(0) + i*esz;
521 uchar* dptr = dst.ptr(i);
523 for( j = 0; j < dst.cols; j++, sptr += src.step[0], dptr += esz )
525 for( k = 0; k < esz; k++ )
532 template<typename _Tp> static void
533 randUniInt_(RNG& rng, _Tp* data, size_t total, int cn, const Scalar& scale, const Scalar& delta)
535 for( size_t i = 0; i < total; i += cn )
536 for( int k = 0; k < cn; k++ )
538 int val = cvFloor( randInt(rng)*scale[k] + delta[k] );
539 data[i + k] = saturate_cast<_Tp>(val);
544 template<typename _Tp> static void
545 randUniFlt_(RNG& rng, _Tp* data, size_t total, int cn, const Scalar& scale, const Scalar& delta)
547 for( size_t i = 0; i < total; i += cn )
548 for( int k = 0; k < cn; k++ )
550 double val = randReal(rng)*scale[k] + delta[k];
551 data[i + k] = saturate_cast<_Tp>(val);
556 void randUni( RNG& rng, Mat& a, const Scalar& param0, const Scalar& param1 )
558 Scalar scale = param0;
559 Scalar delta = param1;
560 double C = a.depth() < CV_32F ? 1./(65536.*65536.) : 1.;
562 for( int k = 0; k < 4; k++ )
564 double s = scale.val[k] - delta.val[k];
569 delta.val[k] = scale.val[k];
575 const Mat *arrays[]={&a, 0};
578 NAryMatIterator it(arrays, &plane);
579 size_t i, nplanes = it.nplanes;
580 int depth = a.depth(), cn = a.channels();
581 size_t total = plane.total()*cn;
583 for( i = 0; i < nplanes; i++, ++it )
588 randUniInt_(rng, plane.ptr<uchar>(), total, cn, scale, delta);
591 randUniInt_(rng, plane.ptr<schar>(), total, cn, scale, delta);
594 randUniInt_(rng, plane.ptr<ushort>(), total, cn, scale, delta);
597 randUniInt_(rng, plane.ptr<short>(), total, cn, scale, delta);
600 randUniInt_(rng, plane.ptr<int>(), total, cn, scale, delta);
603 randUniFlt_(rng, plane.ptr<float>(), total, cn, scale, delta);
606 randUniFlt_(rng, plane.ptr<double>(), total, cn, scale, delta);
615 template<typename _Tp> static void
616 erode_(const Mat& src, Mat& dst, const vector<int>& ofsvec)
618 int width = dst.cols*src.channels(), n = (int)ofsvec.size();
619 const int* ofs = &ofsvec[0];
621 for( int y = 0; y < dst.rows; y++ )
623 const _Tp* sptr = src.ptr<_Tp>(y);
624 _Tp* dptr = dst.ptr<_Tp>(y);
626 for( int x = 0; x < width; x++ )
628 _Tp result = sptr[x + ofs[0]];
629 for( int i = 1; i < n; i++ )
630 result = std::min(result, sptr[x + ofs[i]]);
637 template<typename _Tp> static void
638 dilate_(const Mat& src, Mat& dst, const vector<int>& ofsvec)
640 int width = dst.cols*src.channels(), n = (int)ofsvec.size();
641 const int* ofs = &ofsvec[0];
643 for( int y = 0; y < dst.rows; y++ )
645 const _Tp* sptr = src.ptr<_Tp>(y);
646 _Tp* dptr = dst.ptr<_Tp>(y);
648 for( int x = 0; x < width; x++ )
650 _Tp result = sptr[x + ofs[0]];
651 for( int i = 1; i < n; i++ )
652 result = std::max(result, sptr[x + ofs[i]]);
659 void erode(const Mat& _src, Mat& dst, const Mat& _kernel, Point anchor,
660 int borderType, const Scalar& _borderValue)
662 //if( _src.type() == CV_16UC3 && _src.size() == Size(1, 2) )
664 Mat kernel = _kernel, src;
665 Scalar borderValue = _borderValue;
667 kernel = Mat::ones(3, 3, CV_8U);
670 CV_Assert( kernel.type() == CV_8U );
672 if( anchor == Point(-1,-1) )
673 anchor = Point(kernel.cols/2, kernel.rows/2);
674 if( borderType == BORDER_CONSTANT )
675 borderValue = getMaxVal(src.depth());
676 copyMakeBorder(_src, src, anchor.y, kernel.rows - anchor.y - 1,
677 anchor.x, kernel.cols - anchor.x - 1,
678 borderType, borderValue);
679 dst.create( _src.size(), src.type() );
682 int step = (int)(src.step/src.elemSize1()), cn = src.channels();
683 for( int i = 0; i < kernel.rows; i++ )
684 for( int j = 0; j < kernel.cols; j++ )
685 if( kernel.at<uchar>(i, j) != 0 )
686 ofs.push_back(i*step + j*cn);
688 ofs.push_back(anchor.y*step + anchor.x*cn);
690 switch( src.depth() )
693 erode_<uchar>(src, dst, ofs);
696 erode_<schar>(src, dst, ofs);
699 erode_<ushort>(src, dst, ofs);
702 erode_<short>(src, dst, ofs);
705 erode_<int>(src, dst, ofs);
708 erode_<float>(src, dst, ofs);
711 erode_<double>(src, dst, ofs);
718 void dilate(const Mat& _src, Mat& dst, const Mat& _kernel, Point anchor,
719 int borderType, const Scalar& _borderValue)
721 Mat kernel = _kernel, src;
722 Scalar borderValue = _borderValue;
724 kernel = Mat::ones(3, 3, CV_8U);
727 CV_Assert( kernel.type() == CV_8U );
729 if( anchor == Point(-1,-1) )
730 anchor = Point(kernel.cols/2, kernel.rows/2);
731 if( borderType == BORDER_CONSTANT )
732 borderValue = getMinVal(src.depth());
733 copyMakeBorder(_src, src, anchor.y, kernel.rows - anchor.y - 1,
734 anchor.x, kernel.cols - anchor.x - 1,
735 borderType, borderValue);
736 dst.create( _src.size(), src.type() );
739 int step = (int)(src.step/src.elemSize1()), cn = src.channels();
740 for( int i = 0; i < kernel.rows; i++ )
741 for( int j = 0; j < kernel.cols; j++ )
742 if( kernel.at<uchar>(i, j) != 0 )
743 ofs.push_back(i*step + j*cn);
745 ofs.push_back(anchor.y*step + anchor.x*cn);
747 switch( src.depth() )
750 dilate_<uchar>(src, dst, ofs);
753 dilate_<schar>(src, dst, ofs);
756 dilate_<ushort>(src, dst, ofs);
759 dilate_<short>(src, dst, ofs);
762 dilate_<int>(src, dst, ofs);
765 dilate_<float>(src, dst, ofs);
768 dilate_<double>(src, dst, ofs);
776 template<typename _Tp> static void
777 filter2D_(const Mat& src, Mat& dst, const vector<int>& ofsvec, const vector<double>& coeffvec)
779 const int* ofs = &ofsvec[0];
780 const double* coeff = &coeffvec[0];
781 int width = dst.cols*dst.channels(), ncoeffs = (int)ofsvec.size();
783 for( int y = 0; y < dst.rows; y++ )
785 const _Tp* sptr = src.ptr<_Tp>(y);
786 double* dptr = dst.ptr<double>(y);
788 for( int x = 0; x < width; x++ )
791 for( int i = 0; i < ncoeffs; i++ )
792 s += sptr[x + ofs[i]]*coeff[i];
799 void filter2D(const Mat& _src, Mat& dst, int ddepth, const Mat& kernel,
800 Point anchor, double delta, int borderType, const Scalar& _borderValue)
803 Scalar borderValue = _borderValue;
804 CV_Assert( kernel.type() == CV_32F || kernel.type() == CV_64F );
805 if( anchor == Point(-1,-1) )
806 anchor = Point(kernel.cols/2, kernel.rows/2);
807 if( borderType == BORDER_CONSTANT )
808 borderValue = getMinVal(src.depth());
809 copyMakeBorder(_src, src, anchor.y, kernel.rows - anchor.y - 1,
810 anchor.x, kernel.cols - anchor.x - 1,
811 borderType, borderValue);
812 _dst.create( _src.size(), CV_MAKETYPE(CV_64F, src.channels()) );
815 vector<double> coeff(kernel.rows*kernel.cols);
816 Mat cmat(kernel.rows, kernel.cols, CV_64F, &coeff[0]);
817 convert(kernel, cmat, cmat.type());
819 int step = (int)(src.step/src.elemSize1()), cn = src.channels();
820 for( int i = 0; i < kernel.rows; i++ )
821 for( int j = 0; j < kernel.cols; j++ )
822 ofs.push_back(i*step + j*cn);
824 switch( src.depth() )
827 filter2D_<uchar>(src, _dst, ofs, coeff);
830 filter2D_<schar>(src, _dst, ofs, coeff);
833 filter2D_<ushort>(src, _dst, ofs, coeff);
836 filter2D_<short>(src, _dst, ofs, coeff);
839 filter2D_<int>(src, _dst, ofs, coeff);
842 filter2D_<float>(src, _dst, ofs, coeff);
845 filter2D_<double>(src, _dst, ofs, coeff);
851 convert(_dst, dst, ddepth, 1, delta);
855 static int borderInterpolate( int p, int len, int borderType )
857 if( (unsigned)p < (unsigned)len )
859 else if( borderType == BORDER_REPLICATE )
860 p = p < 0 ? 0 : len - 1;
861 else if( borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101 )
863 int delta = borderType == BORDER_REFLECT_101;
871 p = len - 1 - (p - len) - delta;
873 while( (unsigned)p >= (unsigned)len );
875 else if( borderType == BORDER_WRAP )
878 p -= ((p-len+1)/len)*len;
882 else if( borderType == BORDER_CONSTANT )
885 CV_Error( Error::StsBadArg, "Unknown/unsupported border type" );
890 void copyMakeBorder(const Mat& src, Mat& dst, int top, int bottom, int left, int right,
891 int borderType, const Scalar& borderValue)
893 dst.create(src.rows + top + bottom, src.cols + left + right, src.type());
894 int i, j, k, esz = (int)src.elemSize();
895 int width = src.cols*esz, width1 = dst.cols*esz;
897 if( borderType == BORDER_CONSTANT )
899 vector<uchar> valvec((src.cols + left + right)*esz);
900 uchar* val = &valvec[0];
901 scalarToRawData(borderValue, val, src.type(), (src.cols + left + right)*src.channels());
905 for( i = 0; i < src.rows; i++ )
907 const uchar* sptr = src.ptr(i);
908 uchar* dptr = dst.ptr(i + top) + left;
909 for( j = 0; j < left; j++ )
910 dptr[j - left] = val[j];
912 for( j = 0; j < width; j++ )
914 for( j = 0; j < right; j++ )
915 dptr[j + width] = val[j];
918 for( i = 0; i < top; i++ )
920 uchar* dptr = dst.ptr(i);
921 for( j = 0; j < width1; j++ )
925 for( i = 0; i < bottom; i++ )
927 uchar* dptr = dst.ptr(i + top + src.rows);
928 for( j = 0; j < width1; j++ )
934 vector<int> tabvec((left + right)*esz + 1);
935 int* ltab = &tabvec[0];
936 int* rtab = &tabvec[left*esz];
937 for( i = 0; i < left; i++ )
939 j = borderInterpolate(i - left, src.cols, borderType)*esz;
940 for( k = 0; k < esz; k++ )
941 ltab[i*esz + k] = j + k;
943 for( i = 0; i < right; i++ )
945 j = borderInterpolate(src.cols + i, src.cols, borderType)*esz;
946 for( k = 0; k < esz; k++ )
947 rtab[i*esz + k] = j + k;
952 for( i = 0; i < src.rows; i++ )
954 const uchar* sptr = src.ptr(i);
955 uchar* dptr = dst.ptr(i + top);
957 for( j = 0; j < left; j++ )
958 dptr[j] = sptr[ltab[j]];
959 if( dptr + left != sptr )
961 for( j = 0; j < width; j++ )
962 dptr[j + left] = sptr[j];
964 for( j = 0; j < right; j++ )
965 dptr[j + left + width] = sptr[rtab[j]];
968 for( i = 0; i < top; i++ )
970 j = borderInterpolate(i - top, src.rows, borderType);
971 const uchar* sptr = dst.ptr(j + top);
972 uchar* dptr = dst.ptr(i);
974 for( k = 0; k < width1; k++ )
978 for( i = 0; i < bottom; i++ )
980 j = borderInterpolate(i + src.rows, src.rows, borderType);
981 const uchar* sptr = dst.ptr(j + top);
982 uchar* dptr = dst.ptr(i + top + src.rows);
984 for( k = 0; k < width1; k++ )
991 template<typename _Tp> static void
992 minMaxLoc_(const _Tp* src, size_t total, size_t startidx,
993 double* _minval, double* _maxval,
994 size_t* _minpos, size_t* _maxpos,
997 _Tp maxval = saturate_cast<_Tp>(*_maxval), minval = saturate_cast<_Tp>(*_minval);
998 size_t minpos = *_minpos, maxpos = *_maxpos;
1002 for( size_t i = 0; i < total; i++ )
1005 if( minval > val || !minpos )
1008 minpos = startidx + i;
1010 if( maxval < val || !maxpos )
1013 maxpos = startidx + i;
1019 for( size_t i = 0; i < total; i++ )
1022 if( (minval > val || !minpos) && mask[i] )
1025 minpos = startidx + i;
1027 if( (maxval < val || !maxpos) && mask[i] )
1030 maxpos = startidx + i;
1042 static void setpos( const Mat& mtx, vector<int>& pos, size_t idx )
1044 pos.resize(mtx.dims);
1048 for( int i = mtx.dims-1; i >= 0; i-- )
1050 int sz = mtx.size[i]*(i == mtx.dims-1 ? mtx.channels() : 1);
1051 pos[i] = (int)(idx % sz);
1057 for( int i = mtx.dims-1; i >= 0; i-- )
1062 void minMaxLoc(const Mat& src, double* _minval, double* _maxval,
1063 vector<int>* _minloc, vector<int>* _maxloc,
1066 CV_Assert( src.channels() == 1 );
1067 const Mat *arrays[]={&src, &mask, 0};
1070 NAryMatIterator it(arrays, planes);
1071 size_t startidx = 1, total = planes[0].total();
1072 size_t i, nplanes = it.nplanes;
1073 int depth = src.depth();
1076 size_t maxidx = 0, minidx = 0;
1078 for( i = 0; i < nplanes; i++, ++it, startidx += total )
1080 const uchar* sptr = planes[0].ptr();
1081 const uchar* mptr = planes[1].ptr();
1086 minMaxLoc_((const uchar*)sptr, total, startidx,
1087 &minval, &maxval, &minidx, &maxidx, mptr);
1090 minMaxLoc_((const schar*)sptr, total, startidx,
1091 &minval, &maxval, &minidx, &maxidx, mptr);
1094 minMaxLoc_((const ushort*)sptr, total, startidx,
1095 &minval, &maxval, &minidx, &maxidx, mptr);
1098 minMaxLoc_((const short*)sptr, total, startidx,
1099 &minval, &maxval, &minidx, &maxidx, mptr);
1102 minMaxLoc_((const int*)sptr, total, startidx,
1103 &minval, &maxval, &minidx, &maxidx, mptr);
1106 minMaxLoc_((const float*)sptr, total, startidx,
1107 &minval, &maxval, &minidx, &maxidx, mptr);
1110 minMaxLoc_((const double*)sptr, total, startidx,
1111 &minval, &maxval, &minidx, &maxidx, mptr);
1123 setpos( src, *_maxloc, maxidx );
1125 setpos( src, *_minloc, minidx );
1130 normHamming(const uchar* src, size_t total, int cellSize)
1133 int mask = cellSize == 1 ? 1 : cellSize == 2 ? 3 : cellSize == 4 ? 15 : -1;
1134 CV_Assert( mask >= 0 );
1136 for( size_t i = 0; i < total; i++ )
1138 unsigned a = src[i];
1139 for( ; a != 0; a >>= cellSize )
1140 result += (a & mask) != 0;
1146 template<typename _Tp> static double
1147 norm_(const _Tp* src, size_t total, int cn, int normType, double startval, const uchar* mask)
1150 double result = startval;
1154 if( normType == NORM_INF )
1157 for( i = 0; i < total; i++ )
1158 result = std::max(result, (double)std::abs(0+src[i]));// trick with 0 used to quiet gcc warning
1160 for( int c = 0; c < cn; c++ )
1162 for( i = 0; i < total; i++ )
1164 result = std::max(result, (double)std::abs(0+src[i*cn + c]));
1167 else if( normType == NORM_L1 )
1170 for( i = 0; i < total; i++ )
1171 result += std::abs(0+src[i]);
1173 for( int c = 0; c < cn; c++ )
1175 for( i = 0; i < total; i++ )
1177 result += std::abs(0+src[i*cn + c]);
1183 for( i = 0; i < total; i++ )
1189 for( int c = 0; c < cn; c++ )
1191 for( i = 0; i < total; i++ )
1194 double v = src[i*cn + c];
1203 template<typename _Tp> static double
1204 norm_(const _Tp* src1, const _Tp* src2, size_t total, int cn, int normType, double startval, const uchar* mask)
1207 double result = startval;
1211 if( normType == NORM_INF )
1214 for( i = 0; i < total; i++ )
1215 result = std::max(result, (double)std::abs(src1[i] - src2[i]));
1217 for( int c = 0; c < cn; c++ )
1219 for( i = 0; i < total; i++ )
1221 result = std::max(result, (double)std::abs(src1[i*cn + c] - src2[i*cn + c]));
1224 else if( normType == NORM_L1 )
1227 for( i = 0; i < total; i++ )
1228 result += std::abs(src1[i] - src2[i]);
1230 for( int c = 0; c < cn; c++ )
1232 for( i = 0; i < total; i++ )
1234 result += std::abs(src1[i*cn + c] - src2[i*cn + c]);
1240 for( i = 0; i < total; i++ )
1242 double v = src1[i] - src2[i];
1246 for( int c = 0; c < cn; c++ )
1248 for( i = 0; i < total; i++ )
1251 double v = src1[i*cn + c] - src2[i*cn + c];
1260 double norm(InputArray _src, int normType, InputArray _mask)
1262 Mat src = _src.getMat(), mask = _mask.getMat();
1263 if( normType == NORM_HAMMING || normType == NORM_HAMMING2 )
1268 bitwise_and(src, mask, temp);
1269 return cvtest::norm(temp, normType, Mat());
1272 CV_Assert( src.depth() == CV_8U );
1274 const Mat *arrays[]={&src, 0};
1277 NAryMatIterator it(arrays, planes);
1278 size_t total = planes[0].total();
1279 size_t i, nplanes = it.nplanes;
1281 int cellSize = normType == NORM_HAMMING ? 1 : 2;
1283 for( i = 0; i < nplanes; i++, ++it )
1284 result += normHamming(planes[0].ptr(), total, cellSize);
1287 int normType0 = normType;
1288 normType = normType == NORM_L2SQR ? NORM_L2 : normType;
1290 CV_Assert( mask.empty() || (src.size == mask.size && mask.type() == CV_8U) );
1291 CV_Assert( normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2 );
1293 const Mat *arrays[]={&src, &mask, 0};
1296 NAryMatIterator it(arrays, planes);
1297 size_t total = planes[0].total();
1298 size_t i, nplanes = it.nplanes;
1299 int depth = src.depth(), cn = planes[0].channels();
1302 for( i = 0; i < nplanes; i++, ++it )
1304 const uchar* sptr = planes[0].ptr();
1305 const uchar* mptr = planes[1].ptr();
1310 result = norm_((const uchar*)sptr, total, cn, normType, result, mptr);
1313 result = norm_((const schar*)sptr, total, cn, normType, result, mptr);
1316 result = norm_((const ushort*)sptr, total, cn, normType, result, mptr);
1319 result = norm_((const short*)sptr, total, cn, normType, result, mptr);
1322 result = norm_((const int*)sptr, total, cn, normType, result, mptr);
1325 result = norm_((const float*)sptr, total, cn, normType, result, mptr);
1328 result = norm_((const double*)sptr, total, cn, normType, result, mptr);
1331 CV_Error(Error::StsUnsupportedFormat, "");
1334 if( normType0 == NORM_L2 )
1335 result = sqrt(result);
1340 double norm(InputArray _src1, InputArray _src2, int normType, InputArray _mask)
1342 Mat src1 = _src1.getMat(), src2 = _src2.getMat(), mask = _mask.getMat();
1343 bool isRelative = (normType & NORM_RELATIVE) != 0;
1344 normType &= ~NORM_RELATIVE;
1346 if( normType == NORM_HAMMING || normType == NORM_HAMMING2 )
1349 bitwise_xor(src1, src2, temp);
1351 bitwise_and(temp, mask, temp);
1353 CV_Assert( temp.depth() == CV_8U );
1355 const Mat *arrays[]={&temp, 0};
1358 NAryMatIterator it(arrays, planes);
1359 size_t total = planes[0].total();
1360 size_t i, nplanes = it.nplanes;
1362 int cellSize = normType == NORM_HAMMING ? 1 : 2;
1364 for( i = 0; i < nplanes; i++, ++it )
1365 result += normHamming(planes[0].ptr(), total, cellSize);
1368 int normType0 = normType;
1369 normType = normType == NORM_L2SQR ? NORM_L2 : normType;
1371 CV_Assert( src1.type() == src2.type() && src1.size == src2.size );
1372 CV_Assert( mask.empty() || (src1.size == mask.size && mask.type() == CV_8U) );
1373 CV_Assert( normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2 );
1374 const Mat *arrays[]={&src1, &src2, &mask, 0};
1377 NAryMatIterator it(arrays, planes);
1378 size_t total = planes[0].total();
1379 size_t i, nplanes = it.nplanes;
1380 int depth = src1.depth(), cn = planes[0].channels();
1383 for( i = 0; i < nplanes; i++, ++it )
1385 const uchar* sptr1 = planes[0].ptr();
1386 const uchar* sptr2 = planes[1].ptr();
1387 const uchar* mptr = planes[2].ptr();
1392 result = norm_((const uchar*)sptr1, (const uchar*)sptr2, total, cn, normType, result, mptr);
1395 result = norm_((const schar*)sptr1, (const schar*)sptr2, total, cn, normType, result, mptr);
1398 result = norm_((const ushort*)sptr1, (const ushort*)sptr2, total, cn, normType, result, mptr);
1401 result = norm_((const short*)sptr1, (const short*)sptr2, total, cn, normType, result, mptr);
1404 result = norm_((const int*)sptr1, (const int*)sptr2, total, cn, normType, result, mptr);
1407 result = norm_((const float*)sptr1, (const float*)sptr2, total, cn, normType, result, mptr);
1410 result = norm_((const double*)sptr1, (const double*)sptr2, total, cn, normType, result, mptr);
1413 CV_Error(Error::StsUnsupportedFormat, "");
1416 if( normType0 == NORM_L2 )
1417 result = sqrt(result);
1418 return isRelative ? result / (cvtest::norm(src2, normType) + DBL_EPSILON) : result;
1421 double PSNR(InputArray _src1, InputArray _src2)
1423 CV_Assert( _src1.depth() == CV_8U );
1424 double diff = std::sqrt(cvtest::norm(_src1, _src2, NORM_L2SQR)/(_src1.total()*_src1.channels()));
1425 return 20*log10(255./(diff+DBL_EPSILON));
1428 template<typename _Tp> static double
1429 crossCorr_(const _Tp* src1, const _Tp* src2, size_t total)
1432 for( size_t i = 0; i < total; i++ )
1433 result += (double)src1[i]*src2[i];
1437 double crossCorr(const Mat& src1, const Mat& src2)
1439 CV_Assert( src1.size == src2.size && src1.type() == src2.type() );
1440 const Mat *arrays[]={&src1, &src2, 0};
1443 NAryMatIterator it(arrays, planes);
1444 size_t total = planes[0].total()*planes[0].channels();
1445 size_t i, nplanes = it.nplanes;
1446 int depth = src1.depth();
1449 for( i = 0; i < nplanes; i++, ++it )
1451 const uchar* sptr1 = planes[0].ptr();
1452 const uchar* sptr2 = planes[1].ptr();
1457 result += crossCorr_((const uchar*)sptr1, (const uchar*)sptr2, total);
1460 result += crossCorr_((const schar*)sptr1, (const schar*)sptr2, total);
1463 result += crossCorr_((const ushort*)sptr1, (const ushort*)sptr2, total);
1466 result += crossCorr_((const short*)sptr1, (const short*)sptr2, total);
1469 result += crossCorr_((const int*)sptr1, (const int*)sptr2, total);
1472 result += crossCorr_((const float*)sptr1, (const float*)sptr2, total);
1475 result += crossCorr_((const double*)sptr1, (const double*)sptr2, total);
1478 CV_Error(Error::StsUnsupportedFormat, "");
1486 logicOp_(const uchar* src1, const uchar* src2, uchar* dst, size_t total, char c)
1490 for( i = 0; i < total; i++ )
1491 dst[i] = src1[i] & src2[i];
1493 for( i = 0; i < total; i++ )
1494 dst[i] = src1[i] | src2[i];
1496 for( i = 0; i < total; i++ )
1497 dst[i] = src1[i] ^ src2[i];
1501 logicOpS_(const uchar* src, const uchar* scalar, uchar* dst, size_t total, char c)
1503 const size_t blockSize = 96;
1506 for( i = 0; i < total; i += blockSize, dst += blockSize, src += blockSize )
1508 size_t sz = MIN(total - i, blockSize);
1509 for( j = 0; j < sz; j++ )
1510 dst[j] = src[j] & scalar[j];
1513 for( i = 0; i < total; i += blockSize, dst += blockSize, src += blockSize )
1515 size_t sz = MIN(total - i, blockSize);
1516 for( j = 0; j < sz; j++ )
1517 dst[j] = src[j] | scalar[j];
1521 for( i = 0; i < total; i += blockSize, dst += blockSize, src += blockSize )
1523 size_t sz = MIN(total - i, blockSize);
1524 for( j = 0; j < sz; j++ )
1525 dst[j] = src[j] ^ scalar[j];
1529 for( i = 0; i < total; i++ )
1534 void logicOp( const Mat& src1, const Mat& src2, Mat& dst, char op )
1536 CV_Assert( op == '&' || op == '|' || op == '^' );
1537 CV_Assert( src1.type() == src2.type() && src1.size == src2.size );
1538 dst.create( src1.dims, &src1.size[0], src1.type() );
1539 const Mat *arrays[]={&src1, &src2, &dst, 0};
1542 NAryMatIterator it(arrays, planes);
1543 size_t total = planes[0].total()*planes[0].elemSize();
1544 size_t i, nplanes = it.nplanes;
1546 for( i = 0; i < nplanes; i++, ++it )
1548 const uchar* sptr1 = planes[0].ptr();
1549 const uchar* sptr2 = planes[1].ptr();
1550 uchar* dptr = planes[2].ptr();
1552 logicOp_(sptr1, sptr2, dptr, total, op);
1557 void logicOp(const Mat& src, const Scalar& s, Mat& dst, char op)
1559 CV_Assert( op == '&' || op == '|' || op == '^' || op == '~' );
1560 dst.create( src.dims, &src.size[0], src.type() );
1561 const Mat *arrays[]={&src, &dst, 0};
1564 NAryMatIterator it(arrays, planes);
1565 size_t total = planes[0].total()*planes[0].elemSize();
1566 size_t i, nplanes = it.nplanes;
1568 scalarToRawData(s, buf, src.type(), (int)(96/planes[0].elemSize1()));
1570 for( i = 0; i < nplanes; i++, ++it )
1572 const uchar* sptr = planes[0].ptr();
1573 uchar* dptr = planes[1].ptr();
1575 logicOpS_(sptr, (uchar*)&buf[0], dptr, total, op);
1580 template<typename _Tp> static void
1581 compare_(const _Tp* src1, const _Tp* src2, uchar* dst, size_t total, int cmpop)
1587 for( i = 0; i < total; i++ )
1588 dst[i] = src1[i] < src2[i] ? 255 : 0;
1591 for( i = 0; i < total; i++ )
1592 dst[i] = src1[i] <= src2[i] ? 255 : 0;
1595 for( i = 0; i < total; i++ )
1596 dst[i] = src1[i] == src2[i] ? 255 : 0;
1599 for( i = 0; i < total; i++ )
1600 dst[i] = src1[i] != src2[i] ? 255 : 0;
1603 for( i = 0; i < total; i++ )
1604 dst[i] = src1[i] >= src2[i] ? 255 : 0;
1607 for( i = 0; i < total; i++ )
1608 dst[i] = src1[i] > src2[i] ? 255 : 0;
1611 CV_Error(Error::StsBadArg, "Unknown comparison operation");
1616 template<typename _Tp, typename _WTp> static void
1617 compareS_(const _Tp* src1, _WTp value, uchar* dst, size_t total, int cmpop)
1623 for( i = 0; i < total; i++ )
1624 dst[i] = src1[i] < value ? 255 : 0;
1627 for( i = 0; i < total; i++ )
1628 dst[i] = src1[i] <= value ? 255 : 0;
1631 for( i = 0; i < total; i++ )
1632 dst[i] = src1[i] == value ? 255 : 0;
1635 for( i = 0; i < total; i++ )
1636 dst[i] = src1[i] != value ? 255 : 0;
1639 for( i = 0; i < total; i++ )
1640 dst[i] = src1[i] >= value ? 255 : 0;
1643 for( i = 0; i < total; i++ )
1644 dst[i] = src1[i] > value ? 255 : 0;
1647 CV_Error(Error::StsBadArg, "Unknown comparison operation");
1652 void compare(const Mat& src1, const Mat& src2, Mat& dst, int cmpop)
1654 CV_Assert( src1.type() == src2.type() && src1.channels() == 1 && src1.size == src2.size );
1655 dst.create( src1.dims, &src1.size[0], CV_8U );
1656 const Mat *arrays[]={&src1, &src2, &dst, 0};
1659 NAryMatIterator it(arrays, planes);
1660 size_t total = planes[0].total();
1661 size_t i, nplanes = it.nplanes;
1662 int depth = src1.depth();
1664 for( i = 0; i < nplanes; i++, ++it )
1666 const uchar* sptr1 = planes[0].ptr();
1667 const uchar* sptr2 = planes[1].ptr();
1668 uchar* dptr = planes[2].ptr();
1673 compare_((const uchar*)sptr1, (const uchar*)sptr2, dptr, total, cmpop);
1676 compare_((const schar*)sptr1, (const schar*)sptr2, dptr, total, cmpop);
1679 compare_((const ushort*)sptr1, (const ushort*)sptr2, dptr, total, cmpop);
1682 compare_((const short*)sptr1, (const short*)sptr2, dptr, total, cmpop);
1685 compare_((const int*)sptr1, (const int*)sptr2, dptr, total, cmpop);
1688 compare_((const float*)sptr1, (const float*)sptr2, dptr, total, cmpop);
1691 compare_((const double*)sptr1, (const double*)sptr2, dptr, total, cmpop);
1694 CV_Error(Error::StsUnsupportedFormat, "");
1699 void compare(const Mat& src, double value, Mat& dst, int cmpop)
1701 CV_Assert( src.channels() == 1 );
1702 dst.create( src.dims, &src.size[0], CV_8U );
1703 const Mat *arrays[]={&src, &dst, 0};
1706 NAryMatIterator it(arrays, planes);
1707 size_t total = planes[0].total();
1708 size_t i, nplanes = it.nplanes;
1709 int depth = src.depth();
1710 int ivalue = saturate_cast<int>(value);
1712 for( i = 0; i < nplanes; i++, ++it )
1714 const uchar* sptr = planes[0].ptr();
1715 uchar* dptr = planes[1].ptr();
1720 compareS_((const uchar*)sptr, ivalue, dptr, total, cmpop);
1723 compareS_((const schar*)sptr, ivalue, dptr, total, cmpop);
1726 compareS_((const ushort*)sptr, ivalue, dptr, total, cmpop);
1729 compareS_((const short*)sptr, ivalue, dptr, total, cmpop);
1732 compareS_((const int*)sptr, ivalue, dptr, total, cmpop);
1735 compareS_((const float*)sptr, value, dptr, total, cmpop);
1738 compareS_((const double*)sptr, value, dptr, total, cmpop);
1741 CV_Error(Error::StsUnsupportedFormat, "");
1747 template<typename _Tp> double
1748 cmpUlpsInt_(const _Tp* src1, const _Tp* src2, size_t total, int imaxdiff,
1749 size_t startidx, size_t& idx)
1752 int realmaxdiff = 0;
1753 for( i = 0; i < total; i++ )
1755 int diff = std::abs(src1[i] - src2[i]);
1756 if( realmaxdiff < diff )
1759 if( diff > imaxdiff && idx == 0 )
1767 template<> double cmpUlpsInt_<int>(const int* src1, const int* src2,
1768 size_t total, int imaxdiff,
1769 size_t startidx, size_t& idx)
1772 double realmaxdiff = 0;
1773 for( i = 0; i < total; i++ )
1775 double diff = fabs((double)src1[i] - (double)src2[i]);
1776 if( realmaxdiff < diff )
1779 if( diff > imaxdiff && idx == 0 )
1788 cmpUlpsFlt_(const int* src1, const int* src2, size_t total, int imaxdiff, size_t startidx, size_t& idx)
1790 const int C = 0x7fffffff;
1791 int realmaxdiff = 0;
1793 for( i = 0; i < total; i++ )
1795 int a = src1[i], b = src2[i];
1798 int diff = std::abs(a - b);
1799 if( realmaxdiff < diff )
1802 if( diff > imaxdiff && idx == 0 )
1811 cmpUlpsFlt_(const int64* src1, const int64* src2, size_t total, int imaxdiff, size_t startidx, size_t& idx)
1813 const int64 C = CV_BIG_INT(0x7fffffffffffffff);
1814 double realmaxdiff = 0;
1816 for( i = 0; i < total; i++ )
1818 int64 a = src1[i], b = src2[i];
1821 double diff = fabs((double)a - (double)b);
1822 if( realmaxdiff < diff )
1825 if( diff > imaxdiff && idx == 0 )
1832 bool cmpUlps(const Mat& src1, const Mat& src2, int imaxDiff, double* _realmaxdiff, vector<int>* loc)
1834 CV_Assert( src1.type() == src2.type() && src1.size == src2.size );
1835 const Mat *arrays[]={&src1, &src2, 0};
1837 NAryMatIterator it(arrays, planes);
1838 size_t total = planes[0].total()*planes[0].channels();
1839 size_t i, nplanes = it.nplanes;
1840 int depth = src1.depth();
1841 size_t startidx = 1, idx = 0;
1845 for( i = 0; i < nplanes; i++, ++it, startidx += total )
1847 const uchar* sptr1 = planes[0].ptr();
1848 const uchar* sptr2 = planes[1].ptr();
1849 double realmaxdiff = 0;
1854 realmaxdiff = cmpUlpsInt_((const uchar*)sptr1, (const uchar*)sptr2, total, imaxDiff, startidx, idx);
1857 realmaxdiff = cmpUlpsInt_((const schar*)sptr1, (const schar*)sptr2, total, imaxDiff, startidx, idx);
1860 realmaxdiff = cmpUlpsInt_((const ushort*)sptr1, (const ushort*)sptr2, total, imaxDiff, startidx, idx);
1863 realmaxdiff = cmpUlpsInt_((const short*)sptr1, (const short*)sptr2, total, imaxDiff, startidx, idx);
1866 realmaxdiff = cmpUlpsInt_((const int*)sptr1, (const int*)sptr2, total, imaxDiff, startidx, idx);
1869 realmaxdiff = cmpUlpsFlt_((const int*)sptr1, (const int*)sptr2, total, imaxDiff, startidx, idx);
1872 realmaxdiff = cmpUlpsFlt_((const int64*)sptr1, (const int64*)sptr2, total, imaxDiff, startidx, idx);
1875 CV_Error(Error::StsUnsupportedFormat, "");
1879 *_realmaxdiff = std::max(*_realmaxdiff, realmaxdiff);
1882 setpos(src1, *loc, idx);
1887 template<typename _Tp> static void
1888 checkInt_(const _Tp* a, size_t total, int imin, int imax, size_t startidx, size_t& idx)
1890 for( size_t i = 0; i < total; i++ )
1893 if( val < imin || val > imax )
1902 template<typename _Tp> static void
1903 checkFlt_(const _Tp* a, size_t total, double fmin, double fmax, size_t startidx, size_t& idx)
1905 for( size_t i = 0; i < total; i++ )
1908 if( cvIsNaN(val) || cvIsInf(val) || val < fmin || val > fmax )
1917 // checks that the array does not have NaNs and/or Infs and all the elements are
1918 // within [min_val,max_val). idx is the index of the first "bad" element.
1919 int check( const Mat& a, double fmin, double fmax, vector<int>* _idx )
1921 const Mat *arrays[]={&a, 0};
1923 NAryMatIterator it(arrays, &plane);
1924 size_t total = plane.total()*plane.channels();
1925 size_t i, nplanes = it.nplanes;
1926 int depth = a.depth();
1927 size_t startidx = 1, idx = 0;
1928 int imin = 0, imax = 0;
1930 if( depth <= CV_32S )
1932 imin = cvCeil(fmin);
1933 imax = cvFloor(fmax);
1936 for( i = 0; i < nplanes; i++, ++it, startidx += total )
1938 const uchar* aptr = plane.ptr();
1943 checkInt_((const uchar*)aptr, total, imin, imax, startidx, idx);
1946 checkInt_((const schar*)aptr, total, imin, imax, startidx, idx);
1949 checkInt_((const ushort*)aptr, total, imin, imax, startidx, idx);
1952 checkInt_((const short*)aptr, total, imin, imax, startidx, idx);
1955 checkInt_((const int*)aptr, total, imin, imax, startidx, idx);
1958 checkFlt_((const float*)aptr, total, fmin, fmax, startidx, idx);
1961 checkFlt_((const double*)aptr, total, fmin, fmax, startidx, idx);
1964 CV_Error(Error::StsUnsupportedFormat, "");
1971 if(idx != 0 && _idx)
1972 setpos(a, *_idx, idx);
1973 return idx == 0 ? 0 : -1;
1976 #define CMP_EPS_OK 0
1977 #define CMP_EPS_BIG_DIFF -1
1978 #define CMP_EPS_INVALID_TEST_DATA -2 // there is NaN or Inf value in test data
1979 #define CMP_EPS_INVALID_REF_DATA -3 // there is NaN or Inf value in reference data
1981 // compares two arrays. max_diff is the maximum actual difference,
1982 // success_err_level is maximum allowed difference, idx is the index of the first
1983 // element for which difference is >success_err_level
1984 // (or index of element with the maximum difference)
1985 int cmpEps( const Mat& arr, const Mat& refarr, double* _realmaxdiff,
1986 double success_err_level, vector<int>* _idx,
1987 bool element_wise_relative_error )
1989 CV_Assert( arr.type() == refarr.type() && arr.size == refarr.size );
1991 int ilevel = refarr.depth() <= CV_32S ? cvFloor(success_err_level) : 0;
1992 int result = CMP_EPS_OK;
1994 const Mat *arrays[]={&arr, &refarr, 0};
1996 NAryMatIterator it(arrays, planes);
1997 size_t total = planes[0].total()*planes[0].channels(), j = total;
1998 size_t i, nplanes = it.nplanes;
1999 int depth = arr.depth();
2000 size_t startidx = 1, idx = 0;
2001 double realmaxdiff = 0, maxval = 0;
2006 if( refarr.depth() >= CV_32F && !element_wise_relative_error )
2008 maxval = cvtest::norm( refarr, NORM_INF );
2009 maxval = MAX(maxval, 1.);
2012 for( i = 0; i < nplanes; i++, ++it, startidx += total )
2014 const uchar* sptr1 = planes[0].ptr();
2015 const uchar* sptr2 = planes[1].ptr();
2020 realmaxdiff = cmpUlpsInt_((const uchar*)sptr1, (const uchar*)sptr2, total, ilevel, startidx, idx);
2023 realmaxdiff = cmpUlpsInt_((const schar*)sptr1, (const schar*)sptr2, total, ilevel, startidx, idx);
2026 realmaxdiff = cmpUlpsInt_((const ushort*)sptr1, (const ushort*)sptr2, total, ilevel, startidx, idx);
2029 realmaxdiff = cmpUlpsInt_((const short*)sptr1, (const short*)sptr2, total, ilevel, startidx, idx);
2032 realmaxdiff = cmpUlpsInt_((const int*)sptr1, (const int*)sptr2, total, ilevel, startidx, idx);
2035 for( j = 0; j < total; j++ )
2037 double a_val = ((float*)sptr1)[j];
2038 double b_val = ((float*)sptr2)[j];
2040 if( ((int*)sptr1)[j] == ((int*)sptr2)[j] )
2042 if( cvIsNaN(a_val) || cvIsInf(a_val) )
2044 result = CMP_EPS_INVALID_TEST_DATA;
2048 if( cvIsNaN(b_val) || cvIsInf(b_val) )
2050 result = CMP_EPS_INVALID_REF_DATA;
2054 a_val = fabs(a_val - b_val);
2055 threshold = element_wise_relative_error ? fabs(b_val) + 1 : maxval;
2056 if( a_val > threshold*success_err_level )
2058 realmaxdiff = a_val/threshold;
2066 for( j = 0; j < total; j++ )
2068 double a_val = ((double*)sptr1)[j];
2069 double b_val = ((double*)sptr2)[j];
2071 if( ((int64*)sptr1)[j] == ((int64*)sptr2)[j] )
2073 if( cvIsNaN(a_val) || cvIsInf(a_val) )
2075 result = CMP_EPS_INVALID_TEST_DATA;
2079 if( cvIsNaN(b_val) || cvIsInf(b_val) )
2081 result = CMP_EPS_INVALID_REF_DATA;
2085 a_val = fabs(a_val - b_val);
2086 threshold = element_wise_relative_error ? fabs(b_val) + 1 : maxval;
2087 if( a_val > threshold*success_err_level )
2089 realmaxdiff = a_val/threshold;
2097 return CMP_EPS_BIG_DIFF;
2100 *_realmaxdiff = MAX(*_realmaxdiff, realmaxdiff);
2105 if( result == 0 && idx != 0 )
2106 result = CMP_EPS_BIG_DIFF;
2108 if( result < -1 && _realmaxdiff )
2109 *_realmaxdiff = exp(1000.);
2111 setpos(arr, *_idx, idx);
2117 int cmpEps2( TS* ts, const Mat& a, const Mat& b, double success_err_level,
2118 bool element_wise_relative_error, const char* desc )
2123 int code = cmpEps( a, b, &diff, success_err_level, &idx, element_wise_relative_error );
2127 case CMP_EPS_BIG_DIFF:
2128 sprintf( msg, "%s: Too big difference (=%g)", desc, diff );
2129 code = TS::FAIL_BAD_ACCURACY;
2131 case CMP_EPS_INVALID_TEST_DATA:
2132 sprintf( msg, "%s: Invalid output", desc );
2133 code = TS::FAIL_INVALID_OUTPUT;
2135 case CMP_EPS_INVALID_REF_DATA:
2136 sprintf( msg, "%s: Invalid reference output", desc );
2137 code = TS::FAIL_INVALID_OUTPUT;
2145 if( a.total() == 1 )
2147 ts->printf( TS::LOG, "%s\n", msg );
2149 else if( a.dims == 2 && (a.rows == 1 || a.cols == 1) )
2151 ts->printf( TS::LOG, "%s at element %d\n", msg, idx[0] + idx[1] );
2155 string idxstr = vec2str(", ", &idx[0], idx.size());
2156 ts->printf( TS::LOG, "%s at (%s)\n", msg, idxstr.c_str() );
2164 int cmpEps2_64f( TS* ts, const double* val, const double* refval, int len,
2165 double eps, const char* param_name )
2167 Mat _val(1, len, CV_64F, (void*)val);
2168 Mat _refval(1, len, CV_64F, (void*)refval);
2170 return cmpEps2( ts, _val, _refval, eps, true, param_name );
2174 template<typename _Tp> static void
2175 GEMM_(const _Tp* a_data0, int a_step, int a_delta,
2176 const _Tp* b_data0, int b_step, int b_delta,
2177 const _Tp* c_data0, int c_step, int c_delta,
2178 _Tp* d_data, int d_step,
2179 int d_rows, int d_cols, int a_cols, int cn,
2180 double alpha, double beta)
2182 for( int i = 0; i < d_rows; i++, d_data += d_step, c_data0 += c_step, a_data0 += a_step )
2184 for( int j = 0; j < d_cols; j++ )
2186 const _Tp* a_data = a_data0;
2187 const _Tp* b_data = b_data0 + j*b_delta;
2188 const _Tp* c_data = c_data0 + j*c_delta;
2193 for( int k = 0; k < a_cols; k++ )
2195 s += ((double)a_data[0])*b_data[0];
2199 d_data[j] = (_Tp)(s*alpha + (c_data ? c_data[0]*beta : 0));
2203 double s_re = 0, s_im = 0;
2205 for( int k = 0; k < a_cols; k++ )
2207 s_re += ((double)a_data[0])*b_data[0] - ((double)a_data[1])*b_data[1];
2208 s_im += ((double)a_data[0])*b_data[1] + ((double)a_data[1])*b_data[0];
2218 s_re += c_data[0]*beta;
2219 s_im += c_data[1]*beta;
2222 d_data[j*2] = (_Tp)s_re;
2223 d_data[j*2+1] = (_Tp)s_im;
2230 void gemm( const Mat& _a, const Mat& _b, double alpha,
2231 const Mat& _c, double beta, Mat& d, int flags )
2233 Mat a = _a, b = _b, c = _c;
2235 if( a.data == d.data )
2238 if( b.data == d.data )
2241 if( !c.empty() && c.data == d.data && (flags & cv::GEMM_3_T) )
2244 int a_rows = a.rows, a_cols = a.cols, b_rows = b.rows, b_cols = b.cols;
2245 int cn = a.channels();
2246 int a_step = (int)a.step1(), a_delta = cn;
2247 int b_step = (int)b.step1(), b_delta = cn;
2248 int c_rows = 0, c_cols = 0, c_step = 0, c_delta = 0;
2250 CV_Assert( a.type() == b.type() && a.dims == 2 && b.dims == 2 && cn <= 2 );
2252 if( flags & cv::GEMM_1_T )
2254 std::swap( a_rows, a_cols );
2255 std::swap( a_step, a_delta );
2258 if( flags & cv::GEMM_2_T )
2260 std::swap( b_rows, b_cols );
2261 std::swap( b_step, b_delta );
2268 c_step = (int)c.step1();
2271 if( flags & cv::GEMM_3_T )
2273 std::swap( c_rows, c_cols );
2274 std::swap( c_step, c_delta );
2277 CV_Assert( c.dims == 2 && c.type() == a.type() && c_rows == a_rows && c_cols == b_cols );
2280 d.create(a_rows, b_cols, a.type());
2282 if( a.depth() == CV_32F )
2283 GEMM_(a.ptr<float>(), a_step, a_delta, b.ptr<float>(), b_step, b_delta,
2284 !c.empty() ? c.ptr<float>() : 0, c_step, c_delta, d.ptr<float>(),
2285 (int)d.step1(), a_rows, b_cols, a_cols, cn, alpha, beta );
2287 GEMM_(a.ptr<double>(), a_step, a_delta, b.ptr<double>(), b_step, b_delta,
2288 !c.empty() ? c.ptr<double>() : 0, c_step, c_delta, d.ptr<double>(),
2289 (int)d.step1(), a_rows, b_cols, a_cols, cn, alpha, beta );
2293 template<typename _Tp> static void
2294 transform_(const _Tp* sptr, _Tp* dptr, size_t total, int scn, int dcn, const double* mat)
2296 for( size_t i = 0; i < total; i++, sptr += scn, dptr += dcn )
2298 for( int j = 0; j < dcn; j++ )
2300 double s = mat[j*(scn + 1) + scn];
2301 for( int k = 0; k < scn; k++ )
2302 s += mat[j*(scn + 1) + k]*sptr[k];
2303 dptr[j] = saturate_cast<_Tp>(s);
2309 void transform( const Mat& src, Mat& dst, const Mat& transmat, const Mat& _shift )
2313 int scn = src.channels();
2314 int dcn = dst.channels();
2315 int depth = src.depth();
2316 int mattype = transmat.depth();
2317 Mat shift = _shift.reshape(1, 0);
2318 bool haveShift = !shift.empty();
2320 CV_Assert( scn <= 4 && dcn <= 4 &&
2321 (mattype == CV_32F || mattype == CV_64F) &&
2322 (!haveShift || (shift.type() == mattype && (shift.rows == 1 || shift.cols == 1))) );
2324 // prepare cn x (cn + 1) transform matrix
2325 if( mattype == CV_32F )
2327 for( int i = 0; i < transmat.rows; i++ )
2329 mat[i*(scn+1)+scn] = 0.;
2330 for( int j = 0; j < transmat.cols; j++ )
2331 mat[i*(scn+1)+j] = transmat.at<float>(i,j);
2333 mat[i*(scn+1)+scn] = shift.at<float>(i);
2338 for( int i = 0; i < transmat.rows; i++ )
2340 mat[i*(scn+1)+scn] = 0.;
2341 for( int j = 0; j < transmat.cols; j++ )
2342 mat[i*(scn+1)+j] = transmat.at<double>(i,j);
2344 mat[i*(scn+1)+scn] = shift.at<double>(i);
2348 const Mat *arrays[]={&src, &dst, 0};
2350 NAryMatIterator it(arrays, planes);
2351 size_t total = planes[0].total();
2352 size_t i, nplanes = it.nplanes;
2354 for( i = 0; i < nplanes; i++, ++it )
2356 const uchar* sptr = planes[0].ptr();
2357 uchar* dptr = planes[1].ptr();
2362 transform_((const uchar*)sptr, (uchar*)dptr, total, scn, dcn, mat);
2365 transform_((const schar*)sptr, (schar*)dptr, total, scn, dcn, mat);
2368 transform_((const ushort*)sptr, (ushort*)dptr, total, scn, dcn, mat);
2371 transform_((const short*)sptr, (short*)dptr, total, scn, dcn, mat);
2374 transform_((const int*)sptr, (int*)dptr, total, scn, dcn, mat);
2377 transform_((const float*)sptr, (float*)dptr, total, scn, dcn, mat);
2380 transform_((const double*)sptr, (double*)dptr, total, scn, dcn, mat);
2383 CV_Error(Error::StsUnsupportedFormat, "");
2388 template<typename _Tp> static void
2389 minmax_(const _Tp* src1, const _Tp* src2, _Tp* dst, size_t total, char op)
2392 for( size_t i = 0; i < total; i++ )
2393 dst[i] = std::max(src1[i], src2[i]);
2395 for( size_t i = 0; i < total; i++ )
2396 dst[i] = std::min(src1[i], src2[i]);
2399 static void minmax(const Mat& src1, const Mat& src2, Mat& dst, char op)
2401 dst.create(src1.dims, src1.size, src1.type());
2402 CV_Assert( src1.type() == src2.type() && src1.size == src2.size );
2403 const Mat *arrays[]={&src1, &src2, &dst, 0};
2406 NAryMatIterator it(arrays, planes);
2407 size_t total = planes[0].total()*planes[0].channels();
2408 size_t i, nplanes = it.nplanes, depth = src1.depth();
2410 for( i = 0; i < nplanes; i++, ++it )
2412 const uchar* sptr1 = planes[0].ptr();
2413 const uchar* sptr2 = planes[1].ptr();
2414 uchar* dptr = planes[2].ptr();
2419 minmax_((const uchar*)sptr1, (const uchar*)sptr2, (uchar*)dptr, total, op);
2422 minmax_((const schar*)sptr1, (const schar*)sptr2, (schar*)dptr, total, op);
2425 minmax_((const ushort*)sptr1, (const ushort*)sptr2, (ushort*)dptr, total, op);
2428 minmax_((const short*)sptr1, (const short*)sptr2, (short*)dptr, total, op);
2431 minmax_((const int*)sptr1, (const int*)sptr2, (int*)dptr, total, op);
2434 minmax_((const float*)sptr1, (const float*)sptr2, (float*)dptr, total, op);
2437 minmax_((const double*)sptr1, (const double*)sptr2, (double*)dptr, total, op);
2440 CV_Error(Error::StsUnsupportedFormat, "");
2446 void min(const Mat& src1, const Mat& src2, Mat& dst)
2448 minmax( src1, src2, dst, 'm' );
2451 void max(const Mat& src1, const Mat& src2, Mat& dst)
2453 minmax( src1, src2, dst, 'M' );
2457 template<typename _Tp> static void
2458 minmax_(const _Tp* src1, _Tp val, _Tp* dst, size_t total, char op)
2461 for( size_t i = 0; i < total; i++ )
2462 dst[i] = std::max(src1[i], val);
2464 for( size_t i = 0; i < total; i++ )
2465 dst[i] = std::min(src1[i], val);
2468 static void minmax(const Mat& src1, double val, Mat& dst, char op)
2470 dst.create(src1.dims, src1.size, src1.type());
2471 const Mat *arrays[]={&src1, &dst, 0};
2474 NAryMatIterator it(arrays, planes);
2475 size_t total = planes[0].total()*planes[0].channels();
2476 size_t i, nplanes = it.nplanes, depth = src1.depth();
2477 int ival = saturate_cast<int>(val);
2479 for( i = 0; i < nplanes; i++, ++it )
2481 const uchar* sptr1 = planes[0].ptr();
2482 uchar* dptr = planes[1].ptr();
2487 minmax_((const uchar*)sptr1, saturate_cast<uchar>(ival), (uchar*)dptr, total, op);
2490 minmax_((const schar*)sptr1, saturate_cast<schar>(ival), (schar*)dptr, total, op);
2493 minmax_((const ushort*)sptr1, saturate_cast<ushort>(ival), (ushort*)dptr, total, op);
2496 minmax_((const short*)sptr1, saturate_cast<short>(ival), (short*)dptr, total, op);
2499 minmax_((const int*)sptr1, saturate_cast<int>(ival), (int*)dptr, total, op);
2502 minmax_((const float*)sptr1, saturate_cast<float>(val), (float*)dptr, total, op);
2505 minmax_((const double*)sptr1, saturate_cast<double>(val), (double*)dptr, total, op);
2508 CV_Error(Error::StsUnsupportedFormat, "");
2514 void min(const Mat& src1, double val, Mat& dst)
2516 minmax( src1, val, dst, 'm' );
2519 void max(const Mat& src1, double val, Mat& dst)
2521 minmax( src1, val, dst, 'M' );
2525 template<typename _Tp> static void
2526 muldiv_(const _Tp* src1, const _Tp* src2, _Tp* dst, size_t total, double scale, char op)
2529 for( size_t i = 0; i < total; i++ )
2530 dst[i] = saturate_cast<_Tp>((scale*src1[i])*src2[i]);
2532 for( size_t i = 0; i < total; i++ )
2533 dst[i] = src2[i] ? saturate_cast<_Tp>((scale*src1[i])/src2[i]) : 0;
2535 for( size_t i = 0; i < total; i++ )
2536 dst[i] = src2[i] ? saturate_cast<_Tp>(scale/src2[i]) : 0;
2539 static void muldiv(const Mat& src1, const Mat& src2, Mat& dst, double scale, char op)
2541 dst.create(src2.dims, src2.size, src2.type());
2542 CV_Assert( src1.empty() || (src1.type() == src2.type() && src1.size == src2.size) );
2543 const Mat *arrays[]={&src1, &src2, &dst, 0};
2546 NAryMatIterator it(arrays, planes);
2547 size_t total = planes[1].total()*planes[1].channels();
2548 size_t i, nplanes = it.nplanes, depth = src2.depth();
2550 for( i = 0; i < nplanes; i++, ++it )
2552 const uchar* sptr1 = planes[0].ptr();
2553 const uchar* sptr2 = planes[1].ptr();
2554 uchar* dptr = planes[2].ptr();
2559 muldiv_((const uchar*)sptr1, (const uchar*)sptr2, (uchar*)dptr, total, scale, op);
2562 muldiv_((const schar*)sptr1, (const schar*)sptr2, (schar*)dptr, total, scale, op);
2565 muldiv_((const ushort*)sptr1, (const ushort*)sptr2, (ushort*)dptr, total, scale, op);
2568 muldiv_((const short*)sptr1, (const short*)sptr2, (short*)dptr, total, scale, op);
2571 muldiv_((const int*)sptr1, (const int*)sptr2, (int*)dptr, total, scale, op);
2574 muldiv_((const float*)sptr1, (const float*)sptr2, (float*)dptr, total, scale, op);
2577 muldiv_((const double*)sptr1, (const double*)sptr2, (double*)dptr, total, scale, op);
2580 CV_Error(Error::StsUnsupportedFormat, "");
2586 void multiply(const Mat& src1, const Mat& src2, Mat& dst, double scale)
2588 muldiv( src1, src2, dst, scale, '*' );
2591 void divide(const Mat& src1, const Mat& src2, Mat& dst, double scale)
2593 muldiv( src1, src2, dst, scale, '/' );
2597 template<typename _Tp> static void
2598 mean_(const _Tp* src, const uchar* mask, size_t total, int cn, Scalar& sum, int& nz)
2604 for( size_t i = 0; i < total; i += cn )
2606 for( int c = 0; c < cn; c++ )
2607 sum[c] += src[i + c];
2612 for( size_t i = 0; i < total; i++ )
2616 for( int c = 0; c < cn; c++ )
2617 sum[c] += src[i*cn + c];
2622 Scalar mean(const Mat& src, const Mat& mask)
2624 CV_Assert(mask.empty() || (mask.type() == CV_8U && mask.size == src.size));
2628 const Mat *arrays[]={&src, &mask, 0};
2631 NAryMatIterator it(arrays, planes);
2632 size_t total = planes[0].total();
2633 size_t i, nplanes = it.nplanes;
2634 int depth = src.depth(), cn = src.channels();
2636 for( i = 0; i < nplanes; i++, ++it )
2638 const uchar* sptr = planes[0].ptr();
2639 const uchar* mptr = planes[1].ptr();
2644 mean_((const uchar*)sptr, mptr, total, cn, sum, nz);
2647 mean_((const schar*)sptr, mptr, total, cn, sum, nz);
2650 mean_((const ushort*)sptr, mptr, total, cn, sum, nz);
2653 mean_((const short*)sptr, mptr, total, cn, sum, nz);
2656 mean_((const int*)sptr, mptr, total, cn, sum, nz);
2659 mean_((const float*)sptr, mptr, total, cn, sum, nz);
2662 mean_((const double*)sptr, mptr, total, cn, sum, nz);
2665 CV_Error(Error::StsUnsupportedFormat, "");
2669 return sum * (1./std::max(nz, 1));
2673 void patchZeros( Mat& mat, double level )
2675 int j, ncols = mat.cols * mat.channels();
2676 int depth = mat.depth();
2677 CV_Assert( depth == CV_32F || depth == CV_64F );
2679 for( int i = 0; i < mat.rows; i++ )
2681 if( depth == CV_32F )
2683 float* data = mat.ptr<float>(i);
2684 for( j = 0; j < ncols; j++ )
2685 if( fabs(data[j]) < level )
2690 double* data = mat.ptr<double>(i);
2691 for( j = 0; j < ncols; j++ )
2692 if( fabs(data[j]) < level )
2699 static void calcSobelKernel1D( int order, int _aperture_size, int size, vector<int>& kernel )
2701 int i, j, oldval, newval;
2702 kernel.resize(size + 1);
2704 if( _aperture_size < 0 )
2706 static const int scharr[] = { 3, 10, 3, -1, 0, 1 };
2707 assert( size == 3 );
2708 for( i = 0; i < size; i++ )
2709 kernel[i] = scharr[order*3 + i];
2713 for( i = 1; i <= size; i++ )
2717 for( i = 0; i < size - order - 1; i++ )
2720 for( j = 1; j <= size; j++ )
2722 newval = kernel[j] + kernel[j-1];
2723 kernel[j-1] = oldval;
2728 for( i = 0; i < order; i++ )
2730 oldval = -kernel[0];
2731 for( j = 1; j <= size; j++ )
2733 newval = kernel[j-1] - kernel[j];
2734 kernel[j-1] = oldval;
2741 Mat calcSobelKernel2D( int dx, int dy, int _aperture_size, int origin )
2743 CV_Assert( (_aperture_size == -1 || (_aperture_size >= 1 && _aperture_size % 2 == 1)) &&
2744 dx >= 0 && dy >= 0 && dx + dy <= 3 );
2745 Size ksize = _aperture_size == -1 ? Size(3,3) : _aperture_size > 1 ?
2746 Size(_aperture_size, _aperture_size) : dx > 0 ? Size(3, 1) : Size(1, 3);
2748 Mat kernel(ksize, CV_32F);
2751 calcSobelKernel1D( dx, _aperture_size, ksize.width, kx );
2752 calcSobelKernel1D( dy, _aperture_size, ksize.height, ky );
2754 for( int i = 0; i < kernel.rows; i++ )
2756 float ay = (float)ky[i]*(origin && (dy & 1) ? -1 : 1);
2757 for( int j = 0; j < kernel.cols; j++ )
2758 kernel.at<float>(i, j) = kx[j]*ay;
2765 Mat calcLaplaceKernel2D( int aperture_size )
2767 int ksize = aperture_size == 1 ? 3 : aperture_size;
2768 Mat kernel(ksize, ksize, CV_32F);
2772 calcSobelKernel1D( 2, aperture_size, ksize, kx );
2773 if( aperture_size > 1 )
2774 calcSobelKernel1D( 0, aperture_size, ksize, ky );
2778 ky[0] = ky[2] = 0; ky[1] = 1;
2781 for( int i = 0; i < ksize; i++ )
2782 for( int j = 0; j < ksize; j++ )
2783 kernel.at<float>(i, j) = (float)(kx[j]*ky[i] + kx[i]*ky[j]);
2789 void initUndistortMap( const Mat& _a0, const Mat& _k0, Size sz, Mat& _mapx, Mat& _mapy )
2791 _mapx.create(sz, CV_32F);
2792 _mapy.create(sz, CV_32F);
2794 double a[9], k[5]={0,0,0,0,0};
2795 Mat _a(3, 3, CV_64F, a);
2796 Mat _k(_k0.rows,_k0.cols, CV_MAKETYPE(CV_64F,_k0.channels()),k);
2797 double fx, fy, cx, cy, ifx, ify, cxn, cyn;
2799 _a0.convertTo(_a, CV_64F);
2800 _k0.convertTo(_k, CV_64F);
2801 fx = a[0]; fy = a[4]; cx = a[2]; cy = a[5];
2802 ifx = 1./fx; ify = 1./fy;
2806 for( int v = 0; v < sz.height; v++ )
2808 for( int u = 0; u < sz.width; u++ )
2810 double x = (u - cxn)*ifx;
2811 double y = (v - cyn)*ify;
2812 double x2 = x*x, y2 = y*y;
2813 double r2 = x2 + y2;
2814 double cdist = 1 + (k[0] + (k[1] + k[4]*r2)*r2)*r2;
2815 double x1 = x*cdist + k[2]*2*x*y + k[3]*(r2 + 2*x2);
2816 double y1 = y*cdist + k[3]*2*x*y + k[2]*(r2 + 2*y2);
2818 _mapy.at<float>(v, u) = (float)(y1*fy + cy);
2819 _mapx.at<float>(v, u) = (float)(x1*fx + cx);
2825 std::ostream& operator << (std::ostream& out, const MatInfo& m)
2827 if( !m.m || m.m->empty() )
2831 static const char* depthstr[] = {"8u", "8s", "16u", "16s", "32s", "32f", "64f", "?"};
2832 out << depthstr[m.m->depth()] << "C" << m.m->channels() << " " << m.m->dims << "-dim (";
2833 for( int i = 0; i < m.m->dims; i++ )
2834 out << m.m->size[i] << (i < m.m->dims-1 ? " x " : ")");
2840 static Mat getSubArray(const Mat& m, int border, vector<int>& ofs0, vector<int>& ofs)
2842 ofs.resize(ofs0.size());
2845 std::copy(ofs0.begin(), ofs0.end(), ofs.begin());
2849 CV_Assert(d == (int)ofs.size());
2851 for( i = 0; i < d; i++ )
2853 r[i].start = std::max(0, ofs0[i] - border);
2854 r[i].end = std::min(ofs0[i] + 1 + border, m.size[i]);
2855 ofs[i] = std::min(ofs0[i], border);
2860 template<typename _Tp, typename _WTp> static void
2861 writeElems(std::ostream& out, const void* data, int nelems, int starpos)
2863 for(int i = 0; i < nelems; i++)
2867 out << (_WTp)((_Tp*)data)[i];
2870 out << (i+1 < nelems ? ", " : "");
2875 static void writeElems(std::ostream& out, const void* data, int nelems, int depth, int starpos)
2878 writeElems<uchar, int>(out, data, nelems, starpos);
2879 else if(depth == CV_8S)
2880 writeElems<schar, int>(out, data, nelems, starpos);
2881 else if(depth == CV_16U)
2882 writeElems<ushort, int>(out, data, nelems, starpos);
2883 else if(depth == CV_16S)
2884 writeElems<short, int>(out, data, nelems, starpos);
2885 else if(depth == CV_32S)
2886 writeElems<int, int>(out, data, nelems, starpos);
2887 else if(depth == CV_32F)
2889 std::streamsize pp = out.precision();
2891 writeElems<float, float>(out, data, nelems, starpos);
2894 else if(depth == CV_64F)
2896 std::streamsize pp = out.precision();
2898 writeElems<double, double>(out, data, nelems, starpos);
2902 CV_Error(Error::StsUnsupportedFormat, "");
2908 MatPart(const Mat& _m, const vector<int>* _loc)
2909 : m(&_m), loc(_loc) {}
2911 const vector<int>* loc;
2914 static std::ostream& operator << (std::ostream& out, const MatPart& m)
2916 CV_Assert( !m.loc || ((int)m.loc->size() == m.m->dims && m.m->dims <= 2) );
2921 int i, depth = m.m->depth(), cn = m.m->channels(), width = m.m->cols*cn;
2922 for( i = 0; i < m.m->rows; i++ )
2924 writeElems(out, m.m->ptr(i), width, depth, i == (*m.loc)[0] ? (*m.loc)[1] : -1);
2925 out << (i < m.m->rows-1 ? ";\n" : "");
2931 MatComparator::MatComparator(double _maxdiff, int _context)
2932 : maxdiff(_maxdiff), realmaxdiff(DBL_MAX), context(_context) {}
2934 ::testing::AssertionResult
2935 MatComparator::operator()(const char* expr1, const char* expr2,
2936 const Mat& m1, const Mat& m2)
2938 if( m1.type() != m2.type() || m1.size != m2.size )
2939 return ::testing::AssertionFailure()
2940 << "The reference and the actual output arrays have different type or size:\n"
2941 << expr1 << " ~ " << MatInfo(m1) << "\n"
2942 << expr2 << " ~ " << MatInfo(m2) << "\n";
2944 //bool ok = cvtest::cmpUlps(m1, m2, maxdiff, &realmaxdiff, &loc0);
2945 int code = cmpEps( m1, m2, &realmaxdiff, maxdiff, &loc0, true);
2948 return ::testing::AssertionSuccess();
2950 Mat m[] = {m1.reshape(1,0), m2.reshape(1,0)};
2951 int dims = m[0].dims;
2953 int border = dims <= 2 ? context : 0;
2959 m1part = Mat(1, 1, m[0].depth(), m[0].ptr(&loc[0]));
2960 m2part = Mat(1, 1, m[1].depth(), m[1].ptr(&loc[0]));
2964 m1part = getSubArray(m[0], border, loc0, loc);
2965 m2part = getSubArray(m[1], border, loc0, loc);
2968 return ::testing::AssertionFailure()
2969 << "too big relative difference (" << realmaxdiff << " > "
2970 << maxdiff << ") between "
2971 << MatInfo(m1) << " '" << expr1 << "' and '" << expr2 << "' at " << Mat(loc0).t() << ".\n"
2972 << "- " << expr1 << ":\n" << MatPart(m1part, border > 0 ? &loc : 0) << ".\n"
2973 << "- " << expr2 << ":\n" << MatPart(m2part, border > 0 ? &loc : 0) << ".\n";
2976 void printVersionInfo(bool useStdOut)
2978 // Tell CTest not to discard any output
2979 if(useStdOut) std::cout << "CTEST_FULL_OUTPUT" << std::endl;
2981 ::testing::Test::RecordProperty("cv_version", CV_VERSION);
2982 if(useStdOut) std::cout << "OpenCV version: " << CV_VERSION << std::endl;
2984 std::string buildInfo( cv::getBuildInformation() );
2986 size_t pos1 = buildInfo.find("Version control");
2987 size_t pos2 = buildInfo.find('\n', pos1);
2988 if(pos1 != std::string::npos && pos2 != std::string::npos)
2990 size_t value_start = buildInfo.rfind(' ', pos2) + 1;
2991 std::string ver( buildInfo.substr(value_start, pos2 - value_start) );
2992 ::testing::Test::RecordProperty("cv_vcs_version", ver);
2993 if (useStdOut) std::cout << "OpenCV VCS version: " << ver << std::endl;
2996 pos1 = buildInfo.find("inner version");
2997 pos2 = buildInfo.find('\n', pos1);
2998 if(pos1 != std::string::npos && pos2 != std::string::npos)
3000 size_t value_start = buildInfo.rfind(' ', pos2) + 1;
3001 std::string ver( buildInfo.substr(value_start, pos2 - value_start) );
3002 ::testing::Test::RecordProperty("cv_inner_vcs_version", ver);
3003 if(useStdOut) std::cout << "Inner VCS version: " << ver << std::endl;
3006 const char * build_type =
3013 ::testing::Test::RecordProperty("cv_build_type", build_type);
3014 if (useStdOut) std::cout << "Build type: " << build_type << std::endl;
3016 const char* parallel_framework = currentParallelFramework();
3018 if (parallel_framework) {
3019 ::testing::Test::RecordProperty("cv_parallel_framework", parallel_framework);
3020 if (useStdOut) std::cout << "Parallel framework: " << parallel_framework << std::endl;
3023 std::string cpu_features;
3026 if (checkHardwareSupport(CV_CPU_POPCNT)) cpu_features += " popcnt";
3029 if (checkHardwareSupport(CV_CPU_MMX)) cpu_features += " mmx";
3032 if (checkHardwareSupport(CV_CPU_SSE)) cpu_features += " sse";
3035 if (checkHardwareSupport(CV_CPU_SSE2)) cpu_features += " sse2";
3038 if (checkHardwareSupport(CV_CPU_SSE3)) cpu_features += " sse3";
3041 if (checkHardwareSupport(CV_CPU_SSSE3)) cpu_features += " ssse3";
3044 if (checkHardwareSupport(CV_CPU_SSE4_1)) cpu_features += " sse4.1";
3047 if (checkHardwareSupport(CV_CPU_SSE4_2)) cpu_features += " sse4.2";
3050 if (checkHardwareSupport(CV_CPU_AVX)) cpu_features += " avx";
3053 if (checkHardwareSupport(CV_CPU_AVX2)) cpu_features += " avx2";
3056 if (checkHardwareSupport(CV_CPU_FMA3)) cpu_features += " fma3";
3059 if (checkHardwareSupport(CV_CPU_AVX_512F)) cpu_features += " avx-512f";
3062 if (checkHardwareSupport(CV_CPU_AVX_512BW)) cpu_features += " avx-512bw";
3065 if (checkHardwareSupport(CV_CPU_AVX_512CD)) cpu_features += " avx-512cd";
3068 if (checkHardwareSupport(CV_CPU_AVX_512DQ)) cpu_features += " avx-512dq";
3071 if (checkHardwareSupport(CV_CPU_AVX_512ER)) cpu_features += " avx-512er";
3073 #if CV_AVX_512IFMA512
3074 if (checkHardwareSupport(CV_CPU_AVX_512IFMA512)) cpu_features += " avx-512ifma512";
3077 if (checkHardwareSupport(CV_CPU_AVX_512PF)) cpu_features += " avx-512pf";
3080 if (checkHardwareSupport(CV_CPU_AVX_512VBMI)) cpu_features += " avx-512vbmi";
3083 if (checkHardwareSupport(CV_CPU_AVX_512VL)) cpu_features += " avx-512vl";
3086 if (checkHardwareSupport(CV_CPU_NEON)) cpu_features += " neon";
3089 if (checkHardwareSupport(CV_CPU_FP16)) cpu_features += " fp16";
3092 if (checkHardwareSupport(CV_CPU_VSX)) cpu_features += " VSX";
3095 cpu_features.erase(0, 1); // erase initial space
3097 ::testing::Test::RecordProperty("cv_cpu_features", cpu_features);
3098 if (useStdOut) std::cout << "CPU features: " << cpu_features << std::endl;
3101 const char * ipp_optimization = cv::ipp::useIPP()? "enabled" : "disabled";
3102 ::testing::Test::RecordProperty("cv_ipp_optimization", ipp_optimization);
3103 if (useStdOut) std::cout << "Intel(R) IPP optimization: " << ipp_optimization << std::endl;
3105 cv::String ippVer = cv::ipp::getIppVersion();
3106 ::testing::Test::RecordProperty("cv_ipp_version", ippVer);
3107 if(useStdOut) std::cout << "Intel(R) IPP version: " << ippVer.c_str() << std::endl;
3113 void threshold( const Mat& _src, Mat& _dst,
3114 double thresh, double maxval, int thresh_type )
3117 int depth = _src.depth(), cn = _src.channels();
3118 int width_n = _src.cols*cn, height = _src.rows;
3119 int ithresh = cvFloor(thresh);
3120 int imaxval, ithresh2;
3122 if( depth == CV_8U )
3124 ithresh2 = saturate_cast<uchar>(ithresh);
3125 imaxval = saturate_cast<uchar>(maxval);
3127 else if( depth == CV_16S )
3129 ithresh2 = saturate_cast<short>(ithresh);
3130 imaxval = saturate_cast<short>(maxval);
3134 ithresh2 = cvRound(ithresh);
3135 imaxval = cvRound(maxval);
3138 assert( depth == CV_8U || depth == CV_16S || depth == CV_32F );
3140 switch( thresh_type )
3142 case CV_THRESH_BINARY:
3143 for( i = 0; i < height; i++ )
3145 if( depth == CV_8U )
3147 const uchar* src = _src.ptr<uchar>(i);
3148 uchar* dst = _dst.ptr<uchar>(i);
3149 for( j = 0; j < width_n; j++ )
3150 dst[j] = (uchar)(src[j] > ithresh ? imaxval : 0);
3152 else if( depth == CV_16S )
3154 const short* src = _src.ptr<short>(i);
3155 short* dst = _dst.ptr<short>(i);
3156 for( j = 0; j < width_n; j++ )
3157 dst[j] = (short)(src[j] > ithresh ? imaxval : 0);
3161 const float* src = _src.ptr<float>(i);
3162 float* dst = _dst.ptr<float>(i);
3163 for( j = 0; j < width_n; j++ )
3164 dst[j] = (float)((double)src[j] > thresh ? maxval : 0.f);
3168 case CV_THRESH_BINARY_INV:
3169 for( i = 0; i < height; i++ )
3171 if( depth == CV_8U )
3173 const uchar* src = _src.ptr<uchar>(i);
3174 uchar* dst = _dst.ptr<uchar>(i);
3175 for( j = 0; j < width_n; j++ )
3176 dst[j] = (uchar)(src[j] > ithresh ? 0 : imaxval);
3178 else if( depth == CV_16S )
3180 const short* src = _src.ptr<short>(i);
3181 short* dst = _dst.ptr<short>(i);
3182 for( j = 0; j < width_n; j++ )
3183 dst[j] = (short)(src[j] > ithresh ? 0 : imaxval);
3187 const float* src = _src.ptr<float>(i);
3188 float* dst = _dst.ptr<float>(i);
3189 for( j = 0; j < width_n; j++ )
3190 dst[j] = (float)((double)src[j] > thresh ? 0.f : maxval);
3194 case CV_THRESH_TRUNC:
3195 for( i = 0; i < height; i++ )
3197 if( depth == CV_8U )
3199 const uchar* src = _src.ptr<uchar>(i);
3200 uchar* dst = _dst.ptr<uchar>(i);
3201 for( j = 0; j < width_n; j++ )
3204 dst[j] = (uchar)(s > ithresh ? ithresh2 : s);
3207 else if( depth == CV_16S )
3209 const short* src = _src.ptr<short>(i);
3210 short* dst = _dst.ptr<short>(i);
3211 for( j = 0; j < width_n; j++ )
3214 dst[j] = (short)(s > ithresh ? ithresh2 : s);
3219 const float* src = _src.ptr<float>(i);
3220 float* dst = _dst.ptr<float>(i);
3221 for( j = 0; j < width_n; j++ )
3224 dst[j] = (float)(s > thresh ? thresh : s);
3229 case CV_THRESH_TOZERO:
3230 for( i = 0; i < height; i++ )
3232 if( depth == CV_8U )
3234 const uchar* src = _src.ptr<uchar>(i);
3235 uchar* dst = _dst.ptr<uchar>(i);
3236 for( j = 0; j < width_n; j++ )
3239 dst[j] = (uchar)(s > ithresh ? s : 0);
3242 else if( depth == CV_16S )
3244 const short* src = _src.ptr<short>(i);
3245 short* dst = _dst.ptr<short>(i);
3246 for( j = 0; j < width_n; j++ )
3249 dst[j] = (short)(s > ithresh ? s : 0);
3254 const float* src = _src.ptr<float>(i);
3255 float* dst = _dst.ptr<float>(i);
3256 for( j = 0; j < width_n; j++ )
3259 dst[j] = s > thresh ? s : 0.f;
3264 case CV_THRESH_TOZERO_INV:
3265 for( i = 0; i < height; i++ )
3267 if( depth == CV_8U )
3269 const uchar* src = _src.ptr<uchar>(i);
3270 uchar* dst = _dst.ptr<uchar>(i);
3271 for( j = 0; j < width_n; j++ )
3274 dst[j] = (uchar)(s > ithresh ? 0 : s);
3277 else if( depth == CV_16S )
3279 const short* src = _src.ptr<short>(i);
3280 short* dst = _dst.ptr<short>(i);
3281 for( j = 0; j < width_n; j++ )
3284 dst[j] = (short)(s > ithresh ? 0 : s);
3289 const float* src = _src.ptr<float>(i);
3290 float* dst = _dst.ptr<float>(i);
3291 for( j = 0; j < width_n; j++ )
3294 dst[j] = s > thresh ? 0.f : s;
3306 _minMaxIdx( const float* src, const uchar* mask, double* _minVal, double* _maxVal,
3307 size_t* _minIdx, size_t* _maxIdx, int len, size_t startIdx )
3309 double minVal = FLT_MAX, maxVal = -FLT_MAX;
3310 size_t minIdx = 0, maxIdx = 0;
3314 for( int i = 0; i < len; i++ )
3320 minIdx = startIdx + i;
3325 maxIdx = startIdx + i;
3331 for( int i = 0; i < len; i++ )
3334 if( mask[i] && val < minVal )
3337 minIdx = startIdx + i;
3339 if( mask[i] && val > maxVal )
3342 maxIdx = startIdx + i;
3358 void minMaxIdx( InputArray _img, double* minVal, double* maxVal,
3359 Point* minLoc, Point* maxLoc, InputArray _mask )
3361 Mat img = _img.getMat();
3362 Mat mask = _mask.getMat();
3363 CV_Assert(img.dims <= 2);
3365 _minMaxIdx((const float*)img.data, mask.data, minVal, maxVal, (size_t*)minLoc, (size_t*)maxLoc, (int)img.total(),1);
3367 std::swap(minLoc->x, minLoc->y);
3369 std::swap(maxLoc->x, maxLoc->y);