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 // Copyright (C) 2014-2015, Itseez Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
24 // * Redistribution's in binary form must reproduce the above copyright notice,
25 // this list of conditions and the following disclaimer in the documentation
26 // and/or other materials provided with the distribution.
28 // * The name of the copyright holders may not be used to endorse or promote products
29 // derived from this software without specific prior written permission.
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
44 #include "precomp.hpp"
48 #include "opencv2/core/hal/intrin.hpp"
49 #include "opencl_kernels_imgproc.hpp"
51 #include "opencv2/core/openvx/ovx_defs.hpp"
55 #include "fixedpoint.inl.hpp"
57 * This file includes the code, contributed by Simon Perreault
58 * (the function icvMedianBlur_8u_O1)
60 * Constant-time median filtering -- http://nomis80.org/ctmf.html
61 * Copyright (C) 2006 Simon Perreault
64 * Laboratoire de vision et systemes numeriques
65 * Pavillon Adrien-Pouliot
67 * Sainte-Foy, Quebec, Canada
70 * perreaul@gel.ulaval.ca
76 /****************************************************************************************\
78 \****************************************************************************************/
80 template<typename T, typename ST>
84 RowSum( int _ksize, int _anchor ) :
91 virtual void operator()(const uchar* src, uchar* dst, int width, int cn) CV_OVERRIDE
93 const T* S = (const T*)src;
95 int i = 0, k, ksz_cn = ksize*cn;
97 width = (width - 1)*cn;
100 for( i = 0; i < width + cn; i++ )
102 D[i] = (ST)S[i] + (ST)S[i+cn] + (ST)S[i+cn*2];
105 else if( ksize == 5 )
107 for( i = 0; i < width + cn; i++ )
109 D[i] = (ST)S[i] + (ST)S[i+cn] + (ST)S[i+cn*2] + (ST)S[i + cn*3] + (ST)S[i + cn*4];
115 for( i = 0; i < ksz_cn; i++ )
118 for( i = 0; i < width; i++ )
120 s += (ST)S[i + ksz_cn] - (ST)S[i];
126 ST s0 = 0, s1 = 0, s2 = 0;
127 for( i = 0; i < ksz_cn; i += 3 )
136 for( i = 0; i < width; i += 3 )
138 s0 += (ST)S[i + ksz_cn] - (ST)S[i];
139 s1 += (ST)S[i + ksz_cn + 1] - (ST)S[i + 1];
140 s2 += (ST)S[i + ksz_cn + 2] - (ST)S[i + 2];
148 ST s0 = 0, s1 = 0, s2 = 0, s3 = 0;
149 for( i = 0; i < ksz_cn; i += 4 )
160 for( i = 0; i < width; i += 4 )
162 s0 += (ST)S[i + ksz_cn] - (ST)S[i];
163 s1 += (ST)S[i + ksz_cn + 1] - (ST)S[i + 1];
164 s2 += (ST)S[i + ksz_cn + 2] - (ST)S[i + 2];
165 s3 += (ST)S[i + ksz_cn + 3] - (ST)S[i + 3];
173 for( k = 0; k < cn; k++, S++, D++ )
176 for( i = 0; i < ksz_cn; i += cn )
179 for( i = 0; i < width; i += cn )
181 s += (ST)S[i + ksz_cn] - (ST)S[i];
189 template<typename ST, typename T>
191 public BaseColumnFilter
193 ColumnSum( int _ksize, int _anchor, double _scale ) :
202 virtual void reset() CV_OVERRIDE { sumCount = 0; }
204 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
208 bool haveScale = scale != 1;
209 double _scale = scale;
211 if( width != (int)sum.size() )
220 memset((void*)SUM, 0, width*sizeof(ST));
222 for( ; sumCount < ksize - 1; sumCount++, src++ )
224 const ST* Sp = (const ST*)src[0];
226 for( i = 0; i < width; i++ )
232 CV_Assert( sumCount == ksize-1 );
236 for( ; count--; src++ )
238 const ST* Sp = (const ST*)src[0];
239 const ST* Sm = (const ST*)src[1-ksize];
243 for( i = 0; i <= width - 2; i += 2 )
245 ST s0 = SUM[i] + Sp[i], s1 = SUM[i+1] + Sp[i+1];
246 D[i] = saturate_cast<T>(s0*_scale);
247 D[i+1] = saturate_cast<T>(s1*_scale);
248 s0 -= Sm[i]; s1 -= Sm[i+1];
249 SUM[i] = s0; SUM[i+1] = s1;
252 for( ; i < width; i++ )
254 ST s0 = SUM[i] + Sp[i];
255 D[i] = saturate_cast<T>(s0*_scale);
261 for( i = 0; i <= width - 2; i += 2 )
263 ST s0 = SUM[i] + Sp[i], s1 = SUM[i+1] + Sp[i+1];
264 D[i] = saturate_cast<T>(s0);
265 D[i+1] = saturate_cast<T>(s1);
266 s0 -= Sm[i]; s1 -= Sm[i+1];
267 SUM[i] = s0; SUM[i+1] = s1;
270 for( ; i < width; i++ )
272 ST s0 = SUM[i] + Sp[i];
273 D[i] = saturate_cast<T>(s0);
288 struct ColumnSum<int, uchar> :
289 public BaseColumnFilter
291 ColumnSum( int _ksize, int _anchor, double _scale ) :
300 virtual void reset() CV_OVERRIDE { sumCount = 0; }
302 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
305 bool haveScale = scale != 1;
306 double _scale = scale;
309 bool haveSIMD128 = hasSIMD128();
312 if( width != (int)sum.size() )
321 memset((void*)SUM, 0, width*sizeof(int));
322 for( ; sumCount < ksize - 1; sumCount++, src++ )
324 const int* Sp = (const int*)src[0];
329 for (; i <= width - 4; i += 4)
331 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
335 for( ; i < width; i++ )
341 CV_Assert( sumCount == ksize-1 );
345 for( ; count--; src++ )
347 const int* Sp = (const int*)src[0];
348 const int* Sm = (const int*)src[1-ksize];
349 uchar* D = (uchar*)dst;
357 v_float32x4 v_scale = v_setall_f32((float)_scale);
358 for( ; i <= width-8; i+=8 )
360 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
361 v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
363 v_uint32x4 v_s0d = v_reinterpret_as_u32(v_round(v_cvt_f32(v_s0) * v_scale));
364 v_uint32x4 v_s01d = v_reinterpret_as_u32(v_round(v_cvt_f32(v_s01) * v_scale));
366 v_uint16x8 v_dst = v_pack(v_s0d, v_s01d);
367 v_pack_store(D + i, v_dst);
369 v_store(SUM + i, v_s0 - v_load(Sm + i));
370 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
374 for( ; i < width; i++ )
376 int s0 = SUM[i] + Sp[i];
377 D[i] = saturate_cast<uchar>(s0*_scale);
387 for( ; i <= width-8; i+=8 )
389 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
390 v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
392 v_uint16x8 v_dst = v_pack(v_reinterpret_as_u32(v_s0), v_reinterpret_as_u32(v_s01));
393 v_pack_store(D + i, v_dst);
395 v_store(SUM + i, v_s0 - v_load(Sm + i));
396 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
401 for( ; i < width; i++ )
403 int s0 = SUM[i] + Sp[i];
404 D[i] = saturate_cast<uchar>(s0);
414 std::vector<int> sum;
419 struct ColumnSum<ushort, uchar> :
420 public BaseColumnFilter
424 ColumnSum( int _ksize, int _anchor, double _scale ) :
435 int d = cvRound(1./scale);
436 double scalef = ((double)(1 << SHIFT))/d;
437 divScale = cvFloor(scalef);
447 virtual void reset() CV_OVERRIDE { sumCount = 0; }
449 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
451 const int ds = divScale;
452 const int dd = divDelta;
454 const bool haveScale = scale != 1;
457 bool haveSIMD128 = hasSIMD128();
460 if( width != (int)sum.size() )
469 memset((void*)SUM, 0, width*sizeof(SUM[0]));
470 for( ; sumCount < ksize - 1; sumCount++, src++ )
472 const ushort* Sp = (const ushort*)src[0];
477 for( ; i <= width - 8; i += 8 )
479 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
483 for( ; i < width; i++ )
489 CV_Assert( sumCount == ksize-1 );
493 for( ; count--; src++ )
495 const ushort* Sp = (const ushort*)src[0];
496 const ushort* Sm = (const ushort*)src[1-ksize];
497 uchar* D = (uchar*)dst;
502 v_uint32x4 ds4 = v_setall_u32((unsigned)ds);
503 v_uint16x8 dd8 = v_setall_u16((ushort)dd);
505 for( ; i <= width-16; i+=16 )
507 v_uint16x8 _sm0 = v_load(Sm + i);
508 v_uint16x8 _sm1 = v_load(Sm + i + 8);
510 v_uint16x8 _s0 = v_add_wrap(v_load(SUM + i), v_load(Sp + i));
511 v_uint16x8 _s1 = v_add_wrap(v_load(SUM + i + 8), v_load(Sp + i + 8));
513 v_uint32x4 _s00, _s01, _s10, _s11;
515 v_expand(_s0 + dd8, _s00, _s01);
516 v_expand(_s1 + dd8, _s10, _s11);
518 _s00 = v_shr<SHIFT>(_s00*ds4);
519 _s01 = v_shr<SHIFT>(_s01*ds4);
520 _s10 = v_shr<SHIFT>(_s10*ds4);
521 _s11 = v_shr<SHIFT>(_s11*ds4);
523 v_int16x8 r0 = v_pack(v_reinterpret_as_s32(_s00), v_reinterpret_as_s32(_s01));
524 v_int16x8 r1 = v_pack(v_reinterpret_as_s32(_s10), v_reinterpret_as_s32(_s11));
526 _s0 = v_sub_wrap(_s0, _sm0);
527 _s1 = v_sub_wrap(_s1, _sm1);
529 v_store(D + i, v_pack_u(r0, r1));
530 v_store(SUM + i, _s0);
531 v_store(SUM + i + 8, _s1);
534 for( ; i < width; i++ )
536 int s0 = SUM[i] + Sp[i];
537 D[i] = (uchar)((s0 + dd)*ds >> SHIFT);
538 SUM[i] = (ushort)(s0 - Sm[i]);
544 for( ; i < width; i++ )
546 int s0 = SUM[i] + Sp[i];
547 D[i] = saturate_cast<uchar>(s0);
548 SUM[i] = (ushort)(s0 - Sm[i]);
559 std::vector<ushort> sum;
564 struct ColumnSum<int, short> :
565 public BaseColumnFilter
567 ColumnSum( int _ksize, int _anchor, double _scale ) :
576 virtual void reset() CV_OVERRIDE { sumCount = 0; }
578 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
582 bool haveScale = scale != 1;
583 double _scale = scale;
586 bool haveSIMD128 = hasSIMD128();
589 if( width != (int)sum.size() )
598 memset((void*)SUM, 0, width*sizeof(int));
599 for( ; sumCount < ksize - 1; sumCount++, src++ )
601 const int* Sp = (const int*)src[0];
606 for( ; i <= width - 4; i+=4 )
608 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
612 for( ; i < width; i++ )
618 CV_Assert( sumCount == ksize-1 );
622 for( ; count--; src++ )
624 const int* Sp = (const int*)src[0];
625 const int* Sm = (const int*)src[1-ksize];
626 short* D = (short*)dst;
633 v_float32x4 v_scale = v_setall_f32((float)_scale);
634 for( ; i <= width-8; i+=8 )
636 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
637 v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
639 v_int32x4 v_s0d = v_round(v_cvt_f32(v_s0) * v_scale);
640 v_int32x4 v_s01d = v_round(v_cvt_f32(v_s01) * v_scale);
641 v_store(D + i, v_pack(v_s0d, v_s01d));
643 v_store(SUM + i, v_s0 - v_load(Sm + i));
644 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
648 for( ; i < width; i++ )
650 int s0 = SUM[i] + Sp[i];
651 D[i] = saturate_cast<short>(s0*_scale);
661 for( ; i <= width-8; i+=8 )
663 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
664 v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
666 v_store(D + i, v_pack(v_s0, v_s01));
668 v_store(SUM + i, v_s0 - v_load(Sm + i));
669 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
674 for( ; i < width; i++ )
676 int s0 = SUM[i] + Sp[i];
677 D[i] = saturate_cast<short>(s0);
687 std::vector<int> sum;
692 struct ColumnSum<int, ushort> :
693 public BaseColumnFilter
695 ColumnSum( int _ksize, int _anchor, double _scale ) :
704 virtual void reset() CV_OVERRIDE { sumCount = 0; }
706 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
709 bool haveScale = scale != 1;
710 double _scale = scale;
713 bool haveSIMD128 = hasSIMD128();
716 if( width != (int)sum.size() )
725 memset((void*)SUM, 0, width*sizeof(int));
726 for( ; sumCount < ksize - 1; sumCount++, src++ )
728 const int* Sp = (const int*)src[0];
733 for (; i <= width - 4; i += 4)
735 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
739 for( ; i < width; i++ )
745 CV_Assert( sumCount == ksize-1 );
749 for( ; count--; src++ )
751 const int* Sp = (const int*)src[0];
752 const int* Sm = (const int*)src[1-ksize];
753 ushort* D = (ushort*)dst;
760 v_float32x4 v_scale = v_setall_f32((float)_scale);
761 for( ; i <= width-8; i+=8 )
763 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
764 v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
766 v_uint32x4 v_s0d = v_reinterpret_as_u32(v_round(v_cvt_f32(v_s0) * v_scale));
767 v_uint32x4 v_s01d = v_reinterpret_as_u32(v_round(v_cvt_f32(v_s01) * v_scale));
768 v_store(D + i, v_pack(v_s0d, v_s01d));
770 v_store(SUM + i, v_s0 - v_load(Sm + i));
771 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
775 for( ; i < width; i++ )
777 int s0 = SUM[i] + Sp[i];
778 D[i] = saturate_cast<ushort>(s0*_scale);
788 for( ; i <= width-8; i+=8 )
790 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
791 v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
793 v_store(D + i, v_pack(v_reinterpret_as_u32(v_s0), v_reinterpret_as_u32(v_s01)));
795 v_store(SUM + i, v_s0 - v_load(Sm + i));
796 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
800 for( ; i < width; i++ )
802 int s0 = SUM[i] + Sp[i];
803 D[i] = saturate_cast<ushort>(s0);
813 std::vector<int> sum;
817 struct ColumnSum<int, int> :
818 public BaseColumnFilter
820 ColumnSum( int _ksize, int _anchor, double _scale ) :
829 virtual void reset() CV_OVERRIDE { sumCount = 0; }
831 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
834 bool haveScale = scale != 1;
835 double _scale = scale;
838 bool haveSIMD128 = hasSIMD128();
841 if( width != (int)sum.size() )
850 memset((void*)SUM, 0, width*sizeof(int));
851 for( ; sumCount < ksize - 1; sumCount++, src++ )
853 const int* Sp = (const int*)src[0];
858 for( ; i <= width - 4; i+=4 )
860 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
864 for( ; i < width; i++ )
870 CV_Assert( sumCount == ksize-1 );
874 for( ; count--; src++ )
876 const int* Sp = (const int*)src[0];
877 const int* Sm = (const int*)src[1-ksize];
885 v_float32x4 v_scale = v_setall_f32((float)_scale);
886 for( ; i <= width-4; i+=4 )
888 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
889 v_int32x4 v_s0d = v_round(v_cvt_f32(v_s0) * v_scale);
891 v_store(D + i, v_s0d);
892 v_store(SUM + i, v_s0 - v_load(Sm + i));
896 for( ; i < width; i++ )
898 int s0 = SUM[i] + Sp[i];
899 D[i] = saturate_cast<int>(s0*_scale);
909 for( ; i <= width-4; i+=4 )
911 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
913 v_store(D + i, v_s0);
914 v_store(SUM + i, v_s0 - v_load(Sm + i));
918 for( ; i < width; i++ )
920 int s0 = SUM[i] + Sp[i];
931 std::vector<int> sum;
936 struct ColumnSum<int, float> :
937 public BaseColumnFilter
939 ColumnSum( int _ksize, int _anchor, double _scale ) :
948 virtual void reset() CV_OVERRIDE { sumCount = 0; }
950 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
953 bool haveScale = scale != 1;
954 double _scale = scale;
957 bool haveSIMD128 = hasSIMD128();
960 if( width != (int)sum.size() )
969 memset((void*)SUM, 0, width*sizeof(int));
970 for( ; sumCount < ksize - 1; sumCount++, src++ )
972 const int* Sp = (const int*)src[0];
977 for( ; i <= width - 4; i+=4 )
979 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
984 for( ; i < width; i++ )
990 CV_Assert( sumCount == ksize-1 );
994 for( ; count--; src++ )
996 const int * Sp = (const int*)src[0];
997 const int * Sm = (const int*)src[1-ksize];
998 float* D = (float*)dst;
1006 v_float32x4 v_scale = v_setall_f32((float)_scale);
1007 for (; i <= width - 8; i += 8)
1009 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
1010 v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
1012 v_store(D + i, v_cvt_f32(v_s0) * v_scale);
1013 v_store(D + i + 4, v_cvt_f32(v_s01) * v_scale);
1015 v_store(SUM + i, v_s0 - v_load(Sm + i));
1016 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
1020 for( ; i < width; i++ )
1022 int s0 = SUM[i] + Sp[i];
1023 D[i] = (float)(s0*_scale);
1024 SUM[i] = s0 - Sm[i];
1034 for( ; i <= width-8; i+=8 )
1036 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
1037 v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
1039 v_store(D + i, v_cvt_f32(v_s0));
1040 v_store(D + i + 4, v_cvt_f32(v_s01));
1042 v_store(SUM + i, v_s0 - v_load(Sm + i));
1043 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
1047 for( ; i < width; i++ )
1049 int s0 = SUM[i] + Sp[i];
1051 SUM[i] = s0 - Sm[i];
1060 std::vector<int> sum;
1065 static bool ocl_boxFilter3x3_8UC1( InputArray _src, OutputArray _dst, int ddepth,
1066 Size ksize, Point anchor, int borderType, bool normalize )
1068 const ocl::Device & dev = ocl::Device::getDefault();
1069 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
1075 anchor.x = ksize.width / 2;
1077 anchor.y = ksize.height / 2;
1079 if ( !(dev.isIntel() && (type == CV_8UC1) &&
1080 (_src.offset() == 0) && (_src.step() % 4 == 0) &&
1081 (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0) &&
1082 (anchor.x == 1) && (anchor.y == 1) &&
1083 (ksize.width == 3) && (ksize.height == 3)) )
1086 float alpha = 1.0f / (ksize.height * ksize.width);
1087 Size size = _src.size();
1088 size_t globalsize[2] = { 0, 0 };
1089 size_t localsize[2] = { 0, 0 };
1090 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
1092 globalsize[0] = size.width / 16;
1093 globalsize[1] = size.height / 2;
1095 char build_opts[1024];
1096 sprintf(build_opts, "-D %s %s", borderMap[borderType], normalize ? "-D NORMALIZE" : "");
1098 ocl::Kernel kernel("boxFilter3x3_8UC1_cols16_rows2", cv::ocl::imgproc::boxFilter3x3_oclsrc, build_opts);
1102 UMat src = _src.getUMat();
1103 _dst.create(size, CV_MAKETYPE(ddepth, cn));
1104 if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
1106 UMat dst = _dst.getUMat();
1108 int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
1109 idxArg = kernel.set(idxArg, (int)src.step);
1110 idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
1111 idxArg = kernel.set(idxArg, (int)dst.step);
1112 idxArg = kernel.set(idxArg, (int)dst.rows);
1113 idxArg = kernel.set(idxArg, (int)dst.cols);
1115 idxArg = kernel.set(idxArg, (float)alpha);
1117 return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
1120 #define DIVUP(total, grain) ((total + grain - 1) / (grain))
1121 #define ROUNDUP(sz, n) ((sz) + (n) - 1 - (((sz) + (n) - 1) % (n)))
1123 static bool ocl_boxFilter( InputArray _src, OutputArray _dst, int ddepth,
1124 Size ksize, Point anchor, int borderType, bool normalize, bool sqr = false )
1126 const ocl::Device & dev = ocl::Device::getDefault();
1127 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type), esz = CV_ELEM_SIZE(type);
1128 bool doubleSupport = dev.doubleFPConfig() > 0;
1133 if (cn > 4 || (!doubleSupport && (sdepth == CV_64F || ddepth == CV_64F)) ||
1134 _src.offset() % esz != 0 || _src.step() % esz != 0)
1138 anchor.x = ksize.width / 2;
1140 anchor.y = ksize.height / 2;
1142 int computeUnits = ocl::Device::getDefault().maxComputeUnits();
1143 float alpha = 1.0f / (ksize.height * ksize.width);
1144 Size size = _src.size(), wholeSize;
1145 bool isolated = (borderType & BORDER_ISOLATED) != 0;
1146 borderType &= ~BORDER_ISOLATED;
1147 int wdepth = std::max(CV_32F, std::max(ddepth, sdepth)),
1148 wtype = CV_MAKE_TYPE(wdepth, cn), dtype = CV_MAKE_TYPE(ddepth, cn);
1150 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
1151 size_t globalsize[2] = { (size_t)size.width, (size_t)size.height };
1152 size_t localsize_general[2] = { 0, 1 }, * localsize = NULL;
1154 UMat src = _src.getUMat();
1158 src.locateROI(wholeSize, ofs);
1161 int h = isolated ? size.height : wholeSize.height;
1162 int w = isolated ? size.width : wholeSize.width;
1164 size_t maxWorkItemSizes[32];
1165 ocl::Device::getDefault().maxWorkItemSizes(maxWorkItemSizes);
1166 int tryWorkItems = (int)maxWorkItemSizes[0];
1170 if (dev.isIntel() && !(dev.type() & ocl::Device::TYPE_CPU) &&
1171 ((ksize.width < 5 && ksize.height < 5 && esz <= 4) ||
1172 (ksize.width == 5 && ksize.height == 5 && cn == 1)))
1174 if (w < ksize.width || h < ksize.height)
1177 // Figure out what vector size to use for loading the pixels.
1178 int pxLoadNumPixels = cn != 1 || size.width % 4 ? 1 : 4;
1179 int pxLoadVecSize = cn * pxLoadNumPixels;
1181 // Figure out how many pixels per work item to compute in X and Y
1182 // directions. Too many and we run out of registers.
1183 int pxPerWorkItemX = 1, pxPerWorkItemY = 1;
1184 if (cn <= 2 && ksize.width <= 4 && ksize.height <= 4)
1186 pxPerWorkItemX = size.width % 8 ? size.width % 4 ? size.width % 2 ? 1 : 2 : 4 : 8;
1187 pxPerWorkItemY = size.height % 2 ? 1 : 2;
1189 else if (cn < 4 || (ksize.width <= 4 && ksize.height <= 4))
1191 pxPerWorkItemX = size.width % 2 ? 1 : 2;
1192 pxPerWorkItemY = size.height % 2 ? 1 : 2;
1194 globalsize[0] = size.width / pxPerWorkItemX;
1195 globalsize[1] = size.height / pxPerWorkItemY;
1197 // Need some padding in the private array for pixels
1198 int privDataWidth = ROUNDUP(pxPerWorkItemX + ksize.width - 1, pxLoadNumPixels);
1200 // Make the global size a nice round number so the runtime can pick
1201 // from reasonable choices for the workgroup size
1202 const int wgRound = 256;
1203 globalsize[0] = ROUNDUP(globalsize[0], wgRound);
1205 char build_options[1024], cvt[2][40];
1206 sprintf(build_options, "-D cn=%d "
1207 "-D ANCHOR_X=%d -D ANCHOR_Y=%d -D KERNEL_SIZE_X=%d -D KERNEL_SIZE_Y=%d "
1208 "-D PX_LOAD_VEC_SIZE=%d -D PX_LOAD_NUM_PX=%d "
1209 "-D PX_PER_WI_X=%d -D PX_PER_WI_Y=%d -D PRIV_DATA_WIDTH=%d -D %s -D %s "
1210 "-D PX_LOAD_X_ITERATIONS=%d -D PX_LOAD_Y_ITERATIONS=%d "
1211 "-D srcT=%s -D srcT1=%s -D dstT=%s -D dstT1=%s -D WT=%s -D WT1=%s "
1212 "-D convertToWT=%s -D convertToDstT=%s%s%s -D PX_LOAD_FLOAT_VEC_CONV=convert_%s -D OP_BOX_FILTER",
1213 cn, anchor.x, anchor.y, ksize.width, ksize.height,
1214 pxLoadVecSize, pxLoadNumPixels,
1215 pxPerWorkItemX, pxPerWorkItemY, privDataWidth, borderMap[borderType],
1216 isolated ? "BORDER_ISOLATED" : "NO_BORDER_ISOLATED",
1217 privDataWidth / pxLoadNumPixels, pxPerWorkItemY + ksize.height - 1,
1218 ocl::typeToStr(type), ocl::typeToStr(sdepth), ocl::typeToStr(dtype),
1219 ocl::typeToStr(ddepth), ocl::typeToStr(wtype), ocl::typeToStr(wdepth),
1220 ocl::convertTypeStr(sdepth, wdepth, cn, cvt[0]),
1221 ocl::convertTypeStr(wdepth, ddepth, cn, cvt[1]),
1222 normalize ? " -D NORMALIZE" : "", sqr ? " -D SQR" : "",
1223 ocl::typeToStr(CV_MAKE_TYPE(wdepth, pxLoadVecSize)) //PX_LOAD_FLOAT_VEC_CONV
1227 if (!kernel.create("filterSmall", cv::ocl::imgproc::filterSmall_oclsrc, build_options))
1232 localsize = localsize_general;
1235 int BLOCK_SIZE_X = tryWorkItems, BLOCK_SIZE_Y = std::min(ksize.height * 10, size.height);
1237 while (BLOCK_SIZE_X > 32 && BLOCK_SIZE_X >= ksize.width * 2 && BLOCK_SIZE_X > size.width * 2)
1239 while (BLOCK_SIZE_Y < BLOCK_SIZE_X / 8 && BLOCK_SIZE_Y * computeUnits * 32 < size.height)
1242 if (ksize.width > BLOCK_SIZE_X || w < ksize.width || h < ksize.height)
1246 String opts = format("-D LOCAL_SIZE_X=%d -D BLOCK_SIZE_Y=%d -D ST=%s -D DT=%s -D WT=%s -D convertToDT=%s -D convertToWT=%s"
1247 " -D ANCHOR_X=%d -D ANCHOR_Y=%d -D KERNEL_SIZE_X=%d -D KERNEL_SIZE_Y=%d -D %s%s%s%s%s"
1248 " -D ST1=%s -D DT1=%s -D cn=%d",
1249 BLOCK_SIZE_X, BLOCK_SIZE_Y, ocl::typeToStr(type), ocl::typeToStr(CV_MAKE_TYPE(ddepth, cn)),
1250 ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)),
1251 ocl::convertTypeStr(wdepth, ddepth, cn, cvt[0]),
1252 ocl::convertTypeStr(sdepth, wdepth, cn, cvt[1]),
1253 anchor.x, anchor.y, ksize.width, ksize.height, borderMap[borderType],
1254 isolated ? " -D BORDER_ISOLATED" : "", doubleSupport ? " -D DOUBLE_SUPPORT" : "",
1255 normalize ? " -D NORMALIZE" : "", sqr ? " -D SQR" : "",
1256 ocl::typeToStr(sdepth), ocl::typeToStr(ddepth), cn);
1258 localsize[0] = BLOCK_SIZE_X;
1259 globalsize[0] = DIVUP(size.width, BLOCK_SIZE_X - (ksize.width - 1)) * BLOCK_SIZE_X;
1260 globalsize[1] = DIVUP(size.height, BLOCK_SIZE_Y);
1262 kernel.create("boxFilter", cv::ocl::imgproc::boxFilter_oclsrc, opts);
1266 size_t kernelWorkGroupSize = kernel.workGroupSize();
1267 if (localsize[0] <= kernelWorkGroupSize)
1269 if (BLOCK_SIZE_X < (int)kernelWorkGroupSize)
1272 tryWorkItems = (int)kernelWorkGroupSize;
1276 _dst.create(size, CV_MAKETYPE(ddepth, cn));
1277 UMat dst = _dst.getUMat();
1279 int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
1280 idxArg = kernel.set(idxArg, (int)src.step);
1281 int srcOffsetX = (int)((src.offset % src.step) / src.elemSize());
1282 int srcOffsetY = (int)(src.offset / src.step);
1283 int srcEndX = isolated ? srcOffsetX + size.width : wholeSize.width;
1284 int srcEndY = isolated ? srcOffsetY + size.height : wholeSize.height;
1285 idxArg = kernel.set(idxArg, srcOffsetX);
1286 idxArg = kernel.set(idxArg, srcOffsetY);
1287 idxArg = kernel.set(idxArg, srcEndX);
1288 idxArg = kernel.set(idxArg, srcEndY);
1289 idxArg = kernel.set(idxArg, ocl::KernelArg::WriteOnly(dst));
1291 idxArg = kernel.set(idxArg, (float)alpha);
1293 return kernel.run(2, globalsize, localsize, false);
1303 cv::Ptr<cv::BaseRowFilter> cv::getRowSumFilter(int srcType, int sumType, int ksize, int anchor)
1305 int sdepth = CV_MAT_DEPTH(srcType), ddepth = CV_MAT_DEPTH(sumType);
1306 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(srcType) );
1311 if( sdepth == CV_8U && ddepth == CV_32S )
1312 return makePtr<RowSum<uchar, int> >(ksize, anchor);
1313 if( sdepth == CV_8U && ddepth == CV_16U )
1314 return makePtr<RowSum<uchar, ushort> >(ksize, anchor);
1315 if( sdepth == CV_8U && ddepth == CV_64F )
1316 return makePtr<RowSum<uchar, double> >(ksize, anchor);
1317 if( sdepth == CV_16U && ddepth == CV_32S )
1318 return makePtr<RowSum<ushort, int> >(ksize, anchor);
1319 if( sdepth == CV_16U && ddepth == CV_64F )
1320 return makePtr<RowSum<ushort, double> >(ksize, anchor);
1321 if( sdepth == CV_16S && ddepth == CV_32S )
1322 return makePtr<RowSum<short, int> >(ksize, anchor);
1323 if( sdepth == CV_32S && ddepth == CV_32S )
1324 return makePtr<RowSum<int, int> >(ksize, anchor);
1325 if( sdepth == CV_16S && ddepth == CV_64F )
1326 return makePtr<RowSum<short, double> >(ksize, anchor);
1327 if( sdepth == CV_32F && ddepth == CV_64F )
1328 return makePtr<RowSum<float, double> >(ksize, anchor);
1329 if( sdepth == CV_64F && ddepth == CV_64F )
1330 return makePtr<RowSum<double, double> >(ksize, anchor);
1332 CV_Error_( CV_StsNotImplemented,
1333 ("Unsupported combination of source format (=%d), and buffer format (=%d)",
1336 return Ptr<BaseRowFilter>();
1340 cv::Ptr<cv::BaseColumnFilter> cv::getColumnSumFilter(int sumType, int dstType, int ksize,
1341 int anchor, double scale)
1343 int sdepth = CV_MAT_DEPTH(sumType), ddepth = CV_MAT_DEPTH(dstType);
1344 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(dstType) );
1349 if( ddepth == CV_8U && sdepth == CV_32S )
1350 return makePtr<ColumnSum<int, uchar> >(ksize, anchor, scale);
1351 if( ddepth == CV_8U && sdepth == CV_16U )
1352 return makePtr<ColumnSum<ushort, uchar> >(ksize, anchor, scale);
1353 if( ddepth == CV_8U && sdepth == CV_64F )
1354 return makePtr<ColumnSum<double, uchar> >(ksize, anchor, scale);
1355 if( ddepth == CV_16U && sdepth == CV_32S )
1356 return makePtr<ColumnSum<int, ushort> >(ksize, anchor, scale);
1357 if( ddepth == CV_16U && sdepth == CV_64F )
1358 return makePtr<ColumnSum<double, ushort> >(ksize, anchor, scale);
1359 if( ddepth == CV_16S && sdepth == CV_32S )
1360 return makePtr<ColumnSum<int, short> >(ksize, anchor, scale);
1361 if( ddepth == CV_16S && sdepth == CV_64F )
1362 return makePtr<ColumnSum<double, short> >(ksize, anchor, scale);
1363 if( ddepth == CV_32S && sdepth == CV_32S )
1364 return makePtr<ColumnSum<int, int> >(ksize, anchor, scale);
1365 if( ddepth == CV_32F && sdepth == CV_32S )
1366 return makePtr<ColumnSum<int, float> >(ksize, anchor, scale);
1367 if( ddepth == CV_32F && sdepth == CV_64F )
1368 return makePtr<ColumnSum<double, float> >(ksize, anchor, scale);
1369 if( ddepth == CV_64F && sdepth == CV_32S )
1370 return makePtr<ColumnSum<int, double> >(ksize, anchor, scale);
1371 if( ddepth == CV_64F && sdepth == CV_64F )
1372 return makePtr<ColumnSum<double, double> >(ksize, anchor, scale);
1374 CV_Error_( CV_StsNotImplemented,
1375 ("Unsupported combination of sum format (=%d), and destination format (=%d)",
1378 return Ptr<BaseColumnFilter>();
1382 cv::Ptr<cv::FilterEngine> cv::createBoxFilter( int srcType, int dstType, Size ksize,
1383 Point anchor, bool normalize, int borderType )
1385 int sdepth = CV_MAT_DEPTH(srcType);
1386 int cn = CV_MAT_CN(srcType), sumType = CV_64F;
1387 if( sdepth == CV_8U && CV_MAT_DEPTH(dstType) == CV_8U &&
1388 ksize.width*ksize.height <= 256 )
1390 else if( sdepth <= CV_32S && (!normalize ||
1391 ksize.width*ksize.height <= (sdepth == CV_8U ? (1<<23) :
1392 sdepth == CV_16U ? (1 << 15) : (1 << 16))) )
1394 sumType = CV_MAKETYPE( sumType, cn );
1396 Ptr<BaseRowFilter> rowFilter = getRowSumFilter(srcType, sumType, ksize.width, anchor.x );
1397 Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
1398 dstType, ksize.height, anchor.y, normalize ? 1./(ksize.width*ksize.height) : 1);
1400 return makePtr<FilterEngine>(Ptr<BaseFilter>(), rowFilter, columnFilter,
1401 srcType, dstType, sumType, borderType );
1408 template <> inline bool skipSmallImages<VX_KERNEL_BOX_3x3>(int w, int h) { return w*h < 640 * 480; }
1410 static bool openvx_boxfilter(InputArray _src, OutputArray _dst, int ddepth,
1411 Size ksize, Point anchor,
1412 bool normalize, int borderType)
1416 if (_src.type() != CV_8UC1 || ddepth != CV_8U || !normalize ||
1417 _src.cols() < 3 || _src.rows() < 3 ||
1418 ksize.width != 3 || ksize.height != 3 ||
1419 (anchor.x >= 0 && anchor.x != 1) ||
1420 (anchor.y >= 0 && anchor.y != 1) ||
1421 ovx::skipSmallImages<VX_KERNEL_BOX_3x3>(_src.cols(), _src.rows()))
1424 Mat src = _src.getMat();
1426 if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
1427 return false; //Process isolated borders only
1429 switch (borderType & ~BORDER_ISOLATED)
1431 case BORDER_CONSTANT:
1432 border = VX_BORDER_CONSTANT;
1434 case BORDER_REPLICATE:
1435 border = VX_BORDER_REPLICATE;
1441 _dst.create(src.size(), CV_8UC1);
1442 Mat dst = _dst.getMat();
1446 ivx::Context ctx = ovx::getOpenVXContext();
1449 if (dst.data != src.data)
1455 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1456 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
1457 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1458 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
1460 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
1461 //since OpenVX standard says nothing about thread-safety for now
1462 ivx::border_t prevBorder = ctx.immediateBorder();
1463 ctx.setImmediateBorder(border, (vx_uint8)(0));
1464 ivx::IVX_CHECK_STATUS(vxuBox3x3(ctx, ia, ib));
1465 ctx.setImmediateBorder(prevBorder);
1467 catch (ivx::RuntimeError & e)
1469 VX_DbgThrow(e.what());
1471 catch (ivx::WrapperError & e)
1473 VX_DbgThrow(e.what());
1481 #if defined(HAVE_IPP)
1484 static bool ipp_boxfilter(Mat &src, Mat &dst, Size ksize, Point anchor, bool normalize, int borderType)
1487 CV_INSTRUMENT_REGION_IPP()
1489 #if IPP_VERSION_X100 < 201801
1490 // Problem with SSE42 optimization for 16s and some 8u modes
1491 if(ipp::getIppTopFeatures() == ippCPUID_SSE42 && (((src.depth() == CV_16S || src.depth() == CV_16U) && (src.channels() == 3 || src.channels() == 4)) || (src.depth() == CV_8U && src.channels() == 3 && (ksize.width > 5 || ksize.height > 5))))
1494 // Other optimizations has some degradations too
1495 if((((src.depth() == CV_16S || src.depth() == CV_16U) && (src.channels() == 4)) || (src.depth() == CV_8U && src.channels() == 1 && (ksize.width > 5 || ksize.height > 5))))
1502 if(!ippiCheckAnchor(anchor, ksize))
1507 ::ipp::IwiImage iwSrc = ippiGetImage(src);
1508 ::ipp::IwiImage iwDst = ippiGetImage(dst);
1509 ::ipp::IwiSize iwKSize = ippiGetSize(ksize);
1510 ::ipp::IwiBorderSize borderSize(iwKSize);
1511 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
1515 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBox, iwSrc, iwDst, iwKSize, ::ipp::IwDefault(), ippBorder);
1517 catch (::ipp::IwException)
1524 CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(ksize); CV_UNUSED(anchor); CV_UNUSED(normalize); CV_UNUSED(borderType);
1532 void cv::boxFilter( InputArray _src, OutputArray _dst, int ddepth,
1533 Size ksize, Point anchor,
1534 bool normalize, int borderType )
1536 CV_INSTRUMENT_REGION()
1538 CV_OCL_RUN(_dst.isUMat() &&
1539 (borderType == BORDER_REPLICATE || borderType == BORDER_CONSTANT ||
1540 borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101),
1541 ocl_boxFilter3x3_8UC1(_src, _dst, ddepth, ksize, anchor, borderType, normalize))
1543 CV_OCL_RUN(_dst.isUMat(), ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize))
1545 Mat src = _src.getMat();
1546 int stype = src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
1549 _dst.create( src.size(), CV_MAKETYPE(ddepth, cn) );
1550 Mat dst = _dst.getMat();
1551 if( borderType != BORDER_CONSTANT && normalize && (borderType & BORDER_ISOLATED) != 0 )
1560 Size wsz(src.cols, src.rows);
1561 if(!(borderType&BORDER_ISOLATED))
1562 src.locateROI( wsz, ofs );
1564 CALL_HAL(boxFilter, cv_hal_boxFilter, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, ddepth, cn,
1565 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
1566 anchor.x, anchor.y, normalize, borderType&~BORDER_ISOLATED);
1569 openvx_boxfilter(src, dst, ddepth, ksize, anchor, normalize, borderType))
1571 CV_IPP_RUN_FAST(ipp_boxfilter(src, dst, ksize, anchor, normalize, borderType));
1573 borderType = (borderType&~BORDER_ISOLATED);
1575 Ptr<FilterEngine> f = createBoxFilter( src.type(), dst.type(),
1576 ksize, anchor, normalize, borderType );
1578 f->apply( src, dst, wsz, ofs );
1582 void cv::blur( InputArray src, OutputArray dst,
1583 Size ksize, Point anchor, int borderType )
1585 CV_INSTRUMENT_REGION()
1587 boxFilter( src, dst, -1, ksize, anchor, true, borderType );
1591 /****************************************************************************************\
1593 \****************************************************************************************/
1598 template<typename T, typename ST>
1600 public BaseRowFilter
1602 SqrRowSum( int _ksize, int _anchor ) :
1609 virtual void operator()(const uchar* src, uchar* dst, int width, int cn) CV_OVERRIDE
1611 const T* S = (const T*)src;
1613 int i = 0, k, ksz_cn = ksize*cn;
1615 width = (width - 1)*cn;
1616 for( k = 0; k < cn; k++, S++, D++ )
1619 for( i = 0; i < ksz_cn; i += cn )
1625 for( i = 0; i < width; i += cn )
1627 ST val0 = (ST)S[i], val1 = (ST)S[i + ksz_cn];
1628 s += val1*val1 - val0*val0;
1635 static Ptr<BaseRowFilter> getSqrRowSumFilter(int srcType, int sumType, int ksize, int anchor)
1637 int sdepth = CV_MAT_DEPTH(srcType), ddepth = CV_MAT_DEPTH(sumType);
1638 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(srcType) );
1643 if( sdepth == CV_8U && ddepth == CV_32S )
1644 return makePtr<SqrRowSum<uchar, int> >(ksize, anchor);
1645 if( sdepth == CV_8U && ddepth == CV_64F )
1646 return makePtr<SqrRowSum<uchar, double> >(ksize, anchor);
1647 if( sdepth == CV_16U && ddepth == CV_64F )
1648 return makePtr<SqrRowSum<ushort, double> >(ksize, anchor);
1649 if( sdepth == CV_16S && ddepth == CV_64F )
1650 return makePtr<SqrRowSum<short, double> >(ksize, anchor);
1651 if( sdepth == CV_32F && ddepth == CV_64F )
1652 return makePtr<SqrRowSum<float, double> >(ksize, anchor);
1653 if( sdepth == CV_64F && ddepth == CV_64F )
1654 return makePtr<SqrRowSum<double, double> >(ksize, anchor);
1656 CV_Error_( CV_StsNotImplemented,
1657 ("Unsupported combination of source format (=%d), and buffer format (=%d)",
1660 return Ptr<BaseRowFilter>();
1665 void cv::sqrBoxFilter( InputArray _src, OutputArray _dst, int ddepth,
1666 Size ksize, Point anchor,
1667 bool normalize, int borderType )
1669 CV_INSTRUMENT_REGION()
1671 int srcType = _src.type(), sdepth = CV_MAT_DEPTH(srcType), cn = CV_MAT_CN(srcType);
1672 Size size = _src.size();
1675 ddepth = sdepth < CV_32F ? CV_32F : CV_64F;
1677 if( borderType != BORDER_CONSTANT && normalize )
1679 if( size.height == 1 )
1681 if( size.width == 1 )
1685 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2,
1686 ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize, true))
1688 int sumDepth = CV_64F;
1689 if( sdepth == CV_8U )
1691 int sumType = CV_MAKETYPE( sumDepth, cn ), dstType = CV_MAKETYPE(ddepth, cn);
1693 Mat src = _src.getMat();
1694 _dst.create( size, dstType );
1695 Mat dst = _dst.getMat();
1697 Ptr<BaseRowFilter> rowFilter = getSqrRowSumFilter(srcType, sumType, ksize.width, anchor.x );
1698 Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
1699 dstType, ksize.height, anchor.y,
1700 normalize ? 1./(ksize.width*ksize.height) : 1);
1702 Ptr<FilterEngine> f = makePtr<FilterEngine>(Ptr<BaseFilter>(), rowFilter, columnFilter,
1703 srcType, dstType, sumType, borderType );
1705 Size wsz(src.cols, src.rows);
1706 src.locateROI( wsz, ofs );
1708 f->apply( src, dst, wsz, ofs );
1712 /****************************************************************************************\
1714 \****************************************************************************************/
1716 cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
1718 const int SMALL_GAUSSIAN_SIZE = 7;
1719 static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
1722 {0.25f, 0.5f, 0.25f},
1723 {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
1724 {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
1727 const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
1728 small_gaussian_tab[n>>1] : 0;
1730 CV_Assert( ktype == CV_32F || ktype == CV_64F );
1731 Mat kernel(n, 1, ktype);
1732 float* cf = kernel.ptr<float>();
1733 double* cd = kernel.ptr<double>();
1735 double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
1736 double scale2X = -0.5/(sigmaX*sigmaX);
1740 for( i = 0; i < n; i++ )
1742 double x = i - (n-1)*0.5;
1743 double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x);
1744 if( ktype == CV_32F )
1757 for( i = 0; i < n; i++ )
1759 if( ktype == CV_32F )
1760 cf[i] = (float)(cf[i]*sum);
1770 template <typename T>
1771 static std::vector<T> getFixedpointGaussianKernel( int n, double sigma )
1776 return std::vector<T>(1, softdouble(1.0));
1779 T v3[] = { softdouble(0.25), softdouble(0.5), softdouble(0.25) };
1780 return std::vector<T>(v3, v3 + 3);
1784 T v5[] = { softdouble(0.0625), softdouble(0.25), softdouble(0.375), softdouble(0.25), softdouble(0.0625) };
1785 return std::vector<T>(v5, v5 + 5);
1789 T v7[] = { softdouble(0.03125), softdouble(0.109375), softdouble(0.21875), softdouble(0.28125), softdouble(0.21875), softdouble(0.109375), softdouble(0.03125) };
1790 return std::vector<T>(v7, v7 + 7);
1795 softdouble sigmaX = sigma > 0 ? softdouble(sigma) : mulAdd(softdouble(n),softdouble(0.15),softdouble(0.35));// softdouble(((n-1)*0.5 - 1)*0.3 + 0.8)
1796 softdouble scale2X = softdouble(-0.5*0.25)/(sigmaX*sigmaX);
1797 std::vector<softdouble> values(n);
1799 for(int i = 0, x = 1 - n; i < n; i++, x+=2 )
1801 // x = i - (n - 1)*0.5
1802 // t = std::exp(scale2X*x*x)
1803 values[i] = exp(softdouble(x*x)*scale2X);
1806 sum = softdouble::one()/sum;
1808 std::vector<T> kernel(n);
1809 for(int i = 0; i < n; i++ )
1811 kernel[i] = values[i] * sum;
1817 template <typename ET, typename FT>
1818 void hlineSmooth1N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int)
1820 for (int i = 0; i < len*cn; i++, src++, dst++)
1821 *dst = (*m) * (*src);
1824 void hlineSmooth1N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int)
1827 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
1829 for (; i < lencn - 15; i += 16)
1831 v_uint8x16 v_src = v_load(src + i);
1832 v_uint16x8 v_tmp0, v_tmp1;
1833 v_expand(v_src, v_tmp0, v_tmp1);
1834 v_store((uint16_t*)dst + i, v_mul*v_tmp0);
1835 v_store((uint16_t*)dst + i + 8, v_mul*v_tmp1);
1839 v_uint16x8 v_src = v_load_expand(src + i);
1840 v_store((uint16_t*)dst + i, v_mul*v_src);
1843 for (; i < lencn; i++)
1844 dst[i] = m[0] * src[i];
1846 template <typename ET, typename FT>
1847 void hlineSmooth1N1(const ET* src, int cn, const FT*, int, FT* dst, int len, int)
1849 for (int i = 0; i < len*cn; i++, src++, dst++)
1853 void hlineSmooth1N1<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int)
1857 for (; i < lencn - 15; i += 16)
1859 v_uint8x16 v_src = v_load(src + i);
1860 v_uint16x8 v_tmp0, v_tmp1;
1861 v_expand(v_src, v_tmp0, v_tmp1);
1862 v_store((uint16_t*)dst + i, v_shl<8>(v_tmp0));
1863 v_store((uint16_t*)dst + i + 8, v_shl<8>(v_tmp1));
1867 v_uint16x8 v_src = v_load_expand(src + i);
1868 v_store((uint16_t*)dst + i, v_shl<8>(v_src));
1871 for (; i < lencn; i++)
1874 template <typename ET, typename FT>
1875 void hlineSmooth3N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
1879 FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1880 for (int k = 0; k < cn; k++)
1881 dst[k] = msum * src[k];
1885 // Point that fall left from border
1886 for (int k = 0; k < cn; k++)
1887 dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1888 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1890 int src_idx = borderInterpolate(-1, len, borderType);
1891 for (int k = 0; k < cn; k++)
1892 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1895 src += cn; dst += cn;
1896 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1897 *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1899 // Point that fall right from border
1900 for (int k = 0; k < cn; k++)
1901 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1902 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1904 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1905 for (int k = 0; k < cn; k++)
1906 dst[k] = dst[k] + m[2] * src[src_idx + k];
1911 void hlineSmooth3N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
1915 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1916 for (int k = 0; k < cn; k++)
1917 dst[k] = msum * src[k];
1921 // Point that fall left from border
1922 for (int k = 0; k < cn; k++)
1923 dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1924 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1926 int src_idx = borderInterpolate(-1, len, borderType);
1927 for (int k = 0; k < cn; k++)
1928 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1931 src += cn; dst += cn;
1932 int i = cn, lencn = (len - 1)*cn;
1933 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
1934 v_int16x8 v_mul2 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 2))));
1935 for (; i < lencn - 15; i += 16, src += 16, dst += 16)
1937 v_uint16x8 v_src00, v_src01, v_src10, v_src11;
1938 v_int16x8 v_tmp0, v_tmp1;
1940 v_expand(v_load(src - cn), v_src00, v_src01);
1941 v_expand(v_load(src), v_src10, v_src11);
1942 v_zip(v_reinterpret_as_s16(v_src00), v_reinterpret_as_s16(v_src10), v_tmp0, v_tmp1);
1943 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
1944 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
1945 v_zip(v_reinterpret_as_s16(v_src01), v_reinterpret_as_s16(v_src11), v_tmp0, v_tmp1);
1946 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
1947 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
1949 v_int32x4 v_resj0, v_resj1, v_resj2, v_resj3;
1950 v_expand(v_load(src + cn), v_src00, v_src01);
1951 v_mul_expand(v_reinterpret_as_s16(v_src00), v_mul2, v_resj0, v_resj1);
1952 v_mul_expand(v_reinterpret_as_s16(v_src01), v_mul2, v_resj2, v_resj3);
1958 v_store((uint16_t*)dst, v_pack(v_reinterpret_as_u32(v_res0), v_reinterpret_as_u32(v_res1)));
1959 v_store((uint16_t*)dst + 8, v_pack(v_reinterpret_as_u32(v_res2), v_reinterpret_as_u32(v_res3)));
1961 for (; i < lencn; i++, src++, dst++)
1962 *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1964 // Point that fall right from border
1965 for (int k = 0; k < cn; k++)
1966 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1967 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1969 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1970 for (int k = 0; k < cn; k++)
1971 dst[k] = dst[k] + m[2] * src[src_idx + k];
1975 template <typename ET, typename FT>
1976 void hlineSmooth3N121(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
1980 if(borderType != BORDER_CONSTANT)
1981 for (int k = 0; k < cn; k++)
1982 dst[k] = FT(src[k]);
1984 for (int k = 0; k < cn; k++)
1985 dst[k] = FT(src[k])>>1;
1989 // Point that fall left from border
1990 for (int k = 0; k < cn; k++)
1991 dst[k] = (FT(src[k])>>1) + (FT(src[cn + k])>>2);
1992 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1994 int src_idx = borderInterpolate(-1, len, borderType);
1995 for (int k = 0; k < cn; k++)
1996 dst[k] = dst[k] + (FT(src[src_idx*cn + k])>>2);
1999 src += cn; dst += cn;
2000 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
2001 *dst = ((FT(src[-cn]) + FT(src[cn]))>>2) + (FT(src[0])>>1);
2003 // Point that fall right from border
2004 for (int k = 0; k < cn; k++)
2005 dst[k] = (FT(src[k - cn])>>2) + (FT(src[k])>>1);
2006 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2008 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2009 for (int k = 0; k < cn; k++)
2010 dst[k] = dst[k] + (FT(src[src_idx + k])>>2);
2015 void hlineSmooth3N121<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
2019 if (borderType != BORDER_CONSTANT)
2020 for (int k = 0; k < cn; k++)
2021 dst[k] = ufixedpoint16(src[k]);
2023 for (int k = 0; k < cn; k++)
2024 dst[k] = ufixedpoint16(src[k]) >> 1;
2028 // Point that fall left from border
2029 for (int k = 0; k < cn; k++)
2030 dst[k] = (ufixedpoint16(src[k])>>1) + (ufixedpoint16(src[cn + k])>>2);
2031 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2033 int src_idx = borderInterpolate(-1, len, borderType);
2034 for (int k = 0; k < cn; k++)
2035 dst[k] = dst[k] + (ufixedpoint16(src[src_idx*cn + k])>>2);
2038 src += cn; dst += cn;
2039 int i = cn, lencn = (len - 1)*cn;
2040 for (; i < lencn - 15; i += 16, src += 16, dst += 16)
2042 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2043 v_expand(v_load(src - cn), v_src00, v_src01);
2044 v_expand(v_load(src), v_src10, v_src11);
2045 v_expand(v_load(src + cn), v_src20, v_src21);
2046 v_store((uint16_t*)dst, (v_src00 + v_src20 + (v_src10 << 1)) << 6);
2047 v_store((uint16_t*)dst + 8, (v_src01 + v_src21 + (v_src11 << 1)) << 6);
2049 for (; i < lencn; i++, src++, dst++)
2050 *((uint16_t*)dst) = (uint16_t(src[-cn]) + uint16_t(src[cn]) + (uint16_t(src[0]) << 1)) << 6;
2052 // Point that fall right from border
2053 for (int k = 0; k < cn; k++)
2054 dst[k] = (ufixedpoint16(src[k - cn])>>2) + (ufixedpoint16(src[k])>>1);
2055 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2057 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2058 for (int k = 0; k < cn; k++)
2059 dst[k] = dst[k] + (ufixedpoint16(src[src_idx + k])>>2);
2063 template <typename ET, typename FT>
2064 void hlineSmooth5N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2068 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2069 for (int k = 0; k < cn; k++)
2070 dst[k] = msum * src[k];
2074 if (borderType == BORDER_CONSTANT)
2075 for (int k = 0; k < cn; k++)
2077 dst[k ] = m[2] * src[k] + m[3] * src[k+cn];
2078 dst[k+cn] = m[1] * src[k] + m[2] * src[k+cn];
2082 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2083 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2084 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2085 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2086 for (int k = 0; k < cn; k++)
2088 dst[k ] = m[1] * src[k + idxm1] + m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + idxp1] + m[0] * src[k + idxm2];
2089 dst[k + cn] = m[0] * src[k + idxm1] + m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2095 if (borderType == BORDER_CONSTANT)
2096 for (int k = 0; k < cn; k++)
2098 dst[k ] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2*cn];
2099 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2*cn];
2100 dst[k + 2*cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2*cn];
2104 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2105 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2106 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2107 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2108 for (int k = 0; k < cn; k++)
2110 dst[k ] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2*cn] + m[0] * src[k + idxm2] + m[1] * src[k + idxm1];
2111 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2*cn] + m[0] * src[k + idxm1] + m[4] * src[k + idxp1];
2112 dst[k + 2*cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2*cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2118 // Points that fall left from border
2119 for (int k = 0; k < cn; k++)
2121 dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2*cn + k];
2122 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2*cn + k] + m[4] * src[3*cn + k];
2124 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2126 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2127 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2128 for (int k = 0; k < cn; k++)
2130 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2131 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2135 src += 2*cn; dst += 2*cn;
2136 for (int i = 2*cn; i < (len - 2)*cn; i++, src++, dst++)
2137 *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2139 // Points that fall right from border
2140 for (int k = 0; k < cn; k++)
2142 dst[k] = m[0] * src[k - 2*cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2143 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2145 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2147 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2148 int idxp2 = (borderInterpolate(len+1, len, borderType) - (len - 2))*cn;
2149 for (int k = 0; k < cn; k++)
2151 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2152 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2158 void hlineSmooth5N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2162 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2163 for (int k = 0; k < cn; k++)
2164 dst[k] = msum * src[k];
2168 if (borderType == BORDER_CONSTANT)
2169 for (int k = 0; k < cn; k++)
2171 dst[k] = m[2] * src[k] + m[3] * src[k + cn];
2172 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2176 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2177 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2178 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2179 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2180 for (int k = 0; k < cn; k++)
2182 dst[k] = m[1] * src[k + idxm1] + m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + idxp1] + m[0] * src[k + idxm2];
2183 dst[k + cn] = m[0] * src[k + idxm1] + m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2189 if (borderType == BORDER_CONSTANT)
2190 for (int k = 0; k < cn; k++)
2192 dst[k] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2 * cn];
2193 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2 * cn];
2194 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2198 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2199 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2200 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2201 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2202 for (int k = 0; k < cn; k++)
2204 dst[k] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2 * cn] + m[0] * src[k + idxm2] + m[1] * src[k + idxm1];
2205 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2 * cn] + m[0] * src[k + idxm1] + m[4] * src[k + idxp1];
2206 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2212 // Points that fall left from border
2213 for (int k = 0; k < cn; k++)
2215 dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2 * cn + k];
2216 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2 * cn + k] + m[4] * src[3 * cn + k];
2218 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2220 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2221 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2222 for (int k = 0; k < cn; k++)
2224 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2225 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2229 src += 2 * cn; dst += 2 * cn;
2230 int i = 2*cn, lencn = (len - 2)*cn;
2231 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
2232 v_int16x8 v_mul23 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m + 2))));
2233 v_int16x8 v_mul4 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 4))));
2234 for (; i < lencn - 15; i += 16, src += 16, dst += 16)
2236 v_uint16x8 v_src00, v_src01, v_src10, v_src11;
2237 v_int16x8 v_tmp0, v_tmp1;
2239 v_expand(v_load(src - 2*cn), v_src00, v_src01);
2240 v_expand(v_load(src - cn), v_src10, v_src11);
2241 v_zip(v_reinterpret_as_s16(v_src00), v_reinterpret_as_s16(v_src10), v_tmp0, v_tmp1);
2242 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
2243 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
2244 v_zip(v_reinterpret_as_s16(v_src01), v_reinterpret_as_s16(v_src11), v_tmp0, v_tmp1);
2245 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
2246 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
2249 v_expand(v_load(src), v_src00, v_src01);
2250 v_expand(v_load(src + cn), v_src10, v_src11);
2251 v_zip(v_reinterpret_as_s16(v_src00), v_reinterpret_as_s16(v_src10), v_tmp0, v_tmp1);
2252 v_res0 += v_dotprod(v_tmp0, v_mul23);
2253 v_res1 += v_dotprod(v_tmp1, v_mul23);
2254 v_zip(v_reinterpret_as_s16(v_src01), v_reinterpret_as_s16(v_src11), v_tmp0, v_tmp1);
2255 v_res2 += v_dotprod(v_tmp0, v_mul23);
2256 v_res3 += v_dotprod(v_tmp1, v_mul23);
2258 v_int32x4 v_resj0, v_resj1, v_resj2, v_resj3;
2259 v_expand(v_load(src + 2*cn), v_src00, v_src01);
2260 v_mul_expand(v_reinterpret_as_s16(v_src00), v_mul4, v_resj0, v_resj1);
2261 v_mul_expand(v_reinterpret_as_s16(v_src01), v_mul4, v_resj2, v_resj3);
2267 v_store((uint16_t*)dst, v_pack(v_reinterpret_as_u32(v_res0), v_reinterpret_as_u32(v_res1)));
2268 v_store((uint16_t*)dst + 8, v_pack(v_reinterpret_as_u32(v_res2), v_reinterpret_as_u32(v_res3)));
2270 for (; i < lencn; i++, src++, dst++)
2271 *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2273 // Points that fall right from border
2274 for (int k = 0; k < cn; k++)
2276 dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2277 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2279 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2281 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2282 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2283 for (int k = 0; k < cn; k++)
2285 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2286 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2291 template <typename ET, typename FT>
2292 void hlineSmooth5N14641(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
2296 if (borderType == BORDER_CONSTANT)
2297 for (int k = 0; k < cn; k++)
2298 dst[k] = (FT(src[k])>>3)*3;
2300 for (int k = 0; k < cn; k++)
2305 if (borderType == BORDER_CONSTANT)
2306 for (int k = 0; k < cn; k++)
2308 dst[k] = (FT(src[k])>>4)*6 + (FT(src[k + cn])>>2);
2309 dst[k + cn] = (FT(src[k]) >> 2) + (FT(src[k + cn])>>4)*6;
2313 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2314 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2315 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2316 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2317 for (int k = 0; k < cn; k++)
2319 dst[k] = (FT(src[k])>>4)*6 + (FT(src[k + idxm1])>>2) + (FT(src[k + cn])>>2) + (FT(src[k + idxp1])>>4) + (FT(src[k + idxm2])>>4);
2320 dst[k + cn] = (FT(src[k + cn])>>4)*6 + (FT(src[k])>>2) + (FT(src[k + idxp1])>>2) + (FT(src[k + idxm1])>>4) + (FT(src[k + idxp2])>>4);
2326 if (borderType == BORDER_CONSTANT)
2327 for (int k = 0; k < cn; k++)
2329 dst[k] = (FT(src[k])>>4)*6 + (FT(src[k + cn])>>2) + (FT(src[k + 2 * cn])>>4);
2330 dst[k + cn] = (FT(src[k + cn])>>4)*6 + (FT(src[k])>>2) + (FT(src[k + 2 * cn])>>2);
2331 dst[k + 2 * cn] = (FT(src[k + 2 * cn])>>4)*6 + (FT(src[k + cn])>>2) + (FT(src[k])>>4);
2335 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2336 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2337 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2338 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2339 for (int k = 0; k < cn; k++)
2341 dst[k] = (FT(src[k])>>4)*6 + (FT(src[k + cn])>>2) + (FT(src[k + idxm1])>>2) + (FT(src[k + 2 * cn])>>4) + (FT(src[k + idxm2])>>4);
2342 dst[k + cn] = (FT(src[k + cn])>>4)*6 + (FT(src[k])>>2) + (FT(src[k + 2 * cn])>>2) + (FT(src[k + idxm1])>>4) + (FT(src[k + idxp1])>>4);
2343 dst[k + 2 * cn] = (FT(src[k + 2 * cn])>>4)*6 + (FT(src[k + cn])>>2) + (FT(src[k + idxp1])>>2) + (FT(src[k])>>4) + (FT(src[k + idxp2])>>4);
2349 // Points that fall left from border
2350 for (int k = 0; k < cn; k++)
2352 dst[k] = (FT(src[k])>>4)*6 + (FT(src[cn + k])>>2) + (FT(src[2 * cn + k])>>4);
2353 dst[k + cn] = (FT(src[cn + k])>>4)*6 + (FT(src[k])>>2) + (FT(src[2 * cn + k])>>2) + (FT(src[3 * cn + k])>>4);
2355 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2357 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2358 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2359 for (int k = 0; k < cn; k++)
2361 dst[k] = dst[k] + (FT(src[idxm2 + k])>>4) + (FT(src[idxm1 + k])>>2);
2362 dst[k + cn] = dst[k + cn] + (FT(src[idxm1 + k])>>4);
2366 src += 2 * cn; dst += 2 * cn;
2367 for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2368 *dst = (FT(src[0])>>4)*6 + (FT(src[-cn])>>2) + (FT(src[cn])>>2) + (FT(src[-2 * cn])>>4) + (FT(src[2 * cn])>>4);
2370 // Points that fall right from border
2371 for (int k = 0; k < cn; k++)
2373 dst[k] = (FT(src[k])>>4)*6 + (FT(src[k - cn])>>2) + (FT(src[k + cn])>>2) + (FT(src[k - 2 * cn])>>4);
2374 dst[k + cn] = (FT(src[k + cn])>>4)*6 + (FT(src[k])>>2) + (FT(src[k - cn])>>4);
2376 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2378 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2379 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2380 for (int k = 0; k < cn; k++)
2382 dst[k] = dst[k] + (FT(src[idxp1 + k])>>4);
2383 dst[k + cn] = dst[k + cn] + (FT(src[idxp1 + k])>>2) + (FT(src[idxp2 + k])>>4);
2389 void hlineSmooth5N14641<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
2393 if (borderType == BORDER_CONSTANT)
2394 for (int k = 0; k < cn; k++)
2395 dst[k] = (ufixedpoint16(src[k])>>3) * 3;
2398 for (int k = 0; k < cn; k++)
2404 if (borderType == BORDER_CONSTANT)
2405 for (int k = 0; k < cn; k++)
2407 dst[k] = (ufixedpoint16(src[k]) >> 4) * 6 + (ufixedpoint16(src[k + cn]) >> 2);
2408 dst[k + cn] = (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + cn]) >> 4) * 6;
2412 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2413 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2414 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2415 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2416 for (int k = 0; k < cn; k++)
2418 dst[k] = (ufixedpoint16(src[k]) >> 4) * 6 + (ufixedpoint16(src[k + idxm1]) >> 2) + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 4) + (ufixedpoint16(src[k + idxm2]) >> 4);
2419 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * 6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 4) + (ufixedpoint16(src[k + idxp2]) >> 4);
2425 if (borderType == BORDER_CONSTANT)
2426 for (int k = 0; k < cn; k++)
2428 dst[k] = (ufixedpoint16(src[k]) >> 4) * 6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 4);
2429 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * 6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 2);
2430 dst[k + 2 * cn] = (ufixedpoint16(src[k + 2 * cn]) >> 4) * 6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k]) >> 4);
2434 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2435 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2436 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2437 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2438 for (int k = 0; k < cn; k++)
2440 dst[k] = (ufixedpoint16(src[k]) >> 4) * 6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 4) + (ufixedpoint16(src[k + idxm2]) >> 4);
2441 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * 6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 4) + (ufixedpoint16(src[k + idxp1]) >> 4);
2442 dst[k + 2 * cn] = (ufixedpoint16(src[k + 2 * cn]) >> 4) * 6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 2) + (ufixedpoint16(src[k]) >> 4) + (ufixedpoint16(src[k + idxp2]) >> 4);
2448 // Points that fall left from border
2449 for (int k = 0; k < cn; k++)
2451 dst[k] = (ufixedpoint16(src[k]) >> 4) * 6 + (ufixedpoint16(src[cn + k]) >> 2) + (ufixedpoint16(src[2 * cn + k]) >> 4);
2452 dst[k + cn] = (ufixedpoint16(src[cn + k]) >> 4) * 6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[2 * cn + k]) >> 2) + (ufixedpoint16(src[3 * cn + k]) >> 4);
2454 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2456 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2457 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2458 for (int k = 0; k < cn; k++)
2460 dst[k] = dst[k] + (ufixedpoint16(src[idxm2 + k]) >> 4) + (ufixedpoint16(src[idxm1 + k]) >> 2);
2461 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxm1 + k]) >> 4);
2465 src += 2 * cn; dst += 2 * cn;
2466 int i = 2 * cn, lencn = (len - 2)*cn;
2467 v_uint16x8 v_6 = v_setall_u16(6);
2468 for (; i < lencn - 15; i += 16, src += 16, dst += 16)
2470 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2471 v_expand(v_load(src - 2*cn), v_src00, v_src01);
2472 v_expand(v_load(src - cn), v_src10, v_src11);
2473 v_expand(v_load(src), v_src20, v_src21);
2474 v_expand(v_load(src + cn), v_src30, v_src31);
2475 v_expand(v_load(src + 2*cn), v_src40, v_src41);
2476 v_store((uint16_t*)dst, (v_src20 * v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40) << 4);
2477 v_store((uint16_t*)dst + 8, (v_src21 * v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41) << 4);
2479 for (; i < lencn; i++, src++, dst++)
2480 *((uint16_t*)dst) = (uint16_t(src[0]) * 6 + ((uint16_t(src[-cn]) + uint16_t(src[cn])) << 2) + uint16_t(src[-2 * cn]) + uint16_t(src[2 * cn])) << 4;
2482 // Points that fall right from border
2483 for (int k = 0; k < cn; k++)
2485 dst[k] = (ufixedpoint16(src[k]) >> 4) * 6 + (ufixedpoint16(src[k - cn]) >> 2) + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k - 2 * cn]) >> 4);
2486 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * 6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k - cn]) >> 4);
2488 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2490 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2491 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2492 for (int k = 0; k < cn; k++)
2494 dst[k] = dst[k] + (ufixedpoint16(src[idxp1 + k]) >> 4);
2495 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxp1 + k]) >> 2) + (ufixedpoint16(src[idxp2 + k]) >> 4);
2500 template <typename ET, typename FT>
2501 void hlineSmooth(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2503 int pre_shift = n / 2;
2504 int post_shift = n - pre_shift;
2506 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2508 for (int k = 0; k < cn; k++)
2509 dst[k] = m[pre_shift-i] * src[k];
2510 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2511 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2513 int src_idx = borderInterpolate(j, len, borderType);
2514 for (int k = 0; k < cn; k++)
2515 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2518 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2519 for (int k = 0; k < cn; k++)
2520 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2521 if (borderType != BORDER_CONSTANT)
2522 for (; j < i + post_shift; j++, mid++)
2524 int src_idx = borderInterpolate(j, len, borderType);
2525 for (int k = 0; k < cn; k++)
2526 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2530 for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2532 *dst = m[0] * src[0];
2533 for (int j = 1; j < n; j++)
2534 *dst = *dst + m[j] * src[j*cn];
2537 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2539 for (int k = 0; k < cn; k++)
2540 dst[k] = m[0] * src[k];
2542 for (; j < len - i; j++)
2543 for (int k = 0; k < cn; k++)
2544 dst[k] = dst[k] + m[j] * src[j*cn + k];
2545 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2548 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2549 for (int k = 0; k < cn; k++)
2550 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2555 void hlineSmooth<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2557 int pre_shift = n / 2;
2558 int post_shift = n - pre_shift;
2560 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2562 for (int k = 0; k < cn; k++)
2563 dst[k] = m[pre_shift - i] * src[k];
2564 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2565 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2567 int src_idx = borderInterpolate(j, len, borderType);
2568 for (int k = 0; k < cn; k++)
2569 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2572 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2573 for (int k = 0; k < cn; k++)
2574 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2575 if (borderType != BORDER_CONSTANT)
2576 for (; j < i + post_shift; j++, mid++)
2578 int src_idx = borderInterpolate(j, len, borderType);
2579 for (int k = 0; k < cn; k++)
2580 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2584 int lencn = (len - post_shift + 1)*cn;
2585 for (; i < lencn - 15; i+=16, src+=16, dst+=16)
2587 v_uint16x8 v_src00, v_src01, v_src10, v_src11;
2588 v_int16x8 v_tmp0, v_tmp1;
2590 v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
2592 v_expand(v_load(src), v_src00, v_src01);
2593 v_expand(v_load(src+cn), v_src10, v_src11);
2594 v_zip(v_reinterpret_as_s16(v_src00), v_reinterpret_as_s16(v_src10), v_tmp0, v_tmp1);
2595 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul);
2596 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul);
2597 v_zip(v_reinterpret_as_s16(v_src01), v_reinterpret_as_s16(v_src11), v_tmp0, v_tmp1);
2598 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul);
2599 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul);
2602 for (; j < n - 1; j += 2)
2604 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m + j))));
2606 v_expand(v_load(src + j * cn), v_src00, v_src01);
2607 v_expand(v_load(src + (j + 1) * cn), v_src10, v_src11);
2608 v_zip(v_reinterpret_as_s16(v_src00), v_reinterpret_as_s16(v_src10), v_tmp0, v_tmp1);
2609 v_res0 += v_dotprod(v_tmp0, v_mul);
2610 v_res1 += v_dotprod(v_tmp1, v_mul);
2611 v_zip(v_reinterpret_as_s16(v_src01), v_reinterpret_as_s16(v_src11), v_tmp0, v_tmp1);
2612 v_res2 += v_dotprod(v_tmp0, v_mul);
2613 v_res3 += v_dotprod(v_tmp1, v_mul);
2617 v_int32x4 v_resj0, v_resj1, v_resj2, v_resj3;
2618 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
2619 v_expand(v_load(src + j * cn), v_src00, v_src01);
2620 v_mul_expand(v_reinterpret_as_s16(v_src00), v_mul, v_resj0, v_resj1);
2621 v_mul_expand(v_reinterpret_as_s16(v_src01), v_mul, v_resj2, v_resj3);
2628 v_store((uint16_t*)dst, v_pack(v_reinterpret_as_u32(v_res0), v_reinterpret_as_u32(v_res1)));
2629 v_store((uint16_t*)dst+8, v_pack(v_reinterpret_as_u32(v_res2), v_reinterpret_as_u32(v_res3)));
2631 for (; i < lencn; i++, src++, dst++)
2633 *dst = m[0] * src[0];
2634 for (int j = 1; j < n; j++)
2635 *dst = *dst + m[j] * src[j*cn];
2638 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2640 for (int k = 0; k < cn; k++)
2641 dst[k] = m[0] * src[k];
2643 for (; j < len - i; j++)
2644 for (int k = 0; k < cn; k++)
2645 dst[k] = dst[k] + m[j] * src[j*cn + k];
2646 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2649 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2650 for (int k = 0; k < cn; k++)
2651 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2655 template <typename ET, typename FT>
2656 void vlineSmooth1N(const FT* const * src, const FT* m, int, ET* dst, int len)
2658 const FT* src0 = src[0];
2659 for (int i = 0; i < len; i++)
2660 dst[i] = m * src0[i];
2663 void vlineSmooth1N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
2665 const ufixedpoint16* src0 = src[0];
2666 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
2668 for (; i < len - 7; i += 8)
2670 v_uint16x8 v_src0 = v_load((uint16_t*)src0 + i);
2671 v_uint32x4 v_res0, v_res1;
2672 v_mul_expand(v_src0, v_mul, v_res0, v_res1);
2673 v_pack_store(dst + i, v_rshr_pack<16>(v_res0, v_res1));
2675 for (; i < len; i++)
2676 dst[i] = m[0] * src0[i];
2678 template <typename ET, typename FT>
2679 void vlineSmooth1N1(const FT* const * src, const FT*, int, ET* dst, int len)
2681 const FT* src0 = src[0];
2682 for (int i = 0; i < len; i++)
2686 void vlineSmooth1N1<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
2688 const ufixedpoint16* src0 = src[0];
2690 for (; i < len - 7; i += 8)
2691 v_rshr_pack_store<8>(dst + i, v_load((uint16_t*)(src0 + i)));
2692 for (; i < len; i++)
2695 template <typename ET, typename FT>
2696 void vlineSmooth3N(const FT* const * src, const FT* m, int, ET* dst, int len)
2698 for (int i = 0; i < len; i++)
2699 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
2702 void vlineSmooth3N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
2704 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
2706 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
2709 ufixedpoint32 val[] = { (m[0] + m[1] + m[2]) * ufixedpoint16((uint8_t)128) };
2710 v_128_4 = v_setall_s32(*((int32_t*)val));
2714 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
2715 v_int16x8 v_mul2 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 2))));
2716 for (; i < len - 7; i += 8)
2718 v_int16x8 v_src0, v_src1;
2719 v_int16x8 v_tmp0, v_tmp1;
2721 v_src0 = v_load((int16_t*)(src[0]) + i);
2722 v_src1 = v_load((int16_t*)(src[1]) + i);
2723 v_zip(v_add_wrap(v_src0, v_128), v_add_wrap(v_src1, v_128), v_tmp0, v_tmp1);
2724 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
2725 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
2727 v_int32x4 v_resj0, v_resj1;
2728 v_src0 = v_load((int16_t*)(src[2]) + i);
2729 v_mul_expand(v_add_wrap(v_src0, v_128), v_mul2, v_resj0, v_resj1);
2736 v_uint16x8 v_res = v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1));
2737 v_pack_store(dst + i, v_res);
2739 for (; i < len; i++)
2740 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
2742 template <typename ET, typename FT>
2743 void vlineSmooth3N121(const FT* const * src, const FT*, int, ET* dst, int len)
2745 for (int i = 0; i < len; i++)
2746 dst[i] = ((FT::WT(src[0][i]) + FT::WT(src[2][i])) >> 2) + (FT::WT(src[1][i]) >> 1);
2749 void vlineSmooth3N121<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
2752 for (; i < len - 7; i += 8)
2754 v_uint32x4 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2755 v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
2756 v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
2757 v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
2758 v_uint16x8 v_res = v_rshr_pack<10>(v_src00 + v_src20 + (v_src10 << 1), v_src01 + v_src21 + (v_src11 << 1));
2759 v_pack_store(dst + i, v_res);
2761 for (; i < len; i++)
2762 dst[i] = (((uint32_t)(((uint16_t*)(src[0]))[i]) + (uint32_t)(((uint16_t*)(src[2]))[i]) + ((uint32_t)(((uint16_t*)(src[1]))[i]) << 1)) + (1 << 9)) >> 10;
2764 template <typename ET, typename FT>
2765 void vlineSmooth5N(const FT* const * src, const FT* m, int, ET* dst, int len)
2767 for (int i = 0; i < len; i++)
2768 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i] + m[3] * src[3][i] + m[4] * src[4][i];
2771 void vlineSmooth5N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
2773 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
2775 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
2778 ufixedpoint32 val[] = { (m[0] + m[1] + m[2] + m[3] + m[4]) * ufixedpoint16((uint8_t)128) };
2779 v_128_4 = v_setall_s32(*((int32_t*)val));
2783 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
2784 v_int16x8 v_mul23 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m + 2))));
2785 v_int16x8 v_mul4 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 4))));
2786 for (; i < len - 7; i += 8)
2788 v_int16x8 v_src0, v_src1;
2789 v_int16x8 v_tmp0, v_tmp1;
2791 v_src0 = v_load((int16_t*)(src[0]) + i);
2792 v_src1 = v_load((int16_t*)(src[1]) + i);
2793 v_zip(v_add_wrap(v_src0, v_128), v_add_wrap(v_src1, v_128), v_tmp0, v_tmp1);
2794 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
2795 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
2797 v_src0 = v_load((int16_t*)(src[2]) + i);
2798 v_src1 = v_load((int16_t*)(src[3]) + i);
2799 v_zip(v_add_wrap(v_src0, v_128), v_add_wrap(v_src1, v_128), v_tmp0, v_tmp1);
2800 v_res0 += v_dotprod(v_tmp0, v_mul23);
2801 v_res1 += v_dotprod(v_tmp1, v_mul23);
2803 v_int32x4 v_resj0, v_resj1;
2804 v_src0 = v_load((int16_t*)(src[4]) + i);
2805 v_mul_expand(v_add_wrap(v_src0, v_128), v_mul4, v_resj0, v_resj1);
2812 v_uint16x8 v_res = v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1));
2813 v_pack_store(dst + i, v_res);
2815 for (; i < len; i++)
2816 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i] + m[3] * src[3][i] + m[4] * src[4][i];
2818 template <typename ET, typename FT>
2819 void vlineSmooth5N14641(const FT* const * src, const FT*, int, ET* dst, int len)
2821 for (int i = 0; i < len; i++)
2822 dst[i] = (FT::WT(src[2][i])*6 + ((FT::WT(src[1][i]) + FT::WT(src[3][i]))<<2) + FT::WT(src[0][i]) + FT::WT(src[4][i])) >> 4;
2825 void vlineSmooth5N14641<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
2828 v_uint32x4 v_6 = v_setall_u32(6);
2829 for (; i < len - 7; i += 8)
2831 v_uint32x4 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2832 v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
2833 v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
2834 v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
2835 v_expand(v_load((uint16_t*)(src[3]) + i), v_src30, v_src31);
2836 v_expand(v_load((uint16_t*)(src[4]) + i), v_src40, v_src41);
2837 v_uint16x8 v_res = v_rshr_pack<12>(v_src20*v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40,
2838 v_src21*v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41);
2839 v_pack_store(dst + i, v_res);
2841 for (; i < len; i++)
2842 dst[i] = ((uint32_t)(((uint16_t*)(src[2]))[i]) * 6 +
2843 (((uint32_t)(((uint16_t*)(src[1]))[i]) + (uint32_t)(((uint16_t*)(src[3]))[i])) << 2) +
2844 (uint32_t)(((uint16_t*)(src[0]))[i]) + (uint32_t)(((uint16_t*)(src[4]))[i]) + (1 << 11)) >> 12;
2846 template <typename ET, typename FT>
2847 void vlineSmooth(const FT* const * src, const FT* m, int n, ET* dst, int len)
2849 for (int i = 0; i < len; i++)
2851 typename FT::WT val = m[0] * src[0][i];
2852 for (int j = 1; j < n; j++)
2853 val = val + m[j] * src[j][i];
2858 void vlineSmooth<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
2860 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
2862 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
2865 ufixedpoint16 msum = m[0] + m[1];
2866 for (int j = 2; j < n; j++)
2868 ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
2869 v_128_4 = v_setall_s32(*((int32_t*)val));
2873 for (; i < len - 7; i += 8)
2875 v_int16x8 v_src0, v_src1;
2876 v_int16x8 v_tmp0, v_tmp1;
2878 v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
2880 v_src0 = v_load((int16_t*)(src[0]) + i);
2881 v_src1 = v_load((int16_t*)(src[1]) + i);
2882 v_zip(v_add_wrap(v_src0, v_128), v_add_wrap(v_src1, v_128), v_tmp0, v_tmp1);
2883 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul);
2884 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul);
2887 for (; j < n - 1; j+=2)
2889 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m+j))));
2891 v_src0 = v_load((int16_t*)(src[j]) + i);
2892 v_src1 = v_load((int16_t*)(src[j+1]) + i);
2893 v_zip(v_add_wrap(v_src0, v_128), v_add_wrap(v_src1, v_128), v_tmp0, v_tmp1);
2894 v_res0 += v_dotprod(v_tmp0, v_mul);
2895 v_res1 += v_dotprod(v_tmp1, v_mul);
2899 v_int32x4 v_resj0, v_resj1;
2900 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
2901 v_src0 = v_load((int16_t*)(src[j]) + i);
2902 v_mul_expand(v_add_wrap(v_src0, v_128), v_mul, v_resj0, v_resj1);
2909 v_uint16x8 v_res = v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1));
2910 v_pack_store(dst + i, v_res);
2912 for (; i < len; i++)
2914 ufixedpoint32 val = m[0] * src[0][i];
2915 for (int j = 1; j < n; j++)
2917 val = val + m[j] * src[j][i];
2922 template <typename ET, typename FT>
2923 class fixedSmoothInvoker : public ParallelLoopBody
2926 fixedSmoothInvoker(const ET* _src, size_t _src_stride, ET* _dst, size_t _dst_stride,
2927 int _width, int _height, int _cn, const FT* _kx, int _kxlen, const FT* _ky, int _kylen, int _borderType) : ParallelLoopBody(),
2928 src(_src), dst(_dst), src_stride(_src_stride), dst_stride(_dst_stride),
2929 width(_width), height(_height), cn(_cn), kx(_kx), ky(_ky), kxlen(_kxlen), kylen(_kylen), borderType(_borderType)
2933 if ((kx[0] - FT::one()).isZero())
2934 hlineSmoothFunc = hlineSmooth1N1;
2936 hlineSmoothFunc = hlineSmooth1N;
2938 else if (kxlen == 3)
2940 if ((kx[0] - (FT::one()>>2)).isZero()&&(kx[1] - (FT::one()>>1)).isZero()&&(kx[2] - (FT::one()>>2)).isZero())
2941 hlineSmoothFunc = hlineSmooth3N121;
2943 hlineSmoothFunc = hlineSmooth3N;
2945 else if (kxlen == 5)
2947 if ((kx[2] - (FT::one()*3>>3)).isZero()&&
2948 (kx[1] - (FT::one()>>2)).isZero()&&(kx[3] - (FT::one()>>2)).isZero()&&
2949 (kx[0] - (FT::one()>>4)).isZero()&&(kx[4] - (FT::one()>>4)).isZero())
2950 hlineSmoothFunc = hlineSmooth5N14641;
2952 hlineSmoothFunc = hlineSmooth5N;
2955 hlineSmoothFunc = hlineSmooth;
2958 if ((ky[0] - FT::one()).isZero())
2959 vlineSmoothFunc = vlineSmooth1N1;
2961 vlineSmoothFunc = vlineSmooth1N;
2963 else if (kylen == 3)
2965 if ((ky[0] - (FT::one() >> 2)).isZero() && (ky[1] - (FT::one() >> 1)).isZero() && (ky[2] - (FT::one() >> 2)).isZero())
2966 vlineSmoothFunc = vlineSmooth3N121;
2968 vlineSmoothFunc = vlineSmooth3N;
2970 else if (kylen == 5)
2972 if ((ky[2] - (FT::one() * 3 >> 3)).isZero() &&
2973 (ky[1] - (FT::one() >> 2)).isZero() && (ky[3] - (FT::one() >> 2)).isZero() &&
2974 (ky[0] - (FT::one() >> 4)).isZero() && (ky[4] - (FT::one() >> 4)).isZero())
2975 vlineSmoothFunc = vlineSmooth5N14641;
2977 vlineSmoothFunc = vlineSmooth5N;
2980 vlineSmoothFunc = vlineSmooth;
2982 virtual void operator() (const Range& range) const CV_OVERRIDE
2984 AutoBuffer<FT> _buf(width*cn*kylen);
2986 AutoBuffer<FT*> _ptrs(kylen*2);
2992 for (int i = range.start; i < range.end; i++)
2994 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[0], width, borderType);
2995 vlineSmoothFunc(ptrs, ky, kylen, dst + i * dst_stride, width*cn);
2998 else if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3000 int pre_shift = kylen / 2;
3001 int post_shift = kylen - pre_shift - 1;
3002 // First line evaluation
3003 int idst = range.start;
3004 int ifrom = max(0, idst - pre_shift);
3005 int ito = idst + post_shift + 1;
3008 for (; i < min(ito, height); i++, bufline++)
3010 ptrs[bufline+kylen] = ptrs[bufline] = buf + bufline * width*cn;
3011 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3013 for (; i < ito; i++, bufline++)
3015 int src_idx = borderInterpolate(i, height, borderType);
3016 if (src_idx < ifrom)
3018 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3019 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3023 ptrs[bufline + kylen] = ptrs[bufline] = ptrs[src_idx - ifrom];
3026 for (int j = idst - pre_shift; j < 0; j++)
3028 int src_idx = borderInterpolate(j, height, borderType);
3031 ptrs[2*kylen + j] = ptrs[kylen + j] = buf + (kylen + j) * width*cn;
3032 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[kylen + j], width, borderType);
3036 ptrs[2*kylen + j] = ptrs[kylen + j] = ptrs[src_idx];
3039 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn); idst++;
3041 // border mode dependent part evaluation
3042 // i points to last src row to evaluate in convolution
3043 bufline %= kylen; ito = min(height, range.end + post_shift);
3044 for (; i < min(kylen, ito); i++, idst++)
3046 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3047 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3048 bufline = (bufline + 1) % kylen;
3049 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3051 // Points inside the border
3052 for (; i < ito; i++, idst++)
3054 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3055 bufline = (bufline + 1) % kylen;
3056 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3058 // Points that could fall below border
3059 for (; i < range.end + post_shift; i++, idst++)
3061 int src_idx = borderInterpolate(i, height, borderType);
3062 if ((i - src_idx) > kylen)
3063 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3065 ptrs[bufline + kylen] = ptrs[bufline] = ptrs[(bufline + kylen - (i - src_idx)) % kylen];
3066 bufline = (bufline + 1) % kylen;
3067 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3072 int pre_shift = kylen / 2;
3073 int post_shift = kylen - pre_shift - 1;
3074 // First line evaluation
3075 int idst = range.start;
3076 int ifrom = idst - pre_shift;
3077 int ito = min(idst + post_shift + 1, height);
3078 int i = max(0, ifrom);
3080 for (; i < ito; i++, bufline++)
3082 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3083 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3087 vlineSmooth1N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3088 else if (bufline == 3)
3089 vlineSmooth3N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3090 else if (bufline == 5)
3091 vlineSmooth5N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3093 vlineSmooth(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3096 // border mode dependent part evaluation
3097 // i points to last src row to evaluate in convolution
3098 bufline %= kylen; ito = min(height, range.end + post_shift);
3099 for (; i < min(kylen, ito); i++, idst++)
3101 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3102 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3105 vlineSmooth3N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3106 else if (bufline == 5)
3107 vlineSmooth5N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3109 vlineSmooth(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3112 // Points inside the border
3113 if (i - max(0, ifrom) >= kylen)
3115 for (; i < ito; i++, idst++)
3117 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3118 bufline = (bufline + 1) % kylen;
3119 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3122 // Points that could fall below border
3123 // i points to first src row to evaluate in convolution
3124 bufline = (bufline + 1) % kylen;
3125 for (i = idst - pre_shift; i < range.end - pre_shift; i++, idst++, bufline++)
3126 if (height - i == 3)
3127 vlineSmooth3N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3128 else if (height - i == 5)
3129 vlineSmooth5N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3131 vlineSmooth(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3135 // i points to first src row to evaluate in convolution
3136 for (i = idst - pre_shift; i < min(range.end - pre_shift, 0); i++, idst++)
3138 vlineSmooth3N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3139 else if (height == 5)
3140 vlineSmooth5N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3142 vlineSmooth(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3143 for (; i < range.end - pre_shift; i++, idst++)
3144 if (height - i == 3)
3145 vlineSmooth3N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3146 else if (height - i == 5)
3147 vlineSmooth5N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3149 vlineSmooth(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3156 size_t src_stride, dst_stride;
3157 int width, height, cn;
3161 void(*hlineSmoothFunc)(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType);
3162 void(*vlineSmoothFunc)(const FT* const * src, const FT* m, int n, ET* dst, int len);
3164 fixedSmoothInvoker(const fixedSmoothInvoker&);
3165 fixedSmoothInvoker& operator=(const fixedSmoothInvoker&);
3168 static void getGaussianKernel(int n, double sigma, int ktype, Mat& res) { res = getGaussianKernel(n, sigma, ktype); }
3169 template <typename T> static void getGaussianKernel(int n, double sigma, int, std::vector<T>& res) { res = getFixedpointGaussianKernel<T>(n, sigma); }
3171 template <typename T>
3172 static void createGaussianKernels( T & kx, T & ky, int type, Size &ksize,
3173 double sigma1, double sigma2 )
3175 int depth = CV_MAT_DEPTH(type);
3179 // automatic detection of kernel size from sigma
3180 if( ksize.width <= 0 && sigma1 > 0 )
3181 ksize.width = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3182 if( ksize.height <= 0 && sigma2 > 0 )
3183 ksize.height = cvRound(sigma2*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3185 CV_Assert( ksize.width > 0 && ksize.width % 2 == 1 &&
3186 ksize.height > 0 && ksize.height % 2 == 1 );
3188 sigma1 = std::max( sigma1, 0. );
3189 sigma2 = std::max( sigma2, 0. );
3191 getGaussianKernel( ksize.width, sigma1, std::max(depth, CV_32F), kx );
3192 if( ksize.height == ksize.width && std::abs(sigma1 - sigma2) < DBL_EPSILON )
3195 getGaussianKernel( ksize.height, sigma2, std::max(depth, CV_32F), ky );
3200 cv::Ptr<cv::FilterEngine> cv::createGaussianFilter( int type, Size ksize,
3201 double sigma1, double sigma2,
3205 createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
3207 return createSeparableLinearFilter( type, type, kx, ky, Point(-1,-1), 0, borderType );
3214 static bool ocl_GaussianBlur_8UC1(InputArray _src, OutputArray _dst, Size ksize, int ddepth,
3215 InputArray _kernelX, InputArray _kernelY, int borderType)
3217 const ocl::Device & dev = ocl::Device::getDefault();
3218 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
3220 if ( !(dev.isIntel() && (type == CV_8UC1) &&
3221 (_src.offset() == 0) && (_src.step() % 4 == 0) &&
3222 ((ksize.width == 5 && (_src.cols() % 4 == 0)) ||
3223 (ksize.width == 3 && (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)))) )
3226 Mat kernelX = _kernelX.getMat().reshape(1, 1);
3227 if (kernelX.cols % 2 != 1)
3229 Mat kernelY = _kernelY.getMat().reshape(1, 1);
3230 if (kernelY.cols % 2 != 1)
3236 Size size = _src.size();
3237 size_t globalsize[2] = { 0, 0 };
3238 size_t localsize[2] = { 0, 0 };
3240 if (ksize.width == 3)
3242 globalsize[0] = size.width / 16;
3243 globalsize[1] = size.height / 2;
3245 else if (ksize.width == 5)
3247 globalsize[0] = size.width / 4;
3248 globalsize[1] = size.height / 1;
3251 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
3252 char build_opts[1024];
3253 sprintf(build_opts, "-D %s %s%s", borderMap[borderType & ~BORDER_ISOLATED],
3254 ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
3255 ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());
3259 if (ksize.width == 3)
3260 kernel.create("gaussianBlur3x3_8UC1_cols16_rows2", cv::ocl::imgproc::gaussianBlur3x3_oclsrc, build_opts);
3261 else if (ksize.width == 5)
3262 kernel.create("gaussianBlur5x5_8UC1_cols4", cv::ocl::imgproc::gaussianBlur5x5_oclsrc, build_opts);
3267 UMat src = _src.getUMat();
3268 _dst.create(size, CV_MAKETYPE(ddepth, cn));
3269 if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
3271 UMat dst = _dst.getUMat();
3273 int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
3274 idxArg = kernel.set(idxArg, (int)src.step);
3275 idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
3276 idxArg = kernel.set(idxArg, (int)dst.step);
3277 idxArg = kernel.set(idxArg, (int)dst.rows);
3278 idxArg = kernel.set(idxArg, (int)dst.cols);
3280 return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
3288 template <> inline bool skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(int w, int h) { return w*h < 320 * 240; }
3290 static bool openvx_gaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
3291 double sigma1, double sigma2, int borderType)
3295 // automatic detection of kernel size from sigma
3296 if (ksize.width <= 0 && sigma1 > 0)
3297 ksize.width = cvRound(sigma1*6 + 1) | 1;
3298 if (ksize.height <= 0 && sigma2 > 0)
3299 ksize.height = cvRound(sigma2*6 + 1) | 1;
3301 if (_src.type() != CV_8UC1 ||
3302 _src.cols() < 3 || _src.rows() < 3 ||
3303 ksize.width != 3 || ksize.height != 3)
3306 sigma1 = std::max(sigma1, 0.);
3307 sigma2 = std::max(sigma2, 0.);
3309 if (!(sigma1 == 0.0 || (sigma1 - 0.8) < DBL_EPSILON) || !(sigma2 == 0.0 || (sigma2 - 0.8) < DBL_EPSILON) ||
3310 ovx::skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(_src.cols(), _src.rows()))
3313 Mat src = _src.getMat();
3314 Mat dst = _dst.getMat();
3316 if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
3317 return false; //Process isolated borders only
3319 switch (borderType & ~BORDER_ISOLATED)
3321 case BORDER_CONSTANT:
3322 border = VX_BORDER_CONSTANT;
3324 case BORDER_REPLICATE:
3325 border = VX_BORDER_REPLICATE;
3333 ivx::Context ctx = ovx::getOpenVXContext();
3336 if (dst.data != src.data)
3342 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3343 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
3344 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3345 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
3347 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
3348 //since OpenVX standard says nothing about thread-safety for now
3349 ivx::border_t prevBorder = ctx.immediateBorder();
3350 ctx.setImmediateBorder(border, (vx_uint8)(0));
3351 ivx::IVX_CHECK_STATUS(vxuGaussian3x3(ctx, ia, ib));
3352 ctx.setImmediateBorder(prevBorder);
3354 catch (ivx::RuntimeError & e)
3356 VX_DbgThrow(e.what());
3358 catch (ivx::WrapperError & e)
3360 VX_DbgThrow(e.what());
3368 #if IPP_VERSION_X100 == 201702 // IW 2017u2 has bug which doesn't allow use of partial inMem with tiling
3369 #define IPP_GAUSSIANBLUR_PARALLEL 0
3371 #define IPP_GAUSSIANBLUR_PARALLEL 1
3376 class ipp_gaussianBlurParallel: public ParallelLoopBody
3379 ipp_gaussianBlurParallel(::ipp::IwiImage &src, ::ipp::IwiImage &dst, int kernelSize, float sigma, ::ipp::IwiBorderType &border, bool *pOk):
3380 m_src(src), m_dst(dst), m_kernelSize(kernelSize), m_sigma(sigma), m_border(border), m_pOk(pOk) {
3383 ~ipp_gaussianBlurParallel()
3387 virtual void operator() (const Range& range) const CV_OVERRIDE
3389 CV_INSTRUMENT_REGION_IPP()
3396 ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, m_dst.m_size.width, range.end - range.start);
3397 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, m_src, m_dst, m_kernelSize, m_sigma, ::ipp::IwDefault(), m_border, tile);
3399 catch(::ipp::IwException e)
3406 ::ipp::IwiImage &m_src;
3407 ::ipp::IwiImage &m_dst;
3411 ::ipp::IwiBorderType &m_border;
3413 volatile bool *m_pOk;
3414 const ipp_gaussianBlurParallel& operator= (const ipp_gaussianBlurParallel&);
3419 static bool ipp_GaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
3420 double sigma1, double sigma2, int borderType )
3423 CV_INSTRUMENT_REGION_IPP()
3425 #if IPP_VERSION_X100 < 201800 && ((defined _MSC_VER && defined _M_IX86) || (defined __GNUC__ && defined __i386__))
3426 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
3427 return false; // bug on ia32
3429 if(sigma1 != sigma2)
3432 if(sigma1 < FLT_EPSILON)
3435 if(ksize.width != ksize.height)
3438 // Acquire data and begin processing
3441 Mat src = _src.getMat();
3442 Mat dst = _dst.getMat();
3443 ::ipp::IwiImage iwSrc = ippiGetImage(src);
3444 ::ipp::IwiImage iwDst = ippiGetImage(dst);
3445 ::ipp::IwiBorderSize borderSize = ::ipp::iwiSizeToBorderSize(ippiGetSize(ksize));
3446 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
3450 const int threads = ippiSuggestThreadsNum(iwDst, 2);
3451 if(IPP_GAUSSIANBLUR_PARALLEL && threads > 1) {
3453 ipp_gaussianBlurParallel invoker(iwSrc, iwDst, ksize.width, (float) sigma1, ippBorder, &ok);
3457 const Range range(0, (int) iwDst.m_size.height);
3458 parallel_for_(range, invoker, threads*4);
3463 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, iwSrc, iwDst, ksize.width, sigma1, ::ipp::IwDefault(), ippBorder);
3466 catch (::ipp::IwException ex)
3474 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
3481 void cv::GaussianBlur( InputArray _src, OutputArray _dst, Size ksize,
3482 double sigma1, double sigma2,
3485 CV_INSTRUMENT_REGION()
3487 int type = _src.type();
3488 Size size = _src.size();
3489 _dst.create( size, type );
3491 if( (borderType & ~BORDER_ISOLATED) != BORDER_CONSTANT &&
3492 ((borderType & BORDER_ISOLATED) != 0 || !_src.getMat().isSubmatrix()) )
3494 if( size.height == 1 )
3496 if( size.width == 1 )
3500 if( ksize.width == 1 && ksize.height == 1 )
3506 bool useOpenCL = (ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 &&
3507 ((ksize.width == 3 && ksize.height == 3) ||
3508 (ksize.width == 5 && ksize.height == 5)) &&
3509 _src.rows() > ksize.height && _src.cols() > ksize.width);
3512 int sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
3514 if(sdepth == CV_8U && ((borderType & BORDER_ISOLATED) || !_src.getMat().isSubmatrix()))
3516 std::vector<ufixedpoint16> fkx, fky;
3517 createGaussianKernels(fkx, fky, type, ksize, sigma1, sigma2);
3518 Mat src = _src.getMat();
3519 Mat dst = _dst.getMat();
3520 if (src.data == dst.data)
3522 fixedSmoothInvoker<uint8_t, ufixedpoint16> invoker(src.ptr<uint8_t>(), src.step1(), dst.ptr<uint8_t>(), dst.step1(), dst.cols, dst.rows, dst.channels(), &fkx[0], (int)fkx.size(), &fky[0], (int)fky.size(), borderType & ~BORDER_ISOLATED);
3523 parallel_for_(Range(0, dst.rows), invoker, dst.total() * cn / (double)(1 << 13));
3529 createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
3531 CV_OCL_RUN(useOpenCL, ocl_GaussianBlur_8UC1(_src, _dst, ksize, CV_MAT_DEPTH(type), kx, ky, borderType));
3533 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2 && (size_t)_src.rows() > kx.total() && (size_t)_src.cols() > kx.total(),
3534 ocl_sepFilter2D(_src, _dst, sdepth, kx, ky, Point(-1, -1), 0, borderType))
3536 Mat src = _src.getMat();
3537 Mat dst = _dst.getMat();
3540 Size wsz(src.cols, src.rows);
3541 if(!(borderType & BORDER_ISOLATED))
3542 src.locateROI( wsz, ofs );
3544 CALL_HAL(gaussianBlur, cv_hal_gaussianBlur, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, cn,
3545 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
3546 sigma1, sigma2, borderType&~BORDER_ISOLATED);
3549 openvx_gaussianBlur(src, dst, ksize, sigma1, sigma2, borderType))
3551 CV_IPP_RUN_FAST(ipp_GaussianBlur(src, dst, ksize, sigma1, sigma2, borderType));
3553 sepFilter2D(src, dst, sdepth, kx, ky, Point(-1, -1), 0, borderType);
3556 /****************************************************************************************\
3558 \****************************************************************************************/
3565 * This structure represents a two-tier histogram. The first tier (known as the
3566 * "coarse" level) is 4 bit wide and the second tier (known as the "fine" level)
3567 * is 8 bit wide. Pixels inserted in the fine level also get inserted into the
3568 * coarse bucket designated by the 4 MSBs of the fine bucket value.
3570 * The structure is aligned on 16 bits, which is a prerequisite for SIMD
3571 * instructions. Each bucket is 16 bit wide, which means that extra care must be
3572 * taken to prevent overflow.
3583 static inline void histogram_add_simd( const HT x[16], HT y[16] )
3585 v_store(y, v_load(x) + v_load(y));
3586 v_store(y + 8, v_load(x + 8) + v_load(y + 8));
3589 static inline void histogram_sub_simd( const HT x[16], HT y[16] )
3591 v_store(y, v_load(y) - v_load(x));
3592 v_store(y + 8, v_load(y + 8) - v_load(x + 8));
3598 static inline void histogram_add( const HT x[16], HT y[16] )
3601 for( i = 0; i < 16; ++i )
3602 y[i] = (HT)(y[i] + x[i]);
3605 static inline void histogram_sub( const HT x[16], HT y[16] )
3608 for( i = 0; i < 16; ++i )
3609 y[i] = (HT)(y[i] - x[i]);
3612 static inline void histogram_muladd( int a, const HT x[16],
3615 for( int i = 0; i < 16; ++i )
3616 y[i] = (HT)(y[i] + a * x[i]);
3620 medianBlur_8u_O1( const Mat& _src, Mat& _dst, int ksize )
3623 * HOP is short for Histogram OPeration. This macro makes an operation \a op on
3624 * histogram \a h for pixel value \a x. It takes care of handling both levels.
3626 #define HOP(h,x,op) \
3627 h.coarse[x>>4] op, \
3628 *((HT*)h.fine + x) op
3630 #define COP(c,j,x,op) \
3631 h_coarse[ 16*(n*c+j) + (x>>4) ] op, \
3632 h_fine[ 16 * (n*(16*c+(x>>4)) + j) + (x & 0xF) ] op
3634 int cn = _dst.channels(), m = _dst.rows, r = (ksize-1)/2;
3635 CV_Assert(cn > 0 && cn <= 4);
3636 size_t sstep = _src.step, dstep = _dst.step;
3637 Histogram CV_DECL_ALIGNED(16) H[4];
3638 HT CV_DECL_ALIGNED(16) luc[4][16];
3640 int STRIPE_SIZE = std::min( _dst.cols, 512/cn );
3642 std::vector<HT> _h_coarse(1 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
3643 std::vector<HT> _h_fine(16 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
3644 HT* h_coarse = alignPtr(&_h_coarse[0], 16);
3645 HT* h_fine = alignPtr(&_h_fine[0], 16);
3647 volatile bool useSIMD = hasSIMD128();
3650 for( int x = 0; x < _dst.cols; x += STRIPE_SIZE )
3652 int i, j, k, c, n = std::min(_dst.cols - x, STRIPE_SIZE) + r*2;
3653 const uchar* src = _src.ptr() + x*cn;
3654 uchar* dst = _dst.ptr() + (x - r)*cn;
3656 memset( h_coarse, 0, 16*n*cn*sizeof(h_coarse[0]) );
3657 memset( h_fine, 0, 16*16*n*cn*sizeof(h_fine[0]) );
3659 // First row initialization
3660 for( c = 0; c < cn; c++ )
3662 for( j = 0; j < n; j++ )
3663 COP( c, j, src[cn*j+c], += (cv::HT)(r+2) );
3665 for( i = 1; i < r; i++ )
3667 const uchar* p = src + sstep*std::min(i, m-1);
3668 for ( j = 0; j < n; j++ )
3669 COP( c, j, p[cn*j+c], ++ );
3673 for( i = 0; i < m; i++ )
3675 const uchar* p0 = src + sstep * std::max( 0, i-r-1 );
3676 const uchar* p1 = src + sstep * std::min( m-1, i+r );
3678 memset( H, 0, cn*sizeof(H[0]) );
3679 memset( luc, 0, cn*sizeof(luc[0]) );
3680 for( c = 0; c < cn; c++ )
3682 // Update column histograms for the entire row.
3683 for( j = 0; j < n; j++ )
3685 COP( c, j, p0[j*cn + c], -- );
3686 COP( c, j, p1[j*cn + c], ++ );
3689 // First column initialization
3690 for( k = 0; k < 16; ++k )
3691 histogram_muladd( 2*r+1, &h_fine[16*n*(16*c+k)], &H[c].fine[k][0] );
3696 for( j = 0; j < 2*r; ++j )
3697 histogram_add_simd( &h_coarse[16*(n*c+j)], H[c].coarse );
3699 for( j = r; j < n-r; j++ )
3701 int t = 2*r*r + 2*r, b, sum = 0;
3704 histogram_add_simd( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
3706 // Find median at coarse level
3707 for ( k = 0; k < 16 ; ++k )
3709 sum += H[c].coarse[k];
3712 sum -= H[c].coarse[k];
3716 CV_Assert( k < 16 );
3718 /* Update corresponding histogram segment */
3719 if ( luc[c][k] <= j-r )
3721 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
3722 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
3723 histogram_add_simd( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
3725 if ( luc[c][k] < j+r+1 )
3727 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
3728 luc[c][k] = (HT)(j+r+1);
3733 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
3735 histogram_sub_simd( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
3736 histogram_add_simd( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
3740 histogram_sub_simd( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
3742 /* Find median in segment */
3743 segment = H[c].fine[k];
3744 for ( b = 0; b < 16 ; b++ )
3749 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
3753 CV_Assert( b < 16 );
3759 for( j = 0; j < 2*r; ++j )
3760 histogram_add( &h_coarse[16*(n*c+j)], H[c].coarse );
3762 for( j = r; j < n-r; j++ )
3764 int t = 2*r*r + 2*r, b, sum = 0;
3767 histogram_add( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
3769 // Find median at coarse level
3770 for ( k = 0; k < 16 ; ++k )
3772 sum += H[c].coarse[k];
3775 sum -= H[c].coarse[k];
3779 CV_Assert( k < 16 );
3781 /* Update corresponding histogram segment */
3782 if ( luc[c][k] <= j-r )
3784 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
3785 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
3786 histogram_add( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
3788 if ( luc[c][k] < j+r+1 )
3790 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
3791 luc[c][k] = (HT)(j+r+1);
3796 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
3798 histogram_sub( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
3799 histogram_add( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
3803 histogram_sub( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
3805 /* Find median in segment */
3806 segment = H[c].fine[k];
3807 for ( b = 0; b < 16 ; b++ )
3812 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
3816 CV_Assert( b < 16 );
3828 medianBlur_8u_Om( const Mat& _src, Mat& _dst, int m )
3835 Size size = _dst.size();
3836 const uchar* src = _src.ptr();
3837 uchar* dst = _dst.ptr();
3838 int src_step = (int)_src.step, dst_step = (int)_dst.step;
3839 int cn = _src.channels();
3840 const uchar* src_max = src + size.height*src_step;
3841 CV_Assert(cn > 0 && cn <= 4);
3843 #define UPDATE_ACC01( pix, cn, op ) \
3847 zone0[cn][p >> 4] op; \
3850 //CV_Assert( size.height >= nx && size.width >= nx );
3851 for( x = 0; x < size.width; x++, src += cn, dst += cn )
3853 uchar* dst_cur = dst;
3854 const uchar* src_top = src;
3855 const uchar* src_bottom = src;
3857 int src_step1 = src_step, dst_step1 = dst_step;
3861 src_bottom = src_top += src_step*(size.height-1);
3862 dst_cur += dst_step*(size.height-1);
3863 src_step1 = -src_step1;
3864 dst_step1 = -dst_step1;
3868 memset( zone0, 0, sizeof(zone0[0])*cn );
3869 memset( zone1, 0, sizeof(zone1[0])*cn );
3871 for( y = 0; y <= m/2; y++ )
3873 for( c = 0; c < cn; c++ )
3877 for( k = 0; k < m*cn; k += cn )
3878 UPDATE_ACC01( src_bottom[k+c], c, ++ );
3882 for( k = 0; k < m*cn; k += cn )
3883 UPDATE_ACC01( src_bottom[k+c], c, += m/2+1 );
3887 if( (src_step1 > 0 && y < size.height-1) ||
3888 (src_step1 < 0 && size.height-y-1 > 0) )
3889 src_bottom += src_step1;
3892 for( y = 0; y < size.height; y++, dst_cur += dst_step1 )
3895 for( c = 0; c < cn; c++ )
3900 int t = s + zone0[c][k];
3911 dst_cur[c] = (uchar)k;
3914 if( y+1 == size.height )
3919 for( k = 0; k < m; k++ )
3922 int q = src_bottom[k];
3931 for( k = 0; k < m*3; k += 3 )
3933 UPDATE_ACC01( src_top[k], 0, -- );
3934 UPDATE_ACC01( src_top[k+1], 1, -- );
3935 UPDATE_ACC01( src_top[k+2], 2, -- );
3937 UPDATE_ACC01( src_bottom[k], 0, ++ );
3938 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
3939 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
3945 for( k = 0; k < m*4; k += 4 )
3947 UPDATE_ACC01( src_top[k], 0, -- );
3948 UPDATE_ACC01( src_top[k+1], 1, -- );
3949 UPDATE_ACC01( src_top[k+2], 2, -- );
3950 UPDATE_ACC01( src_top[k+3], 3, -- );
3952 UPDATE_ACC01( src_bottom[k], 0, ++ );
3953 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
3954 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
3955 UPDATE_ACC01( src_bottom[k+3], 3, ++ );
3959 if( (src_step1 > 0 && src_bottom + src_step1 < src_max) ||
3960 (src_step1 < 0 && src_bottom + src_step1 >= src) )
3961 src_bottom += src_step1;
3964 src_top += src_step1;
3974 typedef uchar value_type;
3975 typedef int arg_type;
3977 arg_type load(const uchar* ptr) { return *ptr; }
3978 void store(uchar* ptr, arg_type val) { *ptr = (uchar)val; }
3979 void operator()(arg_type& a, arg_type& b) const
3981 int t = CV_FAST_CAST_8U(a - b);
3988 typedef ushort value_type;
3989 typedef int arg_type;
3991 arg_type load(const ushort* ptr) { return *ptr; }
3992 void store(ushort* ptr, arg_type val) { *ptr = (ushort)val; }
3993 void operator()(arg_type& a, arg_type& b) const
4003 typedef short value_type;
4004 typedef int arg_type;
4006 arg_type load(const short* ptr) { return *ptr; }
4007 void store(short* ptr, arg_type val) { *ptr = (short)val; }
4008 void operator()(arg_type& a, arg_type& b) const
4018 typedef float value_type;
4019 typedef float arg_type;
4021 arg_type load(const float* ptr) { return *ptr; }
4022 void store(float* ptr, arg_type val) { *ptr = val; }
4023 void operator()(arg_type& a, arg_type& b) const
4035 typedef uchar value_type;
4036 typedef v_uint8x16 arg_type;
4038 arg_type load(const uchar* ptr) { return v_load(ptr); }
4039 void store(uchar* ptr, const arg_type &val) { v_store(ptr, val); }
4040 void operator()(arg_type& a, arg_type& b) const
4051 typedef ushort value_type;
4052 typedef v_uint16x8 arg_type;
4054 arg_type load(const ushort* ptr) { return v_load(ptr); }
4055 void store(ushort* ptr, const arg_type &val) { v_store(ptr, val); }
4056 void operator()(arg_type& a, arg_type& b) const
4067 typedef short value_type;
4068 typedef v_int16x8 arg_type;
4070 arg_type load(const short* ptr) { return v_load(ptr); }
4071 void store(short* ptr, const arg_type &val) { v_store(ptr, val); }
4072 void operator()(arg_type& a, arg_type& b) const
4083 typedef float value_type;
4084 typedef v_float32x4 arg_type;
4086 arg_type load(const float* ptr) { return v_load(ptr); }
4087 void store(float* ptr, const arg_type &val) { v_store(ptr, val); }
4088 void operator()(arg_type& a, arg_type& b) const
4098 typedef MinMax8u MinMaxVec8u;
4099 typedef MinMax16u MinMaxVec16u;
4100 typedef MinMax16s MinMaxVec16s;
4101 typedef MinMax32f MinMaxVec32f;
4105 template<class Op, class VecOp>
4107 medianBlur_SortNet( const Mat& _src, Mat& _dst, int m )
4109 typedef typename Op::value_type T;
4110 typedef typename Op::arg_type WT;
4111 typedef typename VecOp::arg_type VT;
4113 const T* src = _src.ptr<T>();
4114 T* dst = _dst.ptr<T>();
4115 int sstep = (int)(_src.step/sizeof(T));
4116 int dstep = (int)(_dst.step/sizeof(T));
4117 Size size = _dst.size();
4118 int i, j, k, cn = _src.channels();
4121 volatile bool useSIMD = hasSIMD128();
4125 if( size.width == 1 || size.height == 1 )
4127 int len = size.width + size.height - 1;
4128 int sdelta = size.height == 1 ? cn : sstep;
4129 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4130 int ddelta = size.height == 1 ? cn : dstep;
4132 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4133 for( j = 0; j < cn; j++, src++ )
4135 WT p0 = src[i > 0 ? -sdelta : 0];
4137 WT p2 = src[i < len - 1 ? sdelta : 0];
4139 op(p0, p1); op(p1, p2); op(p0, p1);
4146 for( i = 0; i < size.height; i++, dst += dstep )
4148 const T* row0 = src + std::max(i - 1, 0)*sstep;
4149 const T* row1 = src + i*sstep;
4150 const T* row2 = src + std::min(i + 1, size.height-1)*sstep;
4151 int limit = useSIMD ? cn : size.width;
4155 for( ; j < limit; j++ )
4157 int j0 = j >= cn ? j - cn : j;
4158 int j2 = j < size.width - cn ? j + cn : j;
4159 WT p0 = row0[j0], p1 = row0[j], p2 = row0[j2];
4160 WT p3 = row1[j0], p4 = row1[j], p5 = row1[j2];
4161 WT p6 = row2[j0], p7 = row2[j], p8 = row2[j2];
4163 op(p1, p2); op(p4, p5); op(p7, p8); op(p0, p1);
4164 op(p3, p4); op(p6, p7); op(p1, p2); op(p4, p5);
4165 op(p7, p8); op(p0, p3); op(p5, p8); op(p4, p7);
4166 op(p3, p6); op(p1, p4); op(p2, p5); op(p4, p7);
4167 op(p4, p2); op(p6, p4); op(p4, p2);
4171 if( limit == size.width )
4174 for( ; j <= size.width - VecOp::SIZE - cn; j += VecOp::SIZE )
4176 VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn);
4177 VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn);
4178 VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn);
4180 vop(p1, p2); vop(p4, p5); vop(p7, p8); vop(p0, p1);
4181 vop(p3, p4); vop(p6, p7); vop(p1, p2); vop(p4, p5);
4182 vop(p7, p8); vop(p0, p3); vop(p5, p8); vop(p4, p7);
4183 vop(p3, p6); vop(p1, p4); vop(p2, p5); vop(p4, p7);
4184 vop(p4, p2); vop(p6, p4); vop(p4, p2);
4185 vop.store(dst+j, p4);
4194 if( size.width == 1 || size.height == 1 )
4196 int len = size.width + size.height - 1;
4197 int sdelta = size.height == 1 ? cn : sstep;
4198 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4199 int ddelta = size.height == 1 ? cn : dstep;
4201 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4202 for( j = 0; j < cn; j++, src++ )
4204 int i1 = i > 0 ? -sdelta : 0;
4205 int i0 = i > 1 ? -sdelta*2 : i1;
4206 int i3 = i < len-1 ? sdelta : 0;
4207 int i4 = i < len-2 ? sdelta*2 : i3;
4208 WT p0 = src[i0], p1 = src[i1], p2 = src[0], p3 = src[i3], p4 = src[i4];
4210 op(p0, p1); op(p3, p4); op(p2, p3); op(p3, p4); op(p0, p2);
4211 op(p2, p4); op(p1, p3); op(p1, p2);
4218 for( i = 0; i < size.height; i++, dst += dstep )
4221 row[0] = src + std::max(i - 2, 0)*sstep;
4222 row[1] = src + std::max(i - 1, 0)*sstep;
4223 row[2] = src + i*sstep;
4224 row[3] = src + std::min(i + 1, size.height-1)*sstep;
4225 row[4] = src + std::min(i + 2, size.height-1)*sstep;
4226 int limit = useSIMD ? cn*2 : size.width;
4230 for( ; j < limit; j++ )
4233 int j1 = j >= cn ? j - cn : j;
4234 int j0 = j >= cn*2 ? j - cn*2 : j1;
4235 int j3 = j < size.width - cn ? j + cn : j;
4236 int j4 = j < size.width - cn*2 ? j + cn*2 : j3;
4237 for( k = 0; k < 5; k++ )
4239 const T* rowk = row[k];
4240 p[k*5] = rowk[j0]; p[k*5+1] = rowk[j1];
4241 p[k*5+2] = rowk[j]; p[k*5+3] = rowk[j3];
4242 p[k*5+4] = rowk[j4];
4245 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]);
4246 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]);
4247 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]);
4248 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]);
4249 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]);
4250 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]);
4251 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]);
4252 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]);
4253 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]);
4254 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]);
4255 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]);
4256 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]);
4257 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]);
4258 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]);
4259 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]);
4260 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]);
4261 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]);
4262 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]);
4263 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]);
4264 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]);
4265 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]);
4266 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]);
4267 op(p[7], p[11]); op(p[11], p[13]); op(p[11], p[12]);
4271 if( limit == size.width )
4274 for( ; j <= size.width - VecOp::SIZE - cn*2; j += VecOp::SIZE )
4277 for( k = 0; k < 5; k++ )
4279 const T* rowk = row[k];
4280 p[k*5] = vop.load(rowk+j-cn*2); p[k*5+1] = vop.load(rowk+j-cn);
4281 p[k*5+2] = vop.load(rowk+j); p[k*5+3] = vop.load(rowk+j+cn);
4282 p[k*5+4] = vop.load(rowk+j+cn*2);
4285 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]);
4286 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]);
4287 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]);
4288 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]);
4289 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]);
4290 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]);
4291 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]);
4292 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]);
4293 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]);
4294 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]);
4295 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]);
4296 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]);
4297 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]);
4298 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]);
4299 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]);
4300 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]);
4301 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]);
4302 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]);
4303 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]);
4304 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]);
4305 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]);
4306 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]);
4307 vop(p[7], p[11]); vop(p[11], p[13]); vop(p[11], p[12]);
4308 vop.store(dst+j, p[12]);
4319 static bool ocl_medianFilter(InputArray _src, OutputArray _dst, int m)
4321 size_t localsize[2] = { 16, 16 };
4322 size_t globalsize[2];
4323 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4325 if ( !((depth == CV_8U || depth == CV_16U || depth == CV_16S || depth == CV_32F) && cn <= 4 && (m == 3 || m == 5)) )
4328 Size imgSize = _src.size();
4329 bool useOptimized = (1 == cn) &&
4330 (size_t)imgSize.width >= localsize[0] * 8 &&
4331 (size_t)imgSize.height >= localsize[1] * 8 &&
4332 imgSize.width % 4 == 0 &&
4333 imgSize.height % 4 == 0 &&
4334 (ocl::Device::getDefault().isIntel());
4336 cv::String kname = format( useOptimized ? "medianFilter%d_u" : "medianFilter%d", m) ;
4337 cv::String kdefs = useOptimized ?
4338 format("-D T=%s -D T1=%s -D T4=%s%d -D cn=%d -D USE_4OPT", ocl::typeToStr(type),
4339 ocl::typeToStr(depth), ocl::typeToStr(depth), cn*4, cn)
4341 format("-D T=%s -D T1=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn) ;
4343 ocl::Kernel k(kname.c_str(), ocl::imgproc::medianFilter_oclsrc, kdefs.c_str() );
4348 UMat src = _src.getUMat();
4349 _dst.create(src.size(), type);
4350 UMat dst = _dst.getUMat();
4352 k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst));
4356 globalsize[0] = DIVUP(src.cols / 4, localsize[0]) * localsize[0];
4357 globalsize[1] = DIVUP(src.rows / 4, localsize[1]) * localsize[1];
4361 globalsize[0] = (src.cols + localsize[0] + 2) / localsize[0] * localsize[0];
4362 globalsize[1] = (src.rows + localsize[1] - 1) / localsize[1] * localsize[1];
4365 return k.run(2, globalsize, localsize, false);
4376 template <> inline bool skipSmallImages<VX_KERNEL_MEDIAN_3x3>(int w, int h) { return w*h < 1280 * 720; }
4378 static bool openvx_medianFilter(InputArray _src, OutputArray _dst, int ksize)
4380 if (_src.type() != CV_8UC1 || _dst.type() != CV_8U
4381 #ifndef VX_VERSION_1_1
4387 Mat src = _src.getMat();
4388 Mat dst = _dst.getMat();
4391 #ifdef VX_VERSION_1_1
4392 ksize != 3 ? ovx::skipSmallImages<VX_KERNEL_NON_LINEAR_FILTER>(src.cols, src.rows) :
4394 ovx::skipSmallImages<VX_KERNEL_MEDIAN_3x3>(src.cols, src.rows)
4400 ivx::Context ctx = ovx::getOpenVXContext();
4401 #ifdef VX_VERSION_1_1
4402 if ((vx_size)ksize > ctx.nonlinearMaxDimension())
4407 if (dst.data != src.data)
4413 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
4414 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
4415 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
4416 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
4418 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
4419 //since OpenVX standard says nothing about thread-safety for now
4420 ivx::border_t prevBorder = ctx.immediateBorder();
4421 ctx.setImmediateBorder(VX_BORDER_REPLICATE);
4422 #ifdef VX_VERSION_1_1
4426 ivx::IVX_CHECK_STATUS(vxuMedian3x3(ctx, ia, ib));
4428 #ifdef VX_VERSION_1_1
4433 mtx = ivx::Matrix::createFromPattern(ctx, VX_PATTERN_BOX, ksize, ksize);
4436 vx_size supportedSize;
4437 ivx::IVX_CHECK_STATUS(vxQueryContext(ctx, VX_CONTEXT_NONLINEAR_MAX_DIMENSION, &supportedSize, sizeof(supportedSize)));
4438 if ((vx_size)ksize > supportedSize)
4440 ctx.setImmediateBorder(prevBorder);
4443 Mat mask(ksize, ksize, CV_8UC1, Scalar(255));
4444 mtx = ivx::Matrix::create(ctx, VX_TYPE_UINT8, ksize, ksize);
4447 ivx::IVX_CHECK_STATUS(vxuNonLinearFilter(ctx, VX_NONLINEAR_FILTER_MEDIAN, ia, mtx, ib));
4450 ctx.setImmediateBorder(prevBorder);
4452 catch (ivx::RuntimeError & e)
4454 VX_DbgThrow(e.what());
4456 catch (ivx::WrapperError & e)
4458 VX_DbgThrow(e.what());
4469 static bool ipp_medianFilter(Mat &src0, Mat &dst, int ksize)
4471 CV_INSTRUMENT_REGION_IPP()
4473 #if IPP_VERSION_X100 < 201801
4474 // Degradations for big kernel
4481 IppiSize dstRoiSize = ippiSize(dst.cols, dst.rows), maskSize = ippiSize(ksize, ksize);
4482 IppDataType ippType = ippiGetDataType(src0.type());
4483 int channels = src0.channels();
4484 IppAutoBuffer<Ipp8u> buffer;
4486 if(src0.isSubmatrix())
4490 if(dst.data != src0.data)
4495 if(ippiFilterMedianBorderGetBufferSize(dstRoiSize, maskSize, ippType, channels, &bufSize) < 0)
4498 buffer.allocate(bufSize);
4504 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C1R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4505 else if(channels == 3)
4506 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C3R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4507 else if(channels == 4)
4508 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C4R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4513 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C1R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4514 else if(channels == 3)
4515 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C3R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4516 else if(channels == 4)
4517 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C4R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4522 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C1R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4523 else if(channels == 3)
4524 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C3R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4525 else if(channels == 4)
4526 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C4R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4531 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_32f_C1R, src.ptr<Ipp32f>(), (int)src.step, dst.ptr<Ipp32f>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
4542 void cv::medianBlur( InputArray _src0, OutputArray _dst, int ksize )
4544 CV_INSTRUMENT_REGION()
4546 CV_Assert( (ksize % 2 == 1) && (_src0.dims() <= 2 ));
4548 if( ksize <= 1 || _src0.empty() )
4554 CV_OCL_RUN(_dst.isUMat(),
4555 ocl_medianFilter(_src0,_dst, ksize))
4557 Mat src0 = _src0.getMat();
4558 _dst.create( src0.size(), src0.type() );
4559 Mat dst = _dst.getMat();
4561 CALL_HAL(medianBlur, cv_hal_medianBlur, src0.data, src0.step, dst.data, dst.step, src0.cols, src0.rows, src0.depth(),
4562 src0.channels(), ksize);
4565 openvx_medianFilter(_src0, _dst, ksize))
4567 CV_IPP_RUN_FAST(ipp_medianFilter(src0, dst, ksize));
4569 bool useSortNet = ksize == 3 || (ksize == 5
4571 && ( src0.depth() > CV_8U || src0.channels() == 2 || src0.channels() > 4 )
4578 if( dst.data != src0.data )
4583 if( src.depth() == CV_8U )
4584 medianBlur_SortNet<MinMax8u, MinMaxVec8u>( src, dst, ksize );
4585 else if( src.depth() == CV_16U )
4586 medianBlur_SortNet<MinMax16u, MinMaxVec16u>( src, dst, ksize );
4587 else if( src.depth() == CV_16S )
4588 medianBlur_SortNet<MinMax16s, MinMaxVec16s>( src, dst, ksize );
4589 else if( src.depth() == CV_32F )
4590 medianBlur_SortNet<MinMax32f, MinMaxVec32f>( src, dst, ksize );
4592 CV_Error(CV_StsUnsupportedFormat, "");
4598 cv::copyMakeBorder( src0, src, 0, 0, ksize/2, ksize/2, BORDER_REPLICATE|BORDER_ISOLATED);
4600 int cn = src0.channels();
4601 CV_Assert( src.depth() == CV_8U && (cn == 1 || cn == 3 || cn == 4) );
4603 double img_size_mp = (double)(src0.total())/(1 << 20);
4604 if( ksize <= 3 + (img_size_mp < 1 ? 12 : img_size_mp < 4 ? 6 : 2)*
4605 (CV_SIMD128 && hasSIMD128() ? 1 : 3))
4606 medianBlur_8u_Om( src, dst, ksize );
4608 medianBlur_8u_O1( src, dst, ksize );
4612 /****************************************************************************************\
4614 \****************************************************************************************/
4619 class BilateralFilter_8u_Invoker :
4620 public ParallelLoopBody
4623 BilateralFilter_8u_Invoker(Mat& _dest, const Mat& _temp, int _radius, int _maxk,
4624 int* _space_ofs, float *_space_weight, float *_color_weight) :
4625 temp(&_temp), dest(&_dest), radius(_radius),
4626 maxk(_maxk), space_ofs(_space_ofs), space_weight(_space_weight), color_weight(_color_weight)
4630 virtual void operator() (const Range& range) const CV_OVERRIDE
4632 int i, j, cn = dest->channels(), k;
4633 Size size = dest->size();
4635 int CV_DECL_ALIGNED(16) buf[4];
4636 bool haveSIMD128 = hasSIMD128();
4639 for( i = range.start; i < range.end; i++ )
4641 const uchar* sptr = temp->ptr(i+radius) + radius*cn;
4642 uchar* dptr = dest->ptr(i);
4646 for( j = 0; j < size.width; j++ )
4648 float sum = 0, wsum = 0;
4654 v_float32x4 _val0 = v_setall_f32(static_cast<float>(val0));
4655 v_float32x4 vsumw = v_setzero_f32();
4656 v_float32x4 vsumc = v_setzero_f32();
4658 for( ; k <= maxk - 4; k += 4 )
4660 v_float32x4 _valF = v_float32x4(sptr[j + space_ofs[k]],
4661 sptr[j + space_ofs[k + 1]],
4662 sptr[j + space_ofs[k + 2]],
4663 sptr[j + space_ofs[k + 3]]);
4664 v_float32x4 _val = v_abs(_valF - _val0);
4665 v_store(buf, v_round(_val));
4667 v_float32x4 _cw = v_float32x4(color_weight[buf[0]],
4668 color_weight[buf[1]],
4669 color_weight[buf[2]],
4670 color_weight[buf[3]]);
4671 v_float32x4 _sw = v_load(space_weight+k);
4672 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
4673 // details: https://github.com/opencv/opencv/issues/11004
4675 vsumc += _cw * _sw * _valF;
4677 v_float32x4 _w = _cw * _sw;
4684 float *bufFloat = (float*)buf;
4685 v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumc, vsumw, vsumc);
4686 v_store(bufFloat, sum4);
4688 wsum += bufFloat[0];
4691 for( ; k < maxk; k++ )
4693 int val = sptr[j + space_ofs[k]];
4694 float w = space_weight[k]*color_weight[std::abs(val - val0)];
4698 // overflow is not possible here => there is no need to use cv::saturate_cast
4699 dptr[j] = (uchar)cvRound(sum/wsum);
4705 for( j = 0; j < size.width*3; j += 3 )
4707 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
4708 int b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
4713 v_float32x4 vsumw = v_setzero_f32();
4714 v_float32x4 vsumb = v_setzero_f32();
4715 v_float32x4 vsumg = v_setzero_f32();
4716 v_float32x4 vsumr = v_setzero_f32();
4717 const v_float32x4 _b0 = v_setall_f32(static_cast<float>(b0));
4718 const v_float32x4 _g0 = v_setall_f32(static_cast<float>(g0));
4719 const v_float32x4 _r0 = v_setall_f32(static_cast<float>(r0));
4721 for( ; k <= maxk - 4; k += 4 )
4723 const uchar* const sptr_k0 = sptr + j + space_ofs[k];
4724 const uchar* const sptr_k1 = sptr + j + space_ofs[k+1];
4725 const uchar* const sptr_k2 = sptr + j + space_ofs[k+2];
4726 const uchar* const sptr_k3 = sptr + j + space_ofs[k+3];
4728 v_float32x4 __b = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k0)));
4729 v_float32x4 __g = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k1)));
4730 v_float32x4 __r = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k2)));
4731 v_float32x4 __z = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k3)));
4732 v_float32x4 _b, _g, _r, _z;
4734 v_transpose4x4(__b, __g, __r, __z, _b, _g, _r, _z);
4736 v_float32x4 bt = v_abs(_b -_b0);
4737 v_float32x4 gt = v_abs(_g -_g0);
4738 v_float32x4 rt = v_abs(_r -_r0);
4741 v_store(buf, v_round(bt));
4743 v_float32x4 _w = v_float32x4(color_weight[buf[0]],color_weight[buf[1]],
4744 color_weight[buf[2]],color_weight[buf[3]]);
4745 v_float32x4 _sw = v_load(space_weight+k);
4747 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
4748 // details: https://github.com/opencv/opencv/issues/11004
4750 vsumb += _w * _sw * _b;
4751 vsumg += _w * _sw * _g;
4752 vsumr += _w * _sw * _r;
4765 float *bufFloat = (float*)buf;
4766 v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumb, vsumg, vsumr);
4767 v_store(bufFloat, sum4);
4768 wsum += bufFloat[0];
4769 sum_b += bufFloat[1];
4770 sum_g += bufFloat[2];
4771 sum_r += bufFloat[3];
4775 for( ; k < maxk; k++ )
4777 const uchar* sptr_k = sptr + j + space_ofs[k];
4778 int b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
4779 float w = space_weight[k]*color_weight[std::abs(b - b0) +
4780 std::abs(g - g0) + std::abs(r - r0)];
4781 sum_b += b*w; sum_g += g*w; sum_r += r*w;
4785 b0 = cvRound(sum_b*wsum);
4786 g0 = cvRound(sum_g*wsum);
4787 r0 = cvRound(sum_r*wsum);
4788 dptr[j] = (uchar)b0; dptr[j+1] = (uchar)g0; dptr[j+2] = (uchar)r0;
4797 int radius, maxk, *space_ofs;
4798 float *space_weight, *color_weight;
4803 static bool ocl_bilateralFilter_8u(InputArray _src, OutputArray _dst, int d,
4804 double sigma_color, double sigma_space,
4808 if (ocl::Device::getDefault().isNVidia())
4812 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4813 int i, j, maxk, radius;
4815 if (depth != CV_8U || cn > 4)
4818 if (sigma_color <= 0)
4820 if (sigma_space <= 0)
4823 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
4824 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
4827 radius = cvRound(sigma_space * 1.5);
4830 radius = MAX(radius, 1);
4833 UMat src = _src.getUMat(), dst = _dst.getUMat(), temp;
4837 copyMakeBorder(src, temp, radius, radius, radius, radius, borderType);
4838 std::vector<float> _space_weight(d * d);
4839 std::vector<int> _space_ofs(d * d);
4840 float * const space_weight = &_space_weight[0];
4841 int * const space_ofs = &_space_ofs[0];
4843 // initialize space-related bilateral filter coefficients
4844 for( i = -radius, maxk = 0; i <= radius; i++ )
4845 for( j = -radius; j <= radius; j++ )
4847 double r = std::sqrt((double)i * i + (double)j * j);
4850 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
4851 space_ofs[maxk++] = (int)(i * temp.step + j * cn);
4855 String cnstr = cn > 1 ? format("%d", cn) : "";
4856 String kernelName("bilateral");
4858 if ((ocl::Device::getDefault().isIntel()) &&
4859 (ocl::Device::getDefault().type() == ocl::Device::TYPE_GPU))
4862 if (dst.cols % 4 == 0 && cn == 1) // For single channel x4 sized images.
4864 kernelName = "bilateral_float4";
4868 ocl::Kernel k(kernelName.c_str(), ocl::imgproc::bilateral_oclsrc,
4869 format("-D radius=%d -D maxk=%d -D cn=%d -D int_t=%s -D uint_t=uint%s -D convert_int_t=%s"
4870 " -D uchar_t=%s -D float_t=%s -D convert_float_t=%s -D convert_uchar_t=%s -D gauss_color_coeff=(float)%f",
4871 radius, maxk, cn, ocl::typeToStr(CV_32SC(cn)), cnstr.c_str(),
4872 ocl::convertTypeStr(CV_8U, CV_32S, cn, cvt[0]),
4873 ocl::typeToStr(type), ocl::typeToStr(CV_32FC(cn)),
4874 ocl::convertTypeStr(CV_32S, CV_32F, cn, cvt[1]),
4875 ocl::convertTypeStr(CV_32F, CV_8U, cn, cvt[2]), gauss_color_coeff));
4879 Mat mspace_weight(1, d * d, CV_32FC1, space_weight);
4880 Mat mspace_ofs(1, d * d, CV_32SC1, space_ofs);
4881 UMat ucolor_weight, uspace_weight, uspace_ofs;
4883 mspace_weight.copyTo(uspace_weight);
4884 mspace_ofs.copyTo(uspace_ofs);
4886 k.args(ocl::KernelArg::ReadOnlyNoSize(temp), ocl::KernelArg::WriteOnly(dst),
4887 ocl::KernelArg::PtrReadOnly(uspace_weight),
4888 ocl::KernelArg::PtrReadOnly(uspace_ofs));
4890 size_t globalsize[2] = { (size_t)dst.cols / sizeDiv, (size_t)dst.rows };
4891 return k.run(2, globalsize, NULL, false);
4896 bilateralFilter_8u( const Mat& src, Mat& dst, int d,
4897 double sigma_color, double sigma_space,
4900 int cn = src.channels();
4901 int i, j, maxk, radius;
4902 Size size = src.size();
4904 CV_Assert( (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data );
4906 if( sigma_color <= 0 )
4908 if( sigma_space <= 0 )
4911 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
4912 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
4915 radius = cvRound(sigma_space*1.5);
4918 radius = MAX(radius, 1);
4922 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
4924 std::vector<float> _color_weight(cn*256);
4925 std::vector<float> _space_weight(d*d);
4926 std::vector<int> _space_ofs(d*d);
4927 float* color_weight = &_color_weight[0];
4928 float* space_weight = &_space_weight[0];
4929 int* space_ofs = &_space_ofs[0];
4931 // initialize color-related bilateral filter coefficients
4933 for( i = 0; i < 256*cn; i++ )
4934 color_weight[i] = (float)std::exp(i*i*gauss_color_coeff);
4936 // initialize space-related bilateral filter coefficients
4937 for( i = -radius, maxk = 0; i <= radius; i++ )
4941 for( ; j <= radius; j++ )
4943 double r = std::sqrt((double)i*i + (double)j*j);
4946 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
4947 space_ofs[maxk++] = (int)(i*temp.step + j*cn);
4951 BilateralFilter_8u_Invoker body(dst, temp, radius, maxk, space_ofs, space_weight, color_weight);
4952 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
4956 class BilateralFilter_32f_Invoker :
4957 public ParallelLoopBody
4961 BilateralFilter_32f_Invoker(int _cn, int _radius, int _maxk, int *_space_ofs,
4962 const Mat& _temp, Mat& _dest, float _scale_index, float *_space_weight, float *_expLUT) :
4963 cn(_cn), radius(_radius), maxk(_maxk), space_ofs(_space_ofs),
4964 temp(&_temp), dest(&_dest), scale_index(_scale_index), space_weight(_space_weight), expLUT(_expLUT)
4968 virtual void operator() (const Range& range) const CV_OVERRIDE
4971 Size size = dest->size();
4973 int CV_DECL_ALIGNED(16) idxBuf[4];
4974 bool haveSIMD128 = hasSIMD128();
4977 for( i = range.start; i < range.end; i++ )
4979 const float* sptr = temp->ptr<float>(i+radius) + radius*cn;
4980 float* dptr = dest->ptr<float>(i);
4984 for( j = 0; j < size.width; j++ )
4986 float sum = 0, wsum = 0;
4987 float val0 = sptr[j];
4992 v_float32x4 vecwsum = v_setzero_f32();
4993 v_float32x4 vecvsum = v_setzero_f32();
4994 const v_float32x4 _val0 = v_setall_f32(sptr[j]);
4995 const v_float32x4 _scale_index = v_setall_f32(scale_index);
4997 for (; k <= maxk - 4; k += 4)
4999 v_float32x4 _sw = v_load(space_weight + k);
5000 v_float32x4 _val = v_float32x4(sptr[j + space_ofs[k]],
5001 sptr[j + space_ofs[k + 1]],
5002 sptr[j + space_ofs[k + 2]],
5003 sptr[j + space_ofs[k + 3]]);
5004 v_float32x4 _alpha = v_abs(_val - _val0) * _scale_index;
5006 v_int32x4 _idx = v_round(_alpha);
5007 v_store(idxBuf, _idx);
5008 _alpha -= v_cvt_f32(_idx);
5010 v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5014 v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5015 expLUT[idxBuf[1] + 1],
5016 expLUT[idxBuf[2] + 1],
5017 expLUT[idxBuf[3] + 1]);
5019 v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5025 float *bufFloat = (float*)idxBuf;
5026 v_float32x4 sum4 = v_reduce_sum4(vecwsum, vecvsum, vecwsum, vecvsum);
5027 v_store(bufFloat, sum4);
5029 wsum += bufFloat[0];
5033 for( ; k < maxk; k++ )
5035 float val = sptr[j + space_ofs[k]];
5036 float alpha = (float)(std::abs(val - val0)*scale_index);
5037 int idx = cvFloor(alpha);
5039 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5043 dptr[j] = (float)(sum/wsum);
5048 CV_Assert( cn == 3 );
5049 for( j = 0; j < size.width*3; j += 3 )
5051 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5052 float b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5057 v_float32x4 sumw = v_setzero_f32();
5058 v_float32x4 sumb = v_setzero_f32();
5059 v_float32x4 sumg = v_setzero_f32();
5060 v_float32x4 sumr = v_setzero_f32();
5061 const v_float32x4 _b0 = v_setall_f32(b0);
5062 const v_float32x4 _g0 = v_setall_f32(g0);
5063 const v_float32x4 _r0 = v_setall_f32(r0);
5064 const v_float32x4 _scale_index = v_setall_f32(scale_index);
5066 for( ; k <= maxk-4; k += 4 )
5068 v_float32x4 _sw = v_load(space_weight + k);
5070 const float* const sptr_k0 = sptr + j + space_ofs[k];
5071 const float* const sptr_k1 = sptr + j + space_ofs[k+1];
5072 const float* const sptr_k2 = sptr + j + space_ofs[k+2];
5073 const float* const sptr_k3 = sptr + j + space_ofs[k+3];
5075 v_float32x4 _v0 = v_load(sptr_k0);
5076 v_float32x4 _v1 = v_load(sptr_k1);
5077 v_float32x4 _v2 = v_load(sptr_k2);
5078 v_float32x4 _v3 = v_load(sptr_k3);
5079 v_float32x4 _b, _g, _r, _dummy;
5081 v_transpose4x4(_v0, _v1, _v2, _v3, _b, _g, _r, _dummy);
5083 v_float32x4 _bt = v_abs(_b - _b0);
5084 v_float32x4 _gt = v_abs(_g - _g0);
5085 v_float32x4 _rt = v_abs(_r - _r0);
5086 v_float32x4 _alpha = _scale_index * (_bt + _gt + _rt);
5088 v_int32x4 _idx = v_round(_alpha);
5089 v_store((int*)idxBuf, _idx);
5090 _alpha -= v_cvt_f32(_idx);
5092 v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5096 v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5097 expLUT[idxBuf[1] + 1],
5098 expLUT[idxBuf[2] + 1],
5099 expLUT[idxBuf[3] + 1]);
5101 v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5111 v_float32x4 sum4 = v_reduce_sum4(sumw, sumb, sumg, sumr);
5112 float *bufFloat = (float*)idxBuf;
5113 v_store(bufFloat, sum4);
5114 wsum += bufFloat[0];
5115 sum_b += bufFloat[1];
5116 sum_g += bufFloat[2];
5117 sum_r += bufFloat[3];
5121 for(; k < maxk; k++ )
5123 const float* sptr_k = sptr + j + space_ofs[k];
5124 float b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5125 float alpha = (float)((std::abs(b - b0) +
5126 std::abs(g - g0) + std::abs(r - r0))*scale_index);
5127 int idx = cvFloor(alpha);
5129 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5130 sum_b += b*w; sum_g += g*w; sum_r += r*w;
5137 dptr[j] = b0; dptr[j+1] = g0; dptr[j+2] = r0;
5144 int cn, radius, maxk, *space_ofs;
5147 float scale_index, *space_weight, *expLUT;
5152 bilateralFilter_32f( const Mat& src, Mat& dst, int d,
5153 double sigma_color, double sigma_space,
5156 int cn = src.channels();
5157 int i, j, maxk, radius;
5158 double minValSrc=-1, maxValSrc=1;
5159 const int kExpNumBinsPerChannel = 1 << 12;
5160 int kExpNumBins = 0;
5161 float lastExpVal = 1.f;
5162 float len, scale_index;
5163 Size size = src.size();
5165 CV_Assert( (src.type() == CV_32FC1 || src.type() == CV_32FC3) && src.data != dst.data );
5167 if( sigma_color <= 0 )
5169 if( sigma_space <= 0 )
5172 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5173 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5176 radius = cvRound(sigma_space*1.5);
5179 radius = MAX(radius, 1);
5181 // compute the min/max range for the input image (even if multichannel)
5183 minMaxLoc( src.reshape(1), &minValSrc, &maxValSrc );
5184 if(std::abs(minValSrc - maxValSrc) < FLT_EPSILON)
5190 // temporary copy of the image with borders for easy processing
5192 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
5193 const double insteadNaNValue = -5. * sigma_color;
5194 patchNaNs( temp, insteadNaNValue ); // this replacement of NaNs makes the assumption that depth values are nonnegative
5195 // TODO: make insteadNaNValue avalible in the outside function interface to control the cases breaking the assumption
5196 // allocate lookup tables
5197 std::vector<float> _space_weight(d*d);
5198 std::vector<int> _space_ofs(d*d);
5199 float* space_weight = &_space_weight[0];
5200 int* space_ofs = &_space_ofs[0];
5202 // assign a length which is slightly more than needed
5203 len = (float)(maxValSrc - minValSrc) * cn;
5204 kExpNumBins = kExpNumBinsPerChannel * cn;
5205 std::vector<float> _expLUT(kExpNumBins+2);
5206 float* expLUT = &_expLUT[0];
5208 scale_index = kExpNumBins/len;
5210 // initialize the exp LUT
5211 for( i = 0; i < kExpNumBins+2; i++ )
5213 if( lastExpVal > 0.f )
5215 double val = i / scale_index;
5216 expLUT[i] = (float)std::exp(val * val * gauss_color_coeff);
5217 lastExpVal = expLUT[i];
5223 // initialize space-related bilateral filter coefficients
5224 for( i = -radius, maxk = 0; i <= radius; i++ )
5225 for( j = -radius; j <= radius; j++ )
5227 double r = std::sqrt((double)i*i + (double)j*j);
5230 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5231 space_ofs[maxk++] = (int)(i*(temp.step/sizeof(float)) + j*cn);
5234 // parallel_for usage
5236 BilateralFilter_32f_Invoker body(cn, radius, maxk, space_ofs, temp, dst, scale_index, space_weight, expLUT);
5237 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
5241 #define IPP_BILATERAL_PARALLEL 1
5244 class ipp_bilateralFilterParallel: public ParallelLoopBody
5247 ipp_bilateralFilterParallel(::ipp::IwiImage &_src, ::ipp::IwiImage &_dst, int _radius, Ipp32f _valSquareSigma, Ipp32f _posSquareSigma, ::ipp::IwiBorderType _borderType, bool *_ok):
5248 src(_src), dst(_dst)
5253 valSquareSigma = _valSquareSigma;
5254 posSquareSigma = _posSquareSigma;
5255 borderType = _borderType;
5259 ~ipp_bilateralFilterParallel() {}
5261 virtual void operator() (const Range& range) const CV_OVERRIDE
5268 ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, dst.m_size.width, range.end - range.start);
5269 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, src, dst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), borderType, tile);
5271 catch(::ipp::IwException)
5278 ::ipp::IwiImage &src;
5279 ::ipp::IwiImage &dst;
5282 Ipp32f valSquareSigma;
5283 Ipp32f posSquareSigma;
5284 ::ipp::IwiBorderType borderType;
5287 const ipp_bilateralFilterParallel& operator= (const ipp_bilateralFilterParallel&);
5291 static bool ipp_bilateralFilter(Mat &src, Mat &dst, int d, double sigmaColor, double sigmaSpace, int borderType)
5294 CV_INSTRUMENT_REGION_IPP()
5296 int radius = IPP_MAX(((d <= 0)?cvRound(sigmaSpace*1.5):d/2), 1);
5297 Ipp32f valSquareSigma = (Ipp32f)((sigmaColor <= 0)?1:sigmaColor*sigmaColor);
5298 Ipp32f posSquareSigma = (Ipp32f)((sigmaSpace <= 0)?1:sigmaSpace*sigmaSpace);
5300 // Acquire data and begin processing
5303 ::ipp::IwiImage iwSrc = ippiGetImage(src);
5304 ::ipp::IwiImage iwDst = ippiGetImage(dst);
5305 ::ipp::IwiBorderSize borderSize(radius);
5306 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
5310 const int threads = ippiSuggestThreadsNum(iwDst, 2);
5311 if(IPP_BILATERAL_PARALLEL && threads > 1) {
5313 Range range(0, (int)iwDst.m_size.height);
5314 ipp_bilateralFilterParallel invoker(iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ippBorder, &ok);
5318 parallel_for_(range, invoker, threads*4);
5323 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), ippBorder);
5326 catch (::ipp::IwException)
5332 CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(d); CV_UNUSED(sigmaColor); CV_UNUSED(sigmaSpace); CV_UNUSED(borderType);
5340 void cv::bilateralFilter( InputArray _src, OutputArray _dst, int d,
5341 double sigmaColor, double sigmaSpace,
5344 CV_INSTRUMENT_REGION()
5346 _dst.create( _src.size(), _src.type() );
5348 CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
5349 ocl_bilateralFilter_8u(_src, _dst, d, sigmaColor, sigmaSpace, borderType))
5351 Mat src = _src.getMat(), dst = _dst.getMat();
5353 CV_IPP_RUN_FAST(ipp_bilateralFilter(src, dst, d, sigmaColor, sigmaSpace, borderType));
5355 if( src.depth() == CV_8U )
5356 bilateralFilter_8u( src, dst, d, sigmaColor, sigmaSpace, borderType );
5357 else if( src.depth() == CV_32F )
5358 bilateralFilter_32f( src, dst, d, sigmaColor, sigmaSpace, borderType );
5360 CV_Error( CV_StsUnsupportedFormat,
5361 "Bilateral filtering is only implemented for 8u and 32f images" );
5364 //////////////////////////////////////////////////////////////////////////////////////////
5367 cvSmooth( const void* srcarr, void* dstarr, int smooth_type,
5368 int param1, int param2, double param3, double param4 )
5370 cv::Mat src = cv::cvarrToMat(srcarr), dst0 = cv::cvarrToMat(dstarr), dst = dst0;
5372 CV_Assert( dst.size() == src.size() &&
5373 (smooth_type == CV_BLUR_NO_SCALE || dst.type() == src.type()) );
5378 if( smooth_type == CV_BLUR || smooth_type == CV_BLUR_NO_SCALE )
5379 cv::boxFilter( src, dst, dst.depth(), cv::Size(param1, param2), cv::Point(-1,-1),
5380 smooth_type == CV_BLUR, cv::BORDER_REPLICATE );
5381 else if( smooth_type == CV_GAUSSIAN )
5382 cv::GaussianBlur( src, dst, cv::Size(param1, param2), param3, param4, cv::BORDER_REPLICATE );
5383 else if( smooth_type == CV_MEDIAN )
5384 cv::medianBlur( src, dst, param1 );
5386 cv::bilateralFilter( src, dst, param1, param3, param4, cv::BORDER_REPLICATE );
5388 if( dst.data != dst0.data )
5389 CV_Error( CV_StsUnmatchedFormats, "The destination image does not have the proper type" );