1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
46 * This file includes the code, contributed by Simon Perreault
47 * (the function icvMedianBlur_8u_O1)
49 * Constant-time median filtering -- http://nomis80.org/ctmf.html
50 * Copyright (C) 2006 Simon Perreault
53 * Laboratoire de vision et systemes numeriques
54 * Pavillon Adrien-Pouliot
56 * Sainte-Foy, Quebec, Canada
59 * perreaul@gel.ulaval.ca
65 /****************************************************************************************\
67 \****************************************************************************************/
69 template<typename T, typename ST> struct RowSum : public BaseRowFilter
71 RowSum( int _ksize, int _anchor )
77 void operator()(const uchar* src, uchar* dst, int width, int cn)
79 const T* S = (const T*)src;
81 int i = 0, k, ksz_cn = ksize*cn;
83 width = (width - 1)*cn;
84 for( k = 0; k < cn; k++, S++, D++ )
87 for( i = 0; i < ksz_cn; i += cn )
90 for( i = 0; i < width; i += cn )
92 s += S[i + ksz_cn] - S[i];
100 template<typename ST, typename T> struct ColumnSum : public BaseColumnFilter
102 ColumnSum( int _ksize, int _anchor, double _scale )
110 void reset() { sumCount = 0; }
112 void operator()(const uchar** src, uchar* dst, int dststep, int count, int width)
116 bool haveScale = scale != 1;
117 double _scale = scale;
119 if( width != (int)sum.size() )
128 for( i = 0; i < width; i++ )
130 for( ; sumCount < ksize - 1; sumCount++, src++ )
132 const ST* Sp = (const ST*)src[0];
133 for( i = 0; i <= width - 2; i += 2 )
135 ST s0 = SUM[i] + Sp[i], s1 = SUM[i+1] + Sp[i+1];
136 SUM[i] = s0; SUM[i+1] = s1;
139 for( ; i < width; i++ )
145 CV_Assert( sumCount == ksize-1 );
149 for( ; count--; src++ )
151 const ST* Sp = (const ST*)src[0];
152 const ST* Sm = (const ST*)src[1-ksize];
156 for( i = 0; i <= width - 2; i += 2 )
158 ST s0 = SUM[i] + Sp[i], s1 = SUM[i+1] + Sp[i+1];
159 D[i] = saturate_cast<T>(s0*_scale);
160 D[i+1] = saturate_cast<T>(s1*_scale);
161 s0 -= Sm[i]; s1 -= Sm[i+1];
162 SUM[i] = s0; SUM[i+1] = s1;
165 for( ; i < width; i++ )
167 ST s0 = SUM[i] + Sp[i];
168 D[i] = saturate_cast<T>(s0*_scale);
174 for( i = 0; i <= width - 2; i += 2 )
176 ST s0 = SUM[i] + Sp[i], s1 = SUM[i+1] + Sp[i+1];
177 D[i] = saturate_cast<T>(s0);
178 D[i+1] = saturate_cast<T>(s1);
179 s0 -= Sm[i]; s1 -= Sm[i+1];
180 SUM[i] = s0; SUM[i+1] = s1;
183 for( ; i < width; i++ )
185 ST s0 = SUM[i] + Sp[i];
186 D[i] = saturate_cast<T>(s0);
200 template<> struct ColumnSum<int, uchar> : public BaseColumnFilter
202 ColumnSum( int _ksize, int _anchor, double _scale )
210 void reset() { sumCount = 0; }
212 void operator()(const uchar** src, uchar* dst, int dststep, int count, int width)
216 bool haveScale = scale != 1;
217 double _scale = scale;
220 bool haveSSE2 = checkHardwareSupport(CV_CPU_SSE2);
223 if( width != (int)sum.size() )
232 memset((void*)SUM, 0, width*sizeof(int));
233 for( ; sumCount < ksize - 1; sumCount++, src++ )
235 const int* Sp = (const int*)src[0];
240 for( ; i < width-4; i+=4 )
242 __m128i _sum = _mm_loadu_si128((const __m128i*)(SUM+i));
243 __m128i _sp = _mm_loadu_si128((const __m128i*)(Sp+i));
244 _mm_storeu_si128((__m128i*)(SUM+i),_mm_add_epi32(_sum, _sp));
248 for( ; i < width; i++ )
254 CV_Assert( sumCount == ksize-1 );
258 for( ; count--; src++ )
260 const int* Sp = (const int*)src[0];
261 const int* Sm = (const int*)src[1-ksize];
262 uchar* D = (uchar*)dst;
269 const __m128 scale4 = _mm_set1_ps((float)_scale);
270 for( ; i < width-8; i+=8 )
272 __m128i _sm = _mm_loadu_si128((const __m128i*)(Sm+i));
273 __m128i _sm1 = _mm_loadu_si128((const __m128i*)(Sm+i+4));
275 __m128i _s0 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i)),
276 _mm_loadu_si128((const __m128i*)(Sp+i)));
277 __m128i _s01 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i+4)),
278 _mm_loadu_si128((const __m128i*)(Sp+i+4)));
280 __m128i _s0T = _mm_cvtps_epi32(_mm_mul_ps(scale4, _mm_cvtepi32_ps(_s0)));
281 __m128i _s0T1 = _mm_cvtps_epi32(_mm_mul_ps(scale4, _mm_cvtepi32_ps(_s01)));
283 _s0T = _mm_packs_epi32(_s0T, _s0T1);
285 _mm_storel_epi64((__m128i*)(D+i), _mm_packus_epi16(_s0T, _s0T));
287 _mm_storeu_si128((__m128i*)(SUM+i), _mm_sub_epi32(_s0,_sm));
288 _mm_storeu_si128((__m128i*)(SUM+i+4),_mm_sub_epi32(_s01,_sm1));
292 for( ; i < width; i++ )
294 int s0 = SUM[i] + Sp[i];
295 D[i] = saturate_cast<uchar>(s0*_scale);
305 for( ; i < width-8; i+=8 )
307 __m128i _sm = _mm_loadu_si128((const __m128i*)(Sm+i));
308 __m128i _sm1 = _mm_loadu_si128((const __m128i*)(Sm+i+4));
310 __m128i _s0 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i)),
311 _mm_loadu_si128((const __m128i*)(Sp+i)));
312 __m128i _s01 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i+4)),
313 _mm_loadu_si128((const __m128i*)(Sp+i+4)));
315 __m128i _s0T = _mm_packs_epi32(_s0, _s01);
317 _mm_storel_epi64((__m128i*)(D+i), _mm_packus_epi16(_s0T, _s0T));
319 _mm_storeu_si128((__m128i*)(SUM+i), _mm_sub_epi32(_s0,_sm));
320 _mm_storeu_si128((__m128i*)(SUM+i+4),_mm_sub_epi32(_s01,_sm1));
325 for( ; i < width; i++ )
327 int s0 = SUM[i] + Sp[i];
328 D[i] = saturate_cast<uchar>(s0);
341 template<> struct ColumnSum<int, short> : public BaseColumnFilter
343 ColumnSum( int _ksize, int _anchor, double _scale )
351 void reset() { sumCount = 0; }
353 void operator()(const uchar** src, uchar* dst, int dststep, int count, int width)
357 bool haveScale = scale != 1;
358 double _scale = scale;
361 bool haveSSE2 = checkHardwareSupport(CV_CPU_SSE2);
364 if( width != (int)sum.size() )
372 memset((void*)SUM, 0, width*sizeof(int));
373 for( ; sumCount < ksize - 1; sumCount++, src++ )
375 const int* Sp = (const int*)src[0];
380 for( ; i < width-4; i+=4 )
382 __m128i _sum = _mm_loadu_si128((const __m128i*)(SUM+i));
383 __m128i _sp = _mm_loadu_si128((const __m128i*)(Sp+i));
384 _mm_storeu_si128((__m128i*)(SUM+i),_mm_add_epi32(_sum, _sp));
388 for( ; i < width; i++ )
394 CV_Assert( sumCount == ksize-1 );
398 for( ; count--; src++ )
400 const int* Sp = (const int*)src[0];
401 const int* Sm = (const int*)src[1-ksize];
402 short* D = (short*)dst;
409 const __m128 scale4 = _mm_set1_ps((float)_scale);
410 for( ; i < width-8; i+=8 )
412 __m128i _sm = _mm_loadu_si128((const __m128i*)(Sm+i));
413 __m128i _sm1 = _mm_loadu_si128((const __m128i*)(Sm+i+4));
415 __m128i _s0 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i)),
416 _mm_loadu_si128((const __m128i*)(Sp+i)));
417 __m128i _s01 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i+4)),
418 _mm_loadu_si128((const __m128i*)(Sp+i+4)));
420 __m128i _s0T = _mm_cvtps_epi32(_mm_mul_ps(scale4, _mm_cvtepi32_ps(_s0)));
421 __m128i _s0T1 = _mm_cvtps_epi32(_mm_mul_ps(scale4, _mm_cvtepi32_ps(_s01)));
423 _mm_storeu_si128((__m128i*)(D+i), _mm_packs_epi32(_s0T, _s0T1));
425 _mm_storeu_si128((__m128i*)(SUM+i),_mm_sub_epi32(_s0,_sm));
426 _mm_storeu_si128((__m128i*)(SUM+i+4), _mm_sub_epi32(_s01,_sm1));
430 for( ; i < width; i++ )
432 int s0 = SUM[i] + Sp[i];
433 D[i] = saturate_cast<short>(s0*_scale);
443 for( ; i < width-8; i+=8 )
446 __m128i _sm = _mm_loadu_si128((const __m128i*)(Sm+i));
447 __m128i _sm1 = _mm_loadu_si128((const __m128i*)(Sm+i+4));
449 __m128i _s0 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i)),
450 _mm_loadu_si128((const __m128i*)(Sp+i)));
451 __m128i _s01 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i+4)),
452 _mm_loadu_si128((const __m128i*)(Sp+i+4)));
454 _mm_storeu_si128((__m128i*)(D+i), _mm_packs_epi32(_s0, _s01));
456 _mm_storeu_si128((__m128i*)(SUM+i), _mm_sub_epi32(_s0,_sm));
457 _mm_storeu_si128((__m128i*)(SUM+i+4),_mm_sub_epi32(_s01,_sm1));
462 for( ; i < width; i++ )
464 int s0 = SUM[i] + Sp[i];
465 D[i] = saturate_cast<short>(s0);
479 template<> struct ColumnSum<int, ushort> : public BaseColumnFilter
481 ColumnSum( int _ksize, int _anchor, double _scale )
489 void reset() { sumCount = 0; }
491 void operator()(const uchar** src, uchar* dst, int dststep, int count, int width)
495 bool haveScale = scale != 1;
496 double _scale = scale;
498 bool haveSSE2 = checkHardwareSupport(CV_CPU_SSE2);
501 if( width != (int)sum.size() )
509 memset((void*)SUM, 0, width*sizeof(int));
510 for( ; sumCount < ksize - 1; sumCount++, src++ )
512 const int* Sp = (const int*)src[0];
517 for( ; i < width-4; i+=4 )
519 __m128i _sum = _mm_loadu_si128((const __m128i*)(SUM+i));
520 __m128i _sp = _mm_loadu_si128((const __m128i*)(Sp+i));
521 _mm_storeu_si128((__m128i*)(SUM+i), _mm_add_epi32(_sum, _sp));
525 for( ; i < width; i++ )
531 CV_Assert( sumCount == ksize-1 );
535 for( ; count--; src++ )
537 const int* Sp = (const int*)src[0];
538 const int* Sm = (const int*)src[1-ksize];
539 ushort* D = (ushort*)dst;
546 const __m128 scale4 = _mm_set1_ps((float)_scale);
547 const __m128i delta0 = _mm_set1_epi32(0x8000);
548 const __m128i delta1 = _mm_set1_epi32(0x80008000);
550 for( ; i < width-4; i+=4)
552 __m128i _sm = _mm_loadu_si128((const __m128i*)(Sm+i));
553 __m128i _s0 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i)),
554 _mm_loadu_si128((const __m128i*)(Sp+i)));
556 __m128i _res = _mm_cvtps_epi32(_mm_mul_ps(scale4, _mm_cvtepi32_ps(_s0)));
558 _res = _mm_sub_epi32(_res, delta0);
559 _res = _mm_add_epi16(_mm_packs_epi32(_res, _res), delta1);
561 _mm_storel_epi64((__m128i*)(D+i), _res);
562 _mm_storeu_si128((__m128i*)(SUM+i), _mm_sub_epi32(_s0,_sm));
566 for( ; i < width; i++ )
568 int s0 = SUM[i] + Sp[i];
569 D[i] = saturate_cast<ushort>(s0*_scale);
579 const __m128i delta0 = _mm_set1_epi32(0x8000);
580 const __m128i delta1 = _mm_set1_epi32(0x80008000);
582 for( ; i < width-4; i+=4 )
584 __m128i _sm = _mm_loadu_si128((const __m128i*)(Sm+i));
585 __m128i _s0 = _mm_add_epi32(_mm_loadu_si128((const __m128i*)(SUM+i)),
586 _mm_loadu_si128((const __m128i*)(Sp+i)));
588 __m128i _res = _mm_sub_epi32(_s0, delta0);
589 _res = _mm_add_epi16(_mm_packs_epi32(_res, _res), delta1);
591 _mm_storel_epi64((__m128i*)(D+i), _res);
592 _mm_storeu_si128((__m128i*)(SUM+i), _mm_sub_epi32(_s0,_sm));
597 for( ; i < width; i++ )
599 int s0 = SUM[i] + Sp[i];
600 D[i] = saturate_cast<ushort>(s0);
616 cv::Ptr<cv::BaseRowFilter> cv::getRowSumFilter(int srcType, int sumType, int ksize, int anchor)
618 int sdepth = CV_MAT_DEPTH(srcType), ddepth = CV_MAT_DEPTH(sumType);
619 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(srcType) );
624 if( sdepth == CV_8U && ddepth == CV_32S )
625 return Ptr<BaseRowFilter>(new RowSum<uchar, int>(ksize, anchor));
626 if( sdepth == CV_8U && ddepth == CV_64F )
627 return Ptr<BaseRowFilter>(new RowSum<uchar, double>(ksize, anchor));
628 if( sdepth == CV_16U && ddepth == CV_32S )
629 return Ptr<BaseRowFilter>(new RowSum<ushort, int>(ksize, anchor));
630 if( sdepth == CV_16U && ddepth == CV_64F )
631 return Ptr<BaseRowFilter>(new RowSum<ushort, double>(ksize, anchor));
632 if( sdepth == CV_16S && ddepth == CV_32S )
633 return Ptr<BaseRowFilter>(new RowSum<short, int>(ksize, anchor));
634 if( sdepth == CV_32S && ddepth == CV_32S )
635 return Ptr<BaseRowFilter>(new RowSum<int, int>(ksize, anchor));
636 if( sdepth == CV_16S && ddepth == CV_64F )
637 return Ptr<BaseRowFilter>(new RowSum<short, double>(ksize, anchor));
638 if( sdepth == CV_32F && ddepth == CV_64F )
639 return Ptr<BaseRowFilter>(new RowSum<float, double>(ksize, anchor));
640 if( sdepth == CV_64F && ddepth == CV_64F )
641 return Ptr<BaseRowFilter>(new RowSum<double, double>(ksize, anchor));
643 CV_Error_( CV_StsNotImplemented,
644 ("Unsupported combination of source format (=%d), and buffer format (=%d)",
647 return Ptr<BaseRowFilter>(0);
651 cv::Ptr<cv::BaseColumnFilter> cv::getColumnSumFilter(int sumType, int dstType, int ksize,
652 int anchor, double scale)
654 int sdepth = CV_MAT_DEPTH(sumType), ddepth = CV_MAT_DEPTH(dstType);
655 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(dstType) );
660 if( ddepth == CV_8U && sdepth == CV_32S )
661 return Ptr<BaseColumnFilter>(new ColumnSum<int, uchar>(ksize, anchor, scale));
662 if( ddepth == CV_8U && sdepth == CV_64F )
663 return Ptr<BaseColumnFilter>(new ColumnSum<double, uchar>(ksize, anchor, scale));
664 if( ddepth == CV_16U && sdepth == CV_32S )
665 return Ptr<BaseColumnFilter>(new ColumnSum<int, ushort>(ksize, anchor, scale));
666 if( ddepth == CV_16U && sdepth == CV_64F )
667 return Ptr<BaseColumnFilter>(new ColumnSum<double, ushort>(ksize, anchor, scale));
668 if( ddepth == CV_16S && sdepth == CV_32S )
669 return Ptr<BaseColumnFilter>(new ColumnSum<int, short>(ksize, anchor, scale));
670 if( ddepth == CV_16S && sdepth == CV_64F )
671 return Ptr<BaseColumnFilter>(new ColumnSum<double, short>(ksize, anchor, scale));
672 if( ddepth == CV_32S && sdepth == CV_32S )
673 return Ptr<BaseColumnFilter>(new ColumnSum<int, int>(ksize, anchor, scale));
674 if( ddepth == CV_32F && sdepth == CV_32S )
675 return Ptr<BaseColumnFilter>(new ColumnSum<int, float>(ksize, anchor, scale));
676 if( ddepth == CV_32F && sdepth == CV_64F )
677 return Ptr<BaseColumnFilter>(new ColumnSum<double, float>(ksize, anchor, scale));
678 if( ddepth == CV_64F && sdepth == CV_32S )
679 return Ptr<BaseColumnFilter>(new ColumnSum<int, double>(ksize, anchor, scale));
680 if( ddepth == CV_64F && sdepth == CV_64F )
681 return Ptr<BaseColumnFilter>(new ColumnSum<double, double>(ksize, anchor, scale));
683 CV_Error_( CV_StsNotImplemented,
684 ("Unsupported combination of sum format (=%d), and destination format (=%d)",
687 return Ptr<BaseColumnFilter>(0);
691 cv::Ptr<cv::FilterEngine> cv::createBoxFilter( int srcType, int dstType, Size ksize,
692 Point anchor, bool normalize, int borderType )
694 int sdepth = CV_MAT_DEPTH(srcType);
695 int cn = CV_MAT_CN(srcType), sumType = CV_64F;
696 if( sdepth <= CV_32S && (!normalize ||
697 ksize.width*ksize.height <= (sdepth == CV_8U ? (1<<23) :
698 sdepth == CV_16U ? (1 << 15) : (1 << 16))) )
700 sumType = CV_MAKETYPE( sumType, cn );
702 Ptr<BaseRowFilter> rowFilter = getRowSumFilter(srcType, sumType, ksize.width, anchor.x );
703 Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
704 dstType, ksize.height, anchor.y, normalize ? 1./(ksize.width*ksize.height) : 1);
706 return Ptr<FilterEngine>(new FilterEngine(Ptr<BaseFilter>(0), rowFilter, columnFilter,
707 srcType, dstType, sumType, borderType ));
711 void cv::boxFilter( InputArray _src, OutputArray _dst, int ddepth,
712 Size ksize, Point anchor,
713 bool normalize, int borderType )
715 Mat src = _src.getMat();
716 int sdepth = src.depth(), cn = src.channels();
719 _dst.create( src.size(), CV_MAKETYPE(ddepth, cn) );
720 Mat dst = _dst.getMat();
721 if( borderType != BORDER_CONSTANT && normalize )
728 #ifdef HAVE_TEGRA_OPTIMIZATION
729 if ( tegra::box(src, dst, ksize, anchor, normalize, borderType) )
733 Ptr<FilterEngine> f = createBoxFilter( src.type(), dst.type(),
734 ksize, anchor, normalize, borderType );
735 f->apply( src, dst );
738 void cv::blur( InputArray src, OutputArray dst,
739 Size ksize, Point anchor, int borderType )
741 boxFilter( src, dst, -1, ksize, anchor, true, borderType );
744 /****************************************************************************************\
746 \****************************************************************************************/
748 cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
750 const int SMALL_GAUSSIAN_SIZE = 7;
751 static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
754 {0.25f, 0.5f, 0.25f},
755 {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
756 {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
759 const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
760 small_gaussian_tab[n>>1] : 0;
762 CV_Assert( ktype == CV_32F || ktype == CV_64F );
763 Mat kernel(n, 1, ktype);
764 float* cf = (float*)kernel.data;
765 double* cd = (double*)kernel.data;
767 double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
768 double scale2X = -0.5/(sigmaX*sigmaX);
772 for( i = 0; i < n; i++ )
774 double x = i - (n-1)*0.5;
775 double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x);
776 if( ktype == CV_32F )
789 for( i = 0; i < n; i++ )
791 if( ktype == CV_32F )
792 cf[i] = (float)(cf[i]*sum);
801 cv::Ptr<cv::FilterEngine> cv::createGaussianFilter( int type, Size ksize,
802 double sigma1, double sigma2,
805 int depth = CV_MAT_DEPTH(type);
809 // automatic detection of kernel size from sigma
810 if( ksize.width <= 0 && sigma1 > 0 )
811 ksize.width = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
812 if( ksize.height <= 0 && sigma2 > 0 )
813 ksize.height = cvRound(sigma2*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
815 CV_Assert( ksize.width > 0 && ksize.width % 2 == 1 &&
816 ksize.height > 0 && ksize.height % 2 == 1 );
818 sigma1 = std::max( sigma1, 0. );
819 sigma2 = std::max( sigma2, 0. );
821 Mat kx = getGaussianKernel( ksize.width, sigma1, std::max(depth, CV_32F) );
823 if( ksize.height == ksize.width && std::abs(sigma1 - sigma2) < DBL_EPSILON )
826 ky = getGaussianKernel( ksize.height, sigma2, std::max(depth, CV_32F) );
828 return createSeparableLinearFilter( type, type, kx, ky, Point(-1,-1), 0, borderType );
832 void cv::GaussianBlur( InputArray _src, OutputArray _dst, Size ksize,
833 double sigma1, double sigma2,
836 Mat src = _src.getMat();
837 _dst.create( src.size(), src.type() );
838 Mat dst = _dst.getMat();
840 if( borderType != BORDER_CONSTANT )
848 if( ksize.width == 1 && ksize.height == 1 )
854 #ifdef HAVE_TEGRA_OPTIMIZATION
855 if(sigma1 == 0 && sigma2 == 0 && tegra::gaussian(src, dst, ksize, borderType))
859 #if defined HAVE_IPP && (IPP_VERSION_MAJOR >= 7)
860 if(src.type() == CV_32FC1 && sigma1 == sigma2 && ksize.width == ksize.height && sigma1 != 0.0 )
862 IppiSize roi = {src.cols, src.rows};
864 ippiFilterGaussGetBufferSize_32f_C1R(roi, ksize.width, &bufSize);
865 AutoBuffer<uchar> buf(bufSize+128);
866 if( ippiFilterGaussBorder_32f_C1R((const Ipp32f *)src.data, (int)src.step,
867 (Ipp32f *)dst.data, (int)dst.step,
868 roi, ksize.width, (Ipp32f)sigma1,
869 (IppiBorderType)borderType, 0.0,
870 alignPtr(&buf[0],32)) >= 0 )
875 Ptr<FilterEngine> f = createGaussianFilter( src.type(), ksize, sigma1, sigma2, borderType );
876 f->apply( src, dst );
880 /****************************************************************************************\
882 \****************************************************************************************/
889 * This structure represents a two-tier histogram. The first tier (known as the
890 * "coarse" level) is 4 bit wide and the second tier (known as the "fine" level)
891 * is 8 bit wide. Pixels inserted in the fine level also get inserted into the
892 * coarse bucket designated by the 4 MSBs of the fine bucket value.
894 * The structure is aligned on 16 bits, which is a prerequisite for SIMD
895 * instructions. Each bucket is 16 bit wide, which means that extra care must be
896 * taken to prevent overflow.
906 #define MEDIAN_HAVE_SIMD 1
908 static inline void histogram_add_simd( const HT x[16], HT y[16] )
910 const __m128i* rx = (const __m128i*)x;
911 __m128i* ry = (__m128i*)y;
912 __m128i r0 = _mm_add_epi16(_mm_load_si128(ry+0),_mm_load_si128(rx+0));
913 __m128i r1 = _mm_add_epi16(_mm_load_si128(ry+1),_mm_load_si128(rx+1));
914 _mm_store_si128(ry+0, r0);
915 _mm_store_si128(ry+1, r1);
918 static inline void histogram_sub_simd( const HT x[16], HT y[16] )
920 const __m128i* rx = (const __m128i*)x;
921 __m128i* ry = (__m128i*)y;
922 __m128i r0 = _mm_sub_epi16(_mm_load_si128(ry+0),_mm_load_si128(rx+0));
923 __m128i r1 = _mm_sub_epi16(_mm_load_si128(ry+1),_mm_load_si128(rx+1));
924 _mm_store_si128(ry+0, r0);
925 _mm_store_si128(ry+1, r1);
929 #define MEDIAN_HAVE_SIMD 0
933 static inline void histogram_add( const HT x[16], HT y[16] )
936 for( i = 0; i < 16; ++i )
937 y[i] = (HT)(y[i] + x[i]);
940 static inline void histogram_sub( const HT x[16], HT y[16] )
943 for( i = 0; i < 16; ++i )
944 y[i] = (HT)(y[i] - x[i]);
947 static inline void histogram_muladd( int a, const HT x[16],
950 for( int i = 0; i < 16; ++i )
951 y[i] = (HT)(y[i] + a * x[i]);
955 medianBlur_8u_O1( const Mat& _src, Mat& _dst, int ksize )
958 * HOP is short for Histogram OPeration. This macro makes an operation \a op on
959 * histogram \a h for pixel value \a x. It takes care of handling both levels.
961 #define HOP(h,x,op) \
963 *((HT*)h.fine + x) op
965 #define COP(c,j,x,op) \
966 h_coarse[ 16*(n*c+j) + (x>>4) ] op, \
967 h_fine[ 16 * (n*(16*c+(x>>4)) + j) + (x & 0xF) ] op
969 int cn = _dst.channels(), m = _dst.rows, r = (ksize-1)/2;
970 size_t sstep = _src.step, dstep = _dst.step;
971 Histogram CV_DECL_ALIGNED(16) H[4];
972 HT CV_DECL_ALIGNED(16) luc[4][16];
974 int STRIPE_SIZE = std::min( _dst.cols, 512/cn );
976 vector<HT> _h_coarse(1 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
977 vector<HT> _h_fine(16 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
978 HT* h_coarse = alignPtr(&_h_coarse[0], 16);
979 HT* h_fine = alignPtr(&_h_fine[0], 16);
981 volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE2);
984 for( int x = 0; x < _dst.cols; x += STRIPE_SIZE )
986 int i, j, k, c, n = std::min(_dst.cols - x, STRIPE_SIZE) + r*2;
987 const uchar* src = _src.data + x*cn;
988 uchar* dst = _dst.data + (x - r)*cn;
990 memset( h_coarse, 0, 16*n*cn*sizeof(h_coarse[0]) );
991 memset( h_fine, 0, 16*16*n*cn*sizeof(h_fine[0]) );
993 // First row initialization
994 for( c = 0; c < cn; c++ )
996 for( j = 0; j < n; j++ )
997 COP( c, j, src[cn*j+c], += (cv::HT)(r+2) );
999 for( i = 1; i < r; i++ )
1001 const uchar* p = src + sstep*std::min(i, m-1);
1002 for ( j = 0; j < n; j++ )
1003 COP( c, j, p[cn*j+c], ++ );
1007 for( i = 0; i < m; i++ )
1009 const uchar* p0 = src + sstep * std::max( 0, i-r-1 );
1010 const uchar* p1 = src + sstep * std::min( m-1, i+r );
1012 memset( H, 0, cn*sizeof(H[0]) );
1013 memset( luc, 0, cn*sizeof(luc[0]) );
1014 for( c = 0; c < cn; c++ )
1016 // Update column histograms for the entire row.
1017 for( j = 0; j < n; j++ )
1019 COP( c, j, p0[j*cn + c], -- );
1020 COP( c, j, p1[j*cn + c], ++ );
1023 // First column initialization
1024 for( k = 0; k < 16; ++k )
1025 histogram_muladd( 2*r+1, &h_fine[16*n*(16*c+k)], &H[c].fine[k][0] );
1027 #if MEDIAN_HAVE_SIMD
1030 for( j = 0; j < 2*r; ++j )
1031 histogram_add_simd( &h_coarse[16*(n*c+j)], H[c].coarse );
1033 for( j = r; j < n-r; j++ )
1035 int t = 2*r*r + 2*r, b, sum = 0;
1038 histogram_add_simd( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
1040 // Find median at coarse level
1041 for ( k = 0; k < 16 ; ++k )
1043 sum += H[c].coarse[k];
1046 sum -= H[c].coarse[k];
1052 /* Update corresponding histogram segment */
1053 if ( luc[c][k] <= j-r )
1055 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
1056 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
1057 histogram_add_simd( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
1059 if ( luc[c][k] < j+r+1 )
1061 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
1062 luc[c][k] = (HT)(j+r+1);
1067 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
1069 histogram_sub_simd( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
1070 histogram_add_simd( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
1074 histogram_sub_simd( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
1076 /* Find median in segment */
1077 segment = H[c].fine[k];
1078 for ( b = 0; b < 16 ; b++ )
1083 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
1093 for( j = 0; j < 2*r; ++j )
1094 histogram_add( &h_coarse[16*(n*c+j)], H[c].coarse );
1096 for( j = r; j < n-r; j++ )
1098 int t = 2*r*r + 2*r, b, sum = 0;
1101 histogram_add( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
1103 // Find median at coarse level
1104 for ( k = 0; k < 16 ; ++k )
1106 sum += H[c].coarse[k];
1109 sum -= H[c].coarse[k];
1115 /* Update corresponding histogram segment */
1116 if ( luc[c][k] <= j-r )
1118 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
1119 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
1120 histogram_add( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
1122 if ( luc[c][k] < j+r+1 )
1124 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
1125 luc[c][k] = (HT)(j+r+1);
1130 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
1132 histogram_sub( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
1133 histogram_add( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
1137 histogram_sub( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
1139 /* Find median in segment */
1140 segment = H[c].fine[k];
1141 for ( b = 0; b < 16 ; b++ )
1146 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
1162 medianBlur_8u_Om( const Mat& _src, Mat& _dst, int m )
1169 Size size = _dst.size();
1170 const uchar* src = _src.data;
1171 uchar* dst = _dst.data;
1172 int src_step = (int)_src.step, dst_step = (int)_dst.step;
1173 int cn = _src.channels();
1174 const uchar* src_max = src + size.height*src_step;
1176 #define UPDATE_ACC01( pix, cn, op ) \
1180 zone0[cn][p >> 4] op; \
1183 //CV_Assert( size.height >= nx && size.width >= nx );
1184 for( x = 0; x < size.width; x++, src += cn, dst += cn )
1186 uchar* dst_cur = dst;
1187 const uchar* src_top = src;
1188 const uchar* src_bottom = src;
1190 int src_step1 = src_step, dst_step1 = dst_step;
1194 src_bottom = src_top += src_step*(size.height-1);
1195 dst_cur += dst_step*(size.height-1);
1196 src_step1 = -src_step1;
1197 dst_step1 = -dst_step1;
1201 memset( zone0, 0, sizeof(zone0[0])*cn );
1202 memset( zone1, 0, sizeof(zone1[0])*cn );
1204 for( y = 0; y <= m/2; y++ )
1206 for( c = 0; c < cn; c++ )
1210 for( k = 0; k < m*cn; k += cn )
1211 UPDATE_ACC01( src_bottom[k+c], c, ++ );
1215 for( k = 0; k < m*cn; k += cn )
1216 UPDATE_ACC01( src_bottom[k+c], c, += m/2+1 );
1220 if( (src_step1 > 0 && y < size.height-1) ||
1221 (src_step1 < 0 && size.height-y-1 > 0) )
1222 src_bottom += src_step1;
1225 for( y = 0; y < size.height; y++, dst_cur += dst_step1 )
1228 for( c = 0; c < cn; c++ )
1233 int t = s + zone0[c][k];
1244 dst_cur[c] = (uchar)k;
1247 if( y+1 == size.height )
1252 for( k = 0; k < m; k++ )
1255 int q = src_bottom[k];
1264 for( k = 0; k < m*3; k += 3 )
1266 UPDATE_ACC01( src_top[k], 0, -- );
1267 UPDATE_ACC01( src_top[k+1], 1, -- );
1268 UPDATE_ACC01( src_top[k+2], 2, -- );
1270 UPDATE_ACC01( src_bottom[k], 0, ++ );
1271 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
1272 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
1278 for( k = 0; k < m*4; k += 4 )
1280 UPDATE_ACC01( src_top[k], 0, -- );
1281 UPDATE_ACC01( src_top[k+1], 1, -- );
1282 UPDATE_ACC01( src_top[k+2], 2, -- );
1283 UPDATE_ACC01( src_top[k+3], 3, -- );
1285 UPDATE_ACC01( src_bottom[k], 0, ++ );
1286 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
1287 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
1288 UPDATE_ACC01( src_bottom[k+3], 3, ++ );
1292 if( (src_step1 > 0 && src_bottom + src_step1 < src_max) ||
1293 (src_step1 < 0 && src_bottom + src_step1 >= src) )
1294 src_bottom += src_step1;
1297 src_top += src_step1;
1307 typedef uchar value_type;
1308 typedef int arg_type;
1310 arg_type load(const uchar* ptr) { return *ptr; }
1311 void store(uchar* ptr, arg_type val) { *ptr = (uchar)val; }
1312 void operator()(arg_type& a, arg_type& b) const
1314 int t = CV_FAST_CAST_8U(a - b);
1321 typedef ushort value_type;
1322 typedef int arg_type;
1324 arg_type load(const ushort* ptr) { return *ptr; }
1325 void store(ushort* ptr, arg_type val) { *ptr = (ushort)val; }
1326 void operator()(arg_type& a, arg_type& b) const
1336 typedef short value_type;
1337 typedef int arg_type;
1339 arg_type load(const short* ptr) { return *ptr; }
1340 void store(short* ptr, arg_type val) { *ptr = (short)val; }
1341 void operator()(arg_type& a, arg_type& b) const
1351 typedef float value_type;
1352 typedef float arg_type;
1354 arg_type load(const float* ptr) { return *ptr; }
1355 void store(float* ptr, arg_type val) { *ptr = val; }
1356 void operator()(arg_type& a, arg_type& b) const
1368 typedef uchar value_type;
1369 typedef __m128i arg_type;
1371 arg_type load(const uchar* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
1372 void store(uchar* ptr, arg_type val) { _mm_storeu_si128((__m128i*)ptr, val); }
1373 void operator()(arg_type& a, arg_type& b) const
1376 a = _mm_min_epu8(a, b);
1377 b = _mm_max_epu8(b, t);
1384 typedef ushort value_type;
1385 typedef __m128i arg_type;
1387 arg_type load(const ushort* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
1388 void store(ushort* ptr, arg_type val) { _mm_storeu_si128((__m128i*)ptr, val); }
1389 void operator()(arg_type& a, arg_type& b) const
1391 arg_type t = _mm_subs_epu16(a, b);
1392 a = _mm_subs_epu16(a, t);
1393 b = _mm_adds_epu16(b, t);
1400 typedef short value_type;
1401 typedef __m128i arg_type;
1403 arg_type load(const short* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
1404 void store(short* ptr, arg_type val) { _mm_storeu_si128((__m128i*)ptr, val); }
1405 void operator()(arg_type& a, arg_type& b) const
1408 a = _mm_min_epi16(a, b);
1409 b = _mm_max_epi16(b, t);
1416 typedef float value_type;
1417 typedef __m128 arg_type;
1419 arg_type load(const float* ptr) { return _mm_loadu_ps(ptr); }
1420 void store(float* ptr, arg_type val) { _mm_storeu_ps(ptr, val); }
1421 void operator()(arg_type& a, arg_type& b) const
1424 a = _mm_min_ps(a, b);
1425 b = _mm_max_ps(b, t);
1432 typedef MinMax8u MinMaxVec8u;
1433 typedef MinMax16u MinMaxVec16u;
1434 typedef MinMax16s MinMaxVec16s;
1435 typedef MinMax32f MinMaxVec32f;
1439 template<class Op, class VecOp>
1441 medianBlur_SortNet( const Mat& _src, Mat& _dst, int m )
1443 typedef typename Op::value_type T;
1444 typedef typename Op::arg_type WT;
1445 typedef typename VecOp::arg_type VT;
1447 const T* src = (const T*)_src.data;
1448 T* dst = (T*)_dst.data;
1449 int sstep = (int)(_src.step/sizeof(T));
1450 int dstep = (int)(_dst.step/sizeof(T));
1451 Size size = _dst.size();
1452 int i, j, k, cn = _src.channels();
1455 volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE2);
1459 if( size.width == 1 || size.height == 1 )
1461 int len = size.width + size.height - 1;
1462 int sdelta = size.height == 1 ? cn : sstep;
1463 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
1464 int ddelta = size.height == 1 ? cn : dstep;
1466 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
1467 for( j = 0; j < cn; j++, src++ )
1469 WT p0 = src[i > 0 ? -sdelta : 0];
1471 WT p2 = src[i < len - 1 ? sdelta : 0];
1473 op(p0, p1); op(p1, p2); op(p0, p1);
1480 for( i = 0; i < size.height; i++, dst += dstep )
1482 const T* row0 = src + std::max(i - 1, 0)*sstep;
1483 const T* row1 = src + i*sstep;
1484 const T* row2 = src + std::min(i + 1, size.height-1)*sstep;
1485 int limit = useSIMD ? cn : size.width;
1489 for( ; j < limit; j++ )
1491 int j0 = j >= cn ? j - cn : j;
1492 int j2 = j < size.width - cn ? j + cn : j;
1493 WT p0 = row0[j0], p1 = row0[j], p2 = row0[j2];
1494 WT p3 = row1[j0], p4 = row1[j], p5 = row1[j2];
1495 WT p6 = row2[j0], p7 = row2[j], p8 = row2[j2];
1497 op(p1, p2); op(p4, p5); op(p7, p8); op(p0, p1);
1498 op(p3, p4); op(p6, p7); op(p1, p2); op(p4, p5);
1499 op(p7, p8); op(p0, p3); op(p5, p8); op(p4, p7);
1500 op(p3, p6); op(p1, p4); op(p2, p5); op(p4, p7);
1501 op(p4, p2); op(p6, p4); op(p4, p2);
1505 if( limit == size.width )
1508 for( ; j <= size.width - VecOp::SIZE - cn; j += VecOp::SIZE )
1510 VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn);
1511 VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn);
1512 VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn);
1514 vop(p1, p2); vop(p4, p5); vop(p7, p8); vop(p0, p1);
1515 vop(p3, p4); vop(p6, p7); vop(p1, p2); vop(p4, p5);
1516 vop(p7, p8); vop(p0, p3); vop(p5, p8); vop(p4, p7);
1517 vop(p3, p6); vop(p1, p4); vop(p2, p5); vop(p4, p7);
1518 vop(p4, p2); vop(p6, p4); vop(p4, p2);
1519 vop.store(dst+j, p4);
1528 if( size.width == 1 || size.height == 1 )
1530 int len = size.width + size.height - 1;
1531 int sdelta = size.height == 1 ? cn : sstep;
1532 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
1533 int ddelta = size.height == 1 ? cn : dstep;
1535 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
1536 for( j = 0; j < cn; j++, src++ )
1538 int i1 = i > 0 ? -sdelta : 0;
1539 int i0 = i > 1 ? -sdelta*2 : i1;
1540 int i3 = i < len-1 ? sdelta : 0;
1541 int i4 = i < len-2 ? sdelta*2 : i3;
1542 WT p0 = src[i0], p1 = src[i1], p2 = src[0], p3 = src[i3], p4 = src[i4];
1544 op(p0, p1); op(p3, p4); op(p2, p3); op(p3, p4); op(p0, p2);
1545 op(p2, p4); op(p1, p3); op(p1, p2);
1552 for( i = 0; i < size.height; i++, dst += dstep )
1555 row[0] = src + std::max(i - 2, 0)*sstep;
1556 row[1] = src + std::max(i - 1, 0)*sstep;
1557 row[2] = src + i*sstep;
1558 row[3] = src + std::min(i + 1, size.height-1)*sstep;
1559 row[4] = src + std::min(i + 2, size.height-1)*sstep;
1560 int limit = useSIMD ? cn*2 : size.width;
1564 for( ; j < limit; j++ )
1567 int j1 = j >= cn ? j - cn : j;
1568 int j0 = j >= cn*2 ? j - cn*2 : j1;
1569 int j3 = j < size.width - cn ? j + cn : j;
1570 int j4 = j < size.width - cn*2 ? j + cn*2 : j3;
1571 for( k = 0; k < 5; k++ )
1573 const T* rowk = row[k];
1574 p[k*5] = rowk[j0]; p[k*5+1] = rowk[j1];
1575 p[k*5+2] = rowk[j]; p[k*5+3] = rowk[j3];
1576 p[k*5+4] = rowk[j4];
1579 op(p[1], p[2]); op(p[0], p[1]); op(p[1], p[2]); op(p[4], p[5]); op(p[3], p[4]);
1580 op(p[4], p[5]); op(p[0], p[3]); op(p[2], p[5]); op(p[2], p[3]); op(p[1], p[4]);
1581 op(p[1], p[2]); op(p[3], p[4]); op(p[7], p[8]); op(p[6], p[7]); op(p[7], p[8]);
1582 op(p[10], p[11]); op(p[9], p[10]); op(p[10], p[11]); op(p[6], p[9]); op(p[8], p[11]);
1583 op(p[8], p[9]); op(p[7], p[10]); op(p[7], p[8]); op(p[9], p[10]); op(p[0], p[6]);
1584 op(p[4], p[10]); op(p[4], p[6]); op(p[2], p[8]); op(p[2], p[4]); op(p[6], p[8]);
1585 op(p[1], p[7]); op(p[5], p[11]); op(p[5], p[7]); op(p[3], p[9]); op(p[3], p[5]);
1586 op(p[7], p[9]); op(p[1], p[2]); op(p[3], p[4]); op(p[5], p[6]); op(p[7], p[8]);
1587 op(p[9], p[10]); op(p[13], p[14]); op(p[12], p[13]); op(p[13], p[14]); op(p[16], p[17]);
1588 op(p[15], p[16]); op(p[16], p[17]); op(p[12], p[15]); op(p[14], p[17]); op(p[14], p[15]);
1589 op(p[13], p[16]); op(p[13], p[14]); op(p[15], p[16]); op(p[19], p[20]); op(p[18], p[19]);
1590 op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[21], p[23]); op(p[22], p[24]);
1591 op(p[22], p[23]); op(p[18], p[21]); op(p[20], p[23]); op(p[20], p[21]); op(p[19], p[22]);
1592 op(p[22], p[24]); op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[12], p[18]);
1593 op(p[16], p[22]); op(p[16], p[18]); op(p[14], p[20]); op(p[20], p[24]); op(p[14], p[16]);
1594 op(p[18], p[20]); op(p[22], p[24]); op(p[13], p[19]); op(p[17], p[23]); op(p[17], p[19]);
1595 op(p[15], p[21]); op(p[15], p[17]); op(p[19], p[21]); op(p[13], p[14]); op(p[15], p[16]);
1596 op(p[17], p[18]); op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[0], p[12]);
1597 op(p[8], p[20]); op(p[8], p[12]); op(p[4], p[16]); op(p[16], p[24]); op(p[12], p[16]);
1598 op(p[2], p[14]); op(p[10], p[22]); op(p[10], p[14]); op(p[6], p[18]); op(p[6], p[10]);
1599 op(p[10], p[12]); op(p[1], p[13]); op(p[9], p[21]); op(p[9], p[13]); op(p[5], p[17]);
1600 op(p[13], p[17]); op(p[3], p[15]); op(p[11], p[23]); op(p[11], p[15]); op(p[7], p[19]);
1601 op(p[7], p[11]); op(p[11], p[13]); op(p[11], p[12]);
1605 if( limit == size.width )
1608 for( ; j <= size.width - VecOp::SIZE - cn*2; j += VecOp::SIZE )
1611 for( k = 0; k < 5; k++ )
1613 const T* rowk = row[k];
1614 p[k*5] = vop.load(rowk+j-cn*2); p[k*5+1] = vop.load(rowk+j-cn);
1615 p[k*5+2] = vop.load(rowk+j); p[k*5+3] = vop.load(rowk+j+cn);
1616 p[k*5+4] = vop.load(rowk+j+cn*2);
1619 vop(p[1], p[2]); vop(p[0], p[1]); vop(p[1], p[2]); vop(p[4], p[5]); vop(p[3], p[4]);
1620 vop(p[4], p[5]); vop(p[0], p[3]); vop(p[2], p[5]); vop(p[2], p[3]); vop(p[1], p[4]);
1621 vop(p[1], p[2]); vop(p[3], p[4]); vop(p[7], p[8]); vop(p[6], p[7]); vop(p[7], p[8]);
1622 vop(p[10], p[11]); vop(p[9], p[10]); vop(p[10], p[11]); vop(p[6], p[9]); vop(p[8], p[11]);
1623 vop(p[8], p[9]); vop(p[7], p[10]); vop(p[7], p[8]); vop(p[9], p[10]); vop(p[0], p[6]);
1624 vop(p[4], p[10]); vop(p[4], p[6]); vop(p[2], p[8]); vop(p[2], p[4]); vop(p[6], p[8]);
1625 vop(p[1], p[7]); vop(p[5], p[11]); vop(p[5], p[7]); vop(p[3], p[9]); vop(p[3], p[5]);
1626 vop(p[7], p[9]); vop(p[1], p[2]); vop(p[3], p[4]); vop(p[5], p[6]); vop(p[7], p[8]);
1627 vop(p[9], p[10]); vop(p[13], p[14]); vop(p[12], p[13]); vop(p[13], p[14]); vop(p[16], p[17]);
1628 vop(p[15], p[16]); vop(p[16], p[17]); vop(p[12], p[15]); vop(p[14], p[17]); vop(p[14], p[15]);
1629 vop(p[13], p[16]); vop(p[13], p[14]); vop(p[15], p[16]); vop(p[19], p[20]); vop(p[18], p[19]);
1630 vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[21], p[23]); vop(p[22], p[24]);
1631 vop(p[22], p[23]); vop(p[18], p[21]); vop(p[20], p[23]); vop(p[20], p[21]); vop(p[19], p[22]);
1632 vop(p[22], p[24]); vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[12], p[18]);
1633 vop(p[16], p[22]); vop(p[16], p[18]); vop(p[14], p[20]); vop(p[20], p[24]); vop(p[14], p[16]);
1634 vop(p[18], p[20]); vop(p[22], p[24]); vop(p[13], p[19]); vop(p[17], p[23]); vop(p[17], p[19]);
1635 vop(p[15], p[21]); vop(p[15], p[17]); vop(p[19], p[21]); vop(p[13], p[14]); vop(p[15], p[16]);
1636 vop(p[17], p[18]); vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[0], p[12]);
1637 vop(p[8], p[20]); vop(p[8], p[12]); vop(p[4], p[16]); vop(p[16], p[24]); vop(p[12], p[16]);
1638 vop(p[2], p[14]); vop(p[10], p[22]); vop(p[10], p[14]); vop(p[6], p[18]); vop(p[6], p[10]);
1639 vop(p[10], p[12]); vop(p[1], p[13]); vop(p[9], p[21]); vop(p[9], p[13]); vop(p[5], p[17]);
1640 vop(p[13], p[17]); vop(p[3], p[15]); vop(p[11], p[23]); vop(p[11], p[15]); vop(p[7], p[19]);
1641 vop(p[7], p[11]); vop(p[11], p[13]); vop(p[11], p[12]);
1642 vop.store(dst+j, p[12]);
1653 void cv::medianBlur( InputArray _src0, OutputArray _dst, int ksize )
1655 Mat src0 = _src0.getMat();
1656 _dst.create( src0.size(), src0.type() );
1657 Mat dst = _dst.getMat();
1665 CV_Assert( ksize % 2 == 1 );
1667 #ifdef HAVE_TEGRA_OPTIMIZATION
1668 if (tegra::medianBlur(src0, dst, ksize))
1672 bool useSortNet = ksize == 3 || (ksize == 5
1674 && src0.depth() > CV_8U
1681 if( dst.data != src0.data )
1686 if( src.depth() == CV_8U )
1687 medianBlur_SortNet<MinMax8u, MinMaxVec8u>( src, dst, ksize );
1688 else if( src.depth() == CV_16U )
1689 medianBlur_SortNet<MinMax16u, MinMaxVec16u>( src, dst, ksize );
1690 else if( src.depth() == CV_16S )
1691 medianBlur_SortNet<MinMax16s, MinMaxVec16s>( src, dst, ksize );
1692 else if( src.depth() == CV_32F )
1693 medianBlur_SortNet<MinMax32f, MinMaxVec32f>( src, dst, ksize );
1695 CV_Error(CV_StsUnsupportedFormat, "");
1701 cv::copyMakeBorder( src0, src, 0, 0, ksize/2, ksize/2, BORDER_REPLICATE );
1703 int cn = src0.channels();
1704 CV_Assert( src.depth() == CV_8U && (cn == 1 || cn == 3 || cn == 4) );
1706 double img_size_mp = (double)(src0.total())/(1 << 20);
1707 if( ksize <= 3 + (img_size_mp < 1 ? 12 : img_size_mp < 4 ? 6 : 2)*(MEDIAN_HAVE_SIMD && checkHardwareSupport(CV_CPU_SSE2) ? 1 : 3))
1708 medianBlur_8u_Om( src, dst, ksize );
1710 medianBlur_8u_O1( src, dst, ksize );
1714 /****************************************************************************************\
1716 \****************************************************************************************/
1721 class BilateralFilter_8u_Invoker :
1722 public ParallelLoopBody
1725 BilateralFilter_8u_Invoker(Mat& _dest, const Mat& _temp, int _radius, int _maxk,
1726 int* _space_ofs, float *_space_weight, float *_color_weight) :
1727 temp(&_temp), dest(&_dest), radius(_radius),
1728 maxk(_maxk), space_ofs(_space_ofs), space_weight(_space_weight), color_weight(_color_weight)
1732 virtual void operator() (const Range& range) const
1734 int i, j, cn = dest->channels(), k;
1735 Size size = dest->size();
1737 int CV_DECL_ALIGNED(16) buf[4];
1738 float CV_DECL_ALIGNED(16) bufSum[4];
1739 static const int CV_DECL_ALIGNED(16) bufSignMask[] = { 0x80000000, 0x80000000, 0x80000000, 0x80000000 };
1740 bool haveSSE3 = checkHardwareSupport(CV_CPU_SSE3);
1743 for( i = range.start; i < range.end; i++ )
1745 const uchar* sptr = temp->ptr(i+radius) + radius*cn;
1746 uchar* dptr = dest->ptr(i);
1750 for( j = 0; j < size.width; j++ )
1752 float sum = 0, wsum = 0;
1758 __m128 _val0 = _mm_set1_ps(static_cast<float>(val0));
1759 const __m128 _signMask = _mm_load_ps((const float*)bufSignMask);
1761 for( ; k <= maxk - 4; k += 4 )
1763 __m128 _valF = _mm_set_ps(sptr[j + space_ofs[k+3]], sptr[j + space_ofs[k+2]],
1764 sptr[j + space_ofs[k+1]], sptr[j + space_ofs[k]]);
1766 __m128 _val = _mm_andnot_ps(_signMask, _mm_sub_ps(_valF, _val0));
1767 _mm_store_si128((__m128i*)buf, _mm_cvtps_epi32(_val));
1769 __m128 _cw = _mm_set_ps(color_weight[buf[3]],color_weight[buf[2]],
1770 color_weight[buf[1]],color_weight[buf[0]]);
1771 __m128 _sw = _mm_loadu_ps(space_weight+k);
1772 __m128 _w = _mm_mul_ps(_cw, _sw);
1773 _cw = _mm_mul_ps(_w, _valF);
1775 _sw = _mm_hadd_ps(_w, _cw);
1776 _sw = _mm_hadd_ps(_sw, _sw);
1777 _mm_storel_pi((__m64*)bufSum, _sw);
1784 for( ; k < maxk; k++ )
1786 int val = sptr[j + space_ofs[k]];
1787 float w = space_weight[k]*color_weight[std::abs(val - val0)];
1791 // overflow is not possible here => there is no need to use CV_CAST_8U
1792 dptr[j] = (uchar)cvRound(sum/wsum);
1798 for( j = 0; j < size.width*3; j += 3 )
1800 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
1801 int b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
1806 const __m128 _b0 = _mm_set1_ps(static_cast<float>(b0));
1807 const __m128 _g0 = _mm_set1_ps(static_cast<float>(g0));
1808 const __m128 _r0 = _mm_set1_ps(static_cast<float>(r0));
1809 const __m128 _signMask = _mm_load_ps((const float*)bufSignMask);
1811 for( ; k <= maxk - 4; k += 4 )
1813 const uchar* sptr_k = sptr + j + space_ofs[k];
1814 const uchar* sptr_k1 = sptr + j + space_ofs[k+1];
1815 const uchar* sptr_k2 = sptr + j + space_ofs[k+2];
1816 const uchar* sptr_k3 = sptr + j + space_ofs[k+3];
1818 __m128 _b = _mm_set_ps(sptr_k3[0],sptr_k2[0],sptr_k1[0],sptr_k[0]);
1819 __m128 _g = _mm_set_ps(sptr_k3[1],sptr_k2[1],sptr_k1[1],sptr_k[1]);
1820 __m128 _r = _mm_set_ps(sptr_k3[2],sptr_k2[2],sptr_k1[2],sptr_k[2]);
1822 __m128 bt = _mm_andnot_ps(_signMask, _mm_sub_ps(_b,_b0));
1823 __m128 gt = _mm_andnot_ps(_signMask, _mm_sub_ps(_g,_g0));
1824 __m128 rt = _mm_andnot_ps(_signMask, _mm_sub_ps(_r,_r0));
1826 bt =_mm_add_ps(rt, _mm_add_ps(bt, gt));
1827 _mm_store_si128((__m128i*)buf, _mm_cvtps_epi32(bt));
1829 __m128 _w = _mm_set_ps(color_weight[buf[3]],color_weight[buf[2]],
1830 color_weight[buf[1]],color_weight[buf[0]]);
1831 __m128 _sw = _mm_loadu_ps(space_weight+k);
1833 _w = _mm_mul_ps(_w,_sw);
1834 _b = _mm_mul_ps(_b, _w);
1835 _g = _mm_mul_ps(_g, _w);
1836 _r = _mm_mul_ps(_r, _w);
1838 _w = _mm_hadd_ps(_w, _b);
1839 _g = _mm_hadd_ps(_g, _r);
1841 _w = _mm_hadd_ps(_w, _g);
1842 _mm_store_ps(bufSum, _w);
1852 for( ; k < maxk; k++ )
1854 const uchar* sptr_k = sptr + j + space_ofs[k];
1855 int b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
1856 float w = space_weight[k]*color_weight[std::abs(b - b0) +
1857 std::abs(g - g0) + std::abs(r - r0)];
1858 sum_b += b*w; sum_g += g*w; sum_r += r*w;
1862 b0 = cvRound(sum_b*wsum);
1863 g0 = cvRound(sum_g*wsum);
1864 r0 = cvRound(sum_r*wsum);
1865 dptr[j] = (uchar)b0; dptr[j+1] = (uchar)g0; dptr[j+2] = (uchar)r0;
1874 int radius, maxk, *space_ofs;
1875 float *space_weight, *color_weight;
1879 bilateralFilter_8u( const Mat& src, Mat& dst, int d,
1880 double sigma_color, double sigma_space,
1884 int cn = src.channels();
1885 int i, j, maxk, radius;
1886 Size size = src.size();
1888 CV_Assert( (src.type() == CV_8UC1 || src.type() == CV_8UC3) &&
1889 src.type() == dst.type() && src.size() == dst.size() &&
1890 src.data != dst.data );
1892 if( sigma_color <= 0 )
1894 if( sigma_space <= 0 )
1897 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
1898 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
1901 radius = cvRound(sigma_space*1.5);
1904 radius = MAX(radius, 1);
1907 #if 0 && defined HAVE_IPP && (IPP_VERSION_MAJOR >= 7)
1910 IppiSize kernel = {d, d};
1911 IppiSize roi={src.cols, src.rows};
1913 ippiFilterBilateralGetBufSize_8u_C1R( ippiFilterBilateralGauss, roi, kernel, &bufsize);
1914 AutoBuffer<uchar> buf(bufsize+128);
1915 IppiFilterBilateralSpec *pSpec = (IppiFilterBilateralSpec *)alignPtr(&buf[0], 32);
1916 ippiFilterBilateralInit_8u_C1R( ippiFilterBilateralGauss, kernel, sigma_color*sigma_color, sigma_space*sigma_space, 1, pSpec );
1918 const Mat* psrc = &src;
1919 if( src.data == dst.data )
1924 if( ippiFilterBilateral_8u_C1R(psrc->data, (int)psrc->step[0],
1925 dst.data, (int)dst.step[0],
1926 roi, kernel, pSpec) >= 0 )
1931 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
1933 vector<float> _color_weight(cn*256);
1934 vector<float> _space_weight(d*d);
1935 vector<int> _space_ofs(d*d);
1936 float* color_weight = &_color_weight[0];
1937 float* space_weight = &_space_weight[0];
1938 int* space_ofs = &_space_ofs[0];
1940 // initialize color-related bilateral filter coefficients
1942 for( i = 0; i < 256*cn; i++ )
1943 color_weight[i] = (float)std::exp(i*i*gauss_color_coeff);
1945 // initialize space-related bilateral filter coefficients
1946 for( i = -radius, maxk = 0; i <= radius; i++ )
1950 for( ;j <= radius; j++ )
1952 double r = std::sqrt((double)i*i + (double)j*j);
1955 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
1956 space_ofs[maxk++] = (int)(i*temp.step + j*cn);
1960 BilateralFilter_8u_Invoker body(dst, temp, radius, maxk, space_ofs, space_weight, color_weight);
1961 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
1965 class BilateralFilter_32f_Invoker :
1966 public ParallelLoopBody
1970 BilateralFilter_32f_Invoker(int _cn, int _radius, int _maxk, int *_space_ofs,
1971 const Mat& _temp, Mat& _dest, float _scale_index, float *_space_weight, float *_expLUT) :
1972 cn(_cn), radius(_radius), maxk(_maxk), space_ofs(_space_ofs),
1973 temp(&_temp), dest(&_dest), scale_index(_scale_index), space_weight(_space_weight), expLUT(_expLUT)
1977 virtual void operator() (const Range& range) const
1980 Size size = dest->size();
1982 int CV_DECL_ALIGNED(16) idxBuf[4];
1983 float CV_DECL_ALIGNED(16) bufSum32[4];
1984 static const int CV_DECL_ALIGNED(16) bufSignMask[] = { 0x80000000, 0x80000000, 0x80000000, 0x80000000 };
1985 bool haveSSE3 = checkHardwareSupport(CV_CPU_SSE3);
1988 for( i = range.start; i < range.end; i++ )
1990 const float* sptr = temp->ptr<float>(i+radius) + radius*cn;
1991 float* dptr = dest->ptr<float>(i);
1995 for( j = 0; j < size.width; j++ )
1997 float sum = 0, wsum = 0;
1998 float val0 = sptr[j];
2003 const __m128 _val0 = _mm_set1_ps(sptr[j]);
2004 const __m128 _scale_index = _mm_set1_ps(scale_index);
2005 const __m128 _signMask = _mm_load_ps((const float*)bufSignMask);
2007 for( ; k <= maxk - 4 ; k += 4 )
2009 __m128 _sw = _mm_loadu_ps(space_weight + k);
2010 __m128 _val = _mm_set_ps(sptr[j + space_ofs[k+3]], sptr[j + space_ofs[k+2]],
2011 sptr[j + space_ofs[k+1]], sptr[j + space_ofs[k]]);
2012 __m128 _alpha = _mm_mul_ps(_mm_andnot_ps( _signMask, _mm_sub_ps(_val,_val0)), _scale_index);
2014 __m128i _idx = _mm_cvtps_epi32(_alpha);
2015 _mm_store_si128((__m128i*)idxBuf, _idx);
2016 _alpha = _mm_sub_ps(_alpha, _mm_cvtepi32_ps(_idx));
2018 __m128 _explut = _mm_set_ps(expLUT[idxBuf[3]], expLUT[idxBuf[2]],
2019 expLUT[idxBuf[1]], expLUT[idxBuf[0]]);
2020 __m128 _explut1 = _mm_set_ps(expLUT[idxBuf[3]+1], expLUT[idxBuf[2]+1],
2021 expLUT[idxBuf[1]+1], expLUT[idxBuf[0]+1]);
2023 __m128 _w = _mm_mul_ps(_sw, _mm_add_ps(_explut, _mm_mul_ps(_alpha, _mm_sub_ps(_explut1, _explut))));
2024 _val = _mm_mul_ps(_w, _val);
2026 _sw = _mm_hadd_ps(_w, _val);
2027 _sw = _mm_hadd_ps(_sw, _sw);
2028 _mm_storel_pi((__m64*)bufSum32, _sw);
2031 wsum += bufSum32[0];
2036 for( ; k < maxk; k++ )
2038 float val = sptr[j + space_ofs[k]];
2039 float alpha = (float)(std::abs(val - val0)*scale_index);
2040 int idx = cvFloor(alpha);
2042 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
2046 dptr[j] = (float)(sum/wsum);
2052 for( j = 0; j < size.width*3; j += 3 )
2054 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
2055 float b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
2060 const __m128 _b0 = _mm_set1_ps(b0);
2061 const __m128 _g0 = _mm_set1_ps(g0);
2062 const __m128 _r0 = _mm_set1_ps(r0);
2063 const __m128 _scale_index = _mm_set1_ps(scale_index);
2064 const __m128 _signMask = _mm_load_ps((const float*)bufSignMask);
2066 for( ; k <= maxk-4; k += 4 )
2068 __m128 _sw = _mm_loadu_ps(space_weight + k);
2070 const float* sptr_k = sptr + j + space_ofs[k];
2071 const float* sptr_k1 = sptr + j + space_ofs[k+1];
2072 const float* sptr_k2 = sptr + j + space_ofs[k+2];
2073 const float* sptr_k3 = sptr + j + space_ofs[k+3];
2075 __m128 _b = _mm_set_ps(sptr_k3[0], sptr_k2[0], sptr_k1[0], sptr_k[0]);
2076 __m128 _g = _mm_set_ps(sptr_k3[1], sptr_k2[1], sptr_k1[1], sptr_k[1]);
2077 __m128 _r = _mm_set_ps(sptr_k3[2], sptr_k2[2], sptr_k1[2], sptr_k[2]);
2079 __m128 _bt = _mm_andnot_ps(_signMask,_mm_sub_ps(_b,_b0));
2080 __m128 _gt = _mm_andnot_ps(_signMask,_mm_sub_ps(_g,_g0));
2081 __m128 _rt = _mm_andnot_ps(_signMask,_mm_sub_ps(_r,_r0));
2083 __m128 _alpha = _mm_mul_ps(_scale_index, _mm_add_ps(_rt,_mm_add_ps(_bt, _gt)));
2085 __m128i _idx = _mm_cvtps_epi32(_alpha);
2086 _mm_store_si128((__m128i*)idxBuf, _idx);
2087 _alpha = _mm_sub_ps(_alpha, _mm_cvtepi32_ps(_idx));
2089 __m128 _explut = _mm_set_ps(expLUT[idxBuf[3]], expLUT[idxBuf[2]], expLUT[idxBuf[1]], expLUT[idxBuf[0]]);
2090 __m128 _explut1 = _mm_set_ps(expLUT[idxBuf[3]+1], expLUT[idxBuf[2]+1], expLUT[idxBuf[1]+1], expLUT[idxBuf[0]+1]);
2092 __m128 _w = _mm_mul_ps(_sw, _mm_add_ps(_explut, _mm_mul_ps(_alpha, _mm_sub_ps(_explut1, _explut))));
2094 _b = _mm_mul_ps(_b, _w);
2095 _g = _mm_mul_ps(_g, _w);
2096 _r = _mm_mul_ps(_r, _w);
2098 _w = _mm_hadd_ps(_w, _b);
2099 _g = _mm_hadd_ps(_g, _r);
2101 _w = _mm_hadd_ps(_w, _g);
2102 _mm_store_ps(bufSum32, _w);
2104 wsum += bufSum32[0];
2105 sum_b += bufSum32[1];
2106 sum_g += bufSum32[2];
2107 sum_r += bufSum32[3];
2113 for(; k < maxk; k++ )
2115 const float* sptr_k = sptr + j + space_ofs[k];
2116 float b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
2117 float alpha = (float)((std::abs(b - b0) +
2118 std::abs(g - g0) + std::abs(r - r0))*scale_index);
2119 int idx = cvFloor(alpha);
2121 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
2122 sum_b += b*w; sum_g += g*w; sum_r += r*w;
2129 dptr[j] = b0; dptr[j+1] = g0; dptr[j+2] = r0;
2136 int cn, radius, maxk, *space_ofs;
2139 float scale_index, *space_weight, *expLUT;
2144 bilateralFilter_32f( const Mat& src, Mat& dst, int d,
2145 double sigma_color, double sigma_space,
2148 int cn = src.channels();
2149 int i, j, maxk, radius;
2150 double minValSrc=-1, maxValSrc=1;
2151 const int kExpNumBinsPerChannel = 1 << 12;
2152 int kExpNumBins = 0;
2153 float lastExpVal = 1.f;
2154 float len, scale_index;
2155 Size size = src.size();
2157 CV_Assert( (src.type() == CV_32FC1 || src.type() == CV_32FC3) &&
2158 src.type() == dst.type() && src.size() == dst.size() &&
2159 src.data != dst.data );
2161 if( sigma_color <= 0 )
2163 if( sigma_space <= 0 )
2166 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
2167 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
2170 radius = cvRound(sigma_space*1.5);
2173 radius = MAX(radius, 1);
2175 // compute the min/max range for the input image (even if multichannel)
2177 minMaxLoc( src.reshape(1), &minValSrc, &maxValSrc );
2178 if(std::abs(minValSrc - maxValSrc) < FLT_EPSILON)
2184 // temporary copy of the image with borders for easy processing
2186 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
2187 const double insteadNaNValue = -5. * sigma_color;
2188 patchNaNs( temp, insteadNaNValue ); // this replacement of NaNs makes the assumption that depth values are nonnegative
2189 // TODO: make insteadNaNValue avalible in the outside function interface to control the cases breaking the assumption
2190 // allocate lookup tables
2191 vector<float> _space_weight(d*d);
2192 vector<int> _space_ofs(d*d);
2193 float* space_weight = &_space_weight[0];
2194 int* space_ofs = &_space_ofs[0];
2196 // assign a length which is slightly more than needed
2197 len = (float)(maxValSrc - minValSrc) * cn;
2198 kExpNumBins = kExpNumBinsPerChannel * cn;
2199 vector<float> _expLUT(kExpNumBins+2);
2200 float* expLUT = &_expLUT[0];
2202 scale_index = kExpNumBins/len;
2204 // initialize the exp LUT
2205 for( i = 0; i < kExpNumBins+2; i++ )
2207 if( lastExpVal > 0.f )
2209 double val = i / scale_index;
2210 expLUT[i] = (float)std::exp(val * val * gauss_color_coeff);
2211 lastExpVal = expLUT[i];
2217 // initialize space-related bilateral filter coefficients
2218 for( i = -radius, maxk = 0; i <= radius; i++ )
2219 for( j = -radius; j <= radius; j++ )
2221 double r = std::sqrt((double)i*i + (double)j*j);
2224 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
2225 space_ofs[maxk++] = (int)(i*(temp.step/sizeof(float)) + j*cn);
2228 // parallel_for usage
2230 BilateralFilter_32f_Invoker body(cn, radius, maxk, space_ofs, temp, dst, scale_index, space_weight, expLUT);
2231 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
2236 void cv::bilateralFilter( InputArray _src, OutputArray _dst, int d,
2237 double sigmaColor, double sigmaSpace,
2240 Mat src = _src.getMat();
2241 _dst.create( src.size(), src.type() );
2242 Mat dst = _dst.getMat();
2244 if( src.depth() == CV_8U )
2245 bilateralFilter_8u( src, dst, d, sigmaColor, sigmaSpace, borderType );
2246 else if( src.depth() == CV_32F )
2247 bilateralFilter_32f( src, dst, d, sigmaColor, sigmaSpace, borderType );
2249 CV_Error( CV_StsUnsupportedFormat,
2250 "Bilateral filtering is only implemented for 8u and 32f images" );
2253 //////////////////////////////////////////////////////////////////////////////////////////
2256 cvSmooth( const void* srcarr, void* dstarr, int smooth_type,
2257 int param1, int param2, double param3, double param4 )
2259 cv::Mat src = cv::cvarrToMat(srcarr), dst0 = cv::cvarrToMat(dstarr), dst = dst0;
2261 CV_Assert( dst.size() == src.size() &&
2262 (smooth_type == CV_BLUR_NO_SCALE || dst.type() == src.type()) );
2267 if( smooth_type == CV_BLUR || smooth_type == CV_BLUR_NO_SCALE )
2268 cv::boxFilter( src, dst, dst.depth(), cv::Size(param1, param2), cv::Point(-1,-1),
2269 smooth_type == CV_BLUR, cv::BORDER_REPLICATE );
2270 else if( smooth_type == CV_GAUSSIAN )
2271 cv::GaussianBlur( src, dst, cv::Size(param1, param2), param3, param4, cv::BORDER_REPLICATE );
2272 else if( smooth_type == CV_MEDIAN )
2273 cv::medianBlur( src, dst, param1 );
2275 cv::bilateralFilter( src, dst, param1, param3, param4, cv::BORDER_REPLICATE );
2277 if( dst.data != dst0.data )
2278 CV_Error( CV_StsUnmatchedFormats, "The destination image does not have the proper type" );