forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts_func.cpp
More file actions
3381 lines (3009 loc) · 98.7 KB
/
ts_func.cpp
File metadata and controls
3381 lines (3009 loc) · 98.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "precomp.hpp"
#include <float.h>
#include <limits.h>
#include "opencv2/imgproc/types_c.h"
#ifdef HAVE_TEGRA_OPTIMIZATION
#include "tegra.hpp"
#endif
using namespace cv;
namespace cvtest
{
const char* getTypeName( int type )
{
static const char* type_names[] = { "8u", "8s", "16u", "16s", "32s", "32f", "64f", "ptr" };
return type_names[CV_MAT_DEPTH(type)];
}
int typeByName( const char* name )
{
int i;
for( i = 0; i < CV_DEPTH_MAX; i++ )
if( strcmp(name, getTypeName(i)) == 0 )
return i;
return -1;
}
string vec2str( const string& sep, const int* v, size_t nelems )
{
char buf[32];
string result = "";
for( size_t i = 0; i < nelems; i++ )
{
sprintf(buf, "%d", v[i]);
result += string(buf);
if( i < nelems - 1 )
result += sep;
}
return result;
}
Size randomSize(RNG& rng, double maxSizeLog)
{
double width_log = rng.uniform(0., maxSizeLog);
double height_log = rng.uniform(0., maxSizeLog - width_log);
if( (unsigned)rng % 2 != 0 )
std::swap(width_log, height_log);
Size sz;
sz.width = cvRound(exp(width_log));
sz.height = cvRound(exp(height_log));
return sz;
}
void randomSize(RNG& rng, int minDims, int maxDims, double maxSizeLog, vector<int>& sz)
{
int i, dims = rng.uniform(minDims, maxDims+1);
sz.resize(dims);
for( i = 0; i < dims; i++ )
{
double v = rng.uniform(0., maxSizeLog);
maxSizeLog -= v;
sz[i] = cvRound(exp(v));
}
for( i = 0; i < dims; i++ )
{
int j = rng.uniform(0, dims);
int k = rng.uniform(0, dims);
std::swap(sz[j], sz[k]);
}
}
int randomType(RNG& rng, int typeMask, int minChannels, int maxChannels)
{
int channels = rng.uniform(minChannels, maxChannels+1);
int depth = 0;
CV_Assert((typeMask & _OutputArray::DEPTH_MASK_ALL) != 0);
for(;;)
{
depth = rng.uniform(CV_8U, CV_64F+1);
if( ((1 << depth) & typeMask) != 0 )
break;
}
return CV_MAKETYPE(depth, channels);
}
double getMinVal(int depth)
{
depth = CV_MAT_DEPTH(depth);
double val = depth == CV_8U ? 0 : depth == CV_8S ? SCHAR_MIN : depth == CV_16U ? 0 :
depth == CV_16S ? SHRT_MIN : depth == CV_32S ? INT_MIN :
depth == CV_32F ? -FLT_MAX : depth == CV_64F ? -DBL_MAX : -1;
CV_Assert(val != -1);
return val;
}
double getMaxVal(int depth)
{
depth = CV_MAT_DEPTH(depth);
double val = depth == CV_8U ? UCHAR_MAX : depth == CV_8S ? SCHAR_MAX : depth == CV_16U ? USHRT_MAX :
depth == CV_16S ? SHRT_MAX : depth == CV_32S ? INT_MAX :
depth == CV_32F ? FLT_MAX : depth == CV_64F ? DBL_MAX : -1;
CV_Assert(val != -1);
return val;
}
Mat randomMat(RNG& rng, Size size, int type, double minVal, double maxVal, bool useRoi)
{
Size size0 = size;
if( useRoi )
{
size0.width += std::max(rng.uniform(0, 10) - 5, 0);
size0.height += std::max(rng.uniform(0, 10) - 5, 0);
}
Mat m(size0, type);
rng.fill(m, RNG::UNIFORM, minVal, maxVal);
if( size0 == size )
return m;
return m(Rect((size0.width-size.width)/2, (size0.height-size.height)/2, size.width, size.height));
}
Mat randomMat(RNG& rng, const vector<int>& size, int type, double minVal, double maxVal, bool useRoi)
{
int i, dims = (int)size.size();
vector<int> size0(dims);
vector<Range> r(dims);
bool eqsize = true;
for( i = 0; i < dims; i++ )
{
size0[i] = size[i];
r[i] = Range::all();
if( useRoi )
{
size0[i] += std::max(rng.uniform(0, 5) - 2, 0);
r[i] = Range((size0[i] - size[i])/2, (size0[i] - size[i])/2 + size[i]);
}
eqsize = eqsize && size[i] == size0[i];
}
Mat m(dims, &size0[0], type);
rng.fill(m, RNG::UNIFORM, minVal, maxVal);
if( eqsize )
return m;
return m(&r[0]);
}
void add(const Mat& _a, double alpha, const Mat& _b, double beta,
Scalar gamma, Mat& c, int ctype, bool calcAbs)
{
Mat a = _a, b = _b;
if( a.empty() || alpha == 0 )
{
// both alpha and beta can be 0, but at least one of a and b must be non-empty array,
// otherwise we do not know the size of the output (and may be type of the output, when ctype<0)
CV_Assert( !a.empty() || !b.empty() );
if( !b.empty() )
{
a = b;
alpha = beta;
b = Mat();
beta = 0;
}
}
if( b.empty() || beta == 0 )
{
b = Mat();
beta = 0;
}
else
CV_Assert(a.size == b.size);
if( ctype < 0 )
ctype = a.depth();
ctype = CV_MAKETYPE(CV_MAT_DEPTH(ctype), a.channels());
c.create(a.dims, &a.size[0], ctype);
const Mat *arrays[] = {&a, &b, &c, 0};
Mat planes[3], buf[3];
NAryMatIterator it(arrays, planes);
size_t i, nplanes = it.nplanes;
int cn=a.channels();
int total = (int)planes[0].total(), maxsize = std::min(12*12*std::max(12/cn, 1), total);
CV_Assert(planes[0].rows == 1);
buf[0].create(1, maxsize, CV_64FC(cn));
if(!b.empty())
buf[1].create(1, maxsize, CV_64FC(cn));
buf[2].create(1, maxsize, CV_64FC(cn));
scalarToRawData(gamma, buf[2].ptr(), CV_64FC(cn), (int)(maxsize*cn));
for( i = 0; i < nplanes; i++, ++it)
{
for( int j = 0; j < total; j += maxsize )
{
int j2 = std::min(j + maxsize, total);
Mat apart0 = planes[0].colRange(j, j2);
Mat cpart0 = planes[2].colRange(j, j2);
Mat apart = buf[0].colRange(0, j2 - j);
apart0.convertTo(apart, apart.type(), alpha);
size_t k, n = (j2 - j)*cn;
double* aptr = apart.ptr<double>();
const double* gptr = buf[2].ptr<double>();
if( b.empty() )
{
for( k = 0; k < n; k++ )
aptr[k] += gptr[k];
}
else
{
Mat bpart0 = planes[1].colRange((int)j, (int)j2);
Mat bpart = buf[1].colRange(0, (int)(j2 - j));
bpart0.convertTo(bpart, bpart.type(), beta);
const double* bptr = bpart.ptr<double>();
for( k = 0; k < n; k++ )
aptr[k] += bptr[k] + gptr[k];
}
if( calcAbs )
for( k = 0; k < n; k++ )
aptr[k] = fabs(aptr[k]);
apart.convertTo(cpart0, cpart0.type(), 1, 0);
}
}
}
template<typename _Tp1, typename _Tp2> inline void
convert_(const _Tp1* src, _Tp2* dst, size_t total, double alpha, double beta)
{
size_t i;
if( alpha == 1 && beta == 0 )
for( i = 0; i < total; i++ )
dst[i] = saturate_cast<_Tp2>(src[i]);
else if( beta == 0 )
for( i = 0; i < total; i++ )
dst[i] = saturate_cast<_Tp2>(src[i]*alpha);
else
for( i = 0; i < total; i++ )
dst[i] = saturate_cast<_Tp2>(src[i]*alpha + beta);
}
template<typename _Tp> inline void
convertTo(const _Tp* src, void* dst, int dtype, size_t total, double alpha, double beta)
{
switch( CV_MAT_DEPTH(dtype) )
{
case CV_8U:
convert_(src, (uchar*)dst, total, alpha, beta);
break;
case CV_8S:
convert_(src, (schar*)dst, total, alpha, beta);
break;
case CV_16U:
convert_(src, (ushort*)dst, total, alpha, beta);
break;
case CV_16S:
convert_(src, (short*)dst, total, alpha, beta);
break;
case CV_32S:
convert_(src, (int*)dst, total, alpha, beta);
break;
case CV_32F:
convert_(src, (float*)dst, total, alpha, beta);
break;
case CV_64F:
convert_(src, (double*)dst, total, alpha, beta);
break;
default:
CV_Assert(0);
}
}
void convert(const Mat& src, cv::OutputArray _dst, int dtype, double alpha, double beta)
{
if (dtype < 0) dtype = _dst.depth();
dtype = CV_MAKETYPE(CV_MAT_DEPTH(dtype), src.channels());
_dst.create(src.dims, &src.size[0], dtype);
Mat dst = _dst.getMat();
if( alpha == 0 )
{
set( dst, Scalar::all(beta) );
return;
}
if( dtype == src.type() && alpha == 1 && beta == 0 )
{
copy( src, dst );
return;
}
const Mat *arrays[]={&src, &dst, 0};
Mat planes[2];
NAryMatIterator it(arrays, planes);
size_t total = planes[0].total()*planes[0].channels();
size_t i, nplanes = it.nplanes;
for( i = 0; i < nplanes; i++, ++it)
{
const uchar* sptr = planes[0].ptr();
uchar* dptr = planes[1].ptr();
switch( src.depth() )
{
case CV_8U:
convertTo((const uchar*)sptr, dptr, dtype, total, alpha, beta);
break;
case CV_8S:
convertTo((const schar*)sptr, dptr, dtype, total, alpha, beta);
break;
case CV_16U:
convertTo((const ushort*)sptr, dptr, dtype, total, alpha, beta);
break;
case CV_16S:
convertTo((const short*)sptr, dptr, dtype, total, alpha, beta);
break;
case CV_32S:
convertTo((const int*)sptr, dptr, dtype, total, alpha, beta);
break;
case CV_32F:
convertTo((const float*)sptr, dptr, dtype, total, alpha, beta);
break;
case CV_64F:
convertTo((const double*)sptr, dptr, dtype, total, alpha, beta);
break;
}
}
}
void copy(const Mat& src, Mat& dst, const Mat& mask, bool invertMask)
{
dst.create(src.dims, &src.size[0], src.type());
if(mask.empty())
{
const Mat* arrays[] = {&src, &dst, 0};
Mat planes[2];
NAryMatIterator it(arrays, planes);
size_t i, nplanes = it.nplanes;
size_t planeSize = planes[0].total()*src.elemSize();
for( i = 0; i < nplanes; i++, ++it )
memcpy(planes[1].ptr(), planes[0].ptr(), planeSize);
return;
}
int mcn = mask.channels();
CV_Assert( src.size == mask.size && mask.depth() == CV_8U
&& (mcn == 1 || mcn == src.channels()) );
const Mat *arrays[]={&src, &dst, &mask, 0};
Mat planes[3];
NAryMatIterator it(arrays, planes);
size_t j, k, elemSize = src.elemSize(), maskElemSize = mask.elemSize(), total = planes[0].total();
size_t i, nplanes = it.nplanes;
size_t elemSize1 = src.elemSize1();
for( i = 0; i < nplanes; i++, ++it)
{
const uchar* sptr = planes[0].ptr();
uchar* dptr = planes[1].ptr();
const uchar* mptr = planes[2].ptr();
for( j = 0; j < total; j++, sptr += elemSize, dptr += elemSize, mptr += maskElemSize )
{
if( mcn == 1)
{
if( (mptr[0] != 0) ^ invertMask )
for( k = 0; k < elemSize; k++ )
dptr[k] = sptr[k];
}
else
{
for( int c = 0; c < mcn; c++ )
if( (mptr[c] != 0) ^ invertMask )
for( k = 0; k < elemSize1; k++ )
dptr[k + c * elemSize1] = sptr[k + c * elemSize1];
}
}
}
}
void set(Mat& dst, const Scalar& gamma, const Mat& mask)
{
double buf[12];
scalarToRawData(gamma, &buf, dst.type(), dst.channels());
const uchar* gptr = (const uchar*)&buf[0];
if(mask.empty())
{
const Mat* arrays[] = {&dst, 0};
Mat plane;
NAryMatIterator it(arrays, &plane);
size_t i, nplanes = it.nplanes;
size_t j, k, elemSize = dst.elemSize(), planeSize = plane.total()*elemSize;
for( k = 1; k < elemSize; k++ )
if( gptr[k] != gptr[0] )
break;
bool uniform = k >= elemSize;
for( i = 0; i < nplanes; i++, ++it )
{
uchar* dptr = plane.ptr();
if( uniform )
memset( dptr, gptr[0], planeSize );
else if( i == 0 )
{
for( j = 0; j < planeSize; j += elemSize, dptr += elemSize )
for( k = 0; k < elemSize; k++ )
dptr[k] = gptr[k];
}
else
memcpy(dptr, dst.ptr(), planeSize);
}
return;
}
int cn = dst.channels(), mcn = mask.channels();
CV_Assert( dst.size == mask.size && (mcn == 1 || mcn == cn) );
const Mat *arrays[]={&dst, &mask, 0};
Mat planes[2];
NAryMatIterator it(arrays, planes);
size_t j, k, elemSize = dst.elemSize(), maskElemSize = mask.elemSize(), total = planes[0].total();
size_t i, nplanes = it.nplanes;
size_t elemSize1 = dst.elemSize1();
for( i = 0; i < nplanes; i++, ++it)
{
uchar* dptr = planes[0].ptr();
const uchar* mptr = planes[1].ptr();
for( j = 0; j < total; j++, dptr += elemSize, mptr += maskElemSize )
{
if( mcn == 1)
{
if( mptr[0] )
for( k = 0; k < elemSize; k++ )
dptr[k] = gptr[k];
}
else
{
for( int c = 0; c < mcn; c++ )
if( mptr[c] )
for( k = 0; k < elemSize1; k++ )
dptr[k + c * elemSize1] = gptr[k + c * elemSize1];
}
}
}
}
void insert(const Mat& src, Mat& dst, int coi)
{
CV_Assert( dst.size == src.size && src.depth() == dst.depth() &&
0 <= coi && coi < dst.channels() );
const Mat* arrays[] = {&src, &dst, 0};
Mat planes[2];
NAryMatIterator it(arrays, planes);
size_t i, nplanes = it.nplanes;
size_t j, k, size0 = src.elemSize(), size1 = dst.elemSize(), total = planes[0].total();
for( i = 0; i < nplanes; i++, ++it )
{
const uchar* sptr = planes[0].ptr();
uchar* dptr = planes[1].ptr() + coi*size0;
for( j = 0; j < total; j++, sptr += size0, dptr += size1 )
{
for( k = 0; k < size0; k++ )
dptr[k] = sptr[k];
}
}
}
void extract(const Mat& src, Mat& dst, int coi)
{
dst.create( src.dims, &src.size[0], src.depth() );
CV_Assert( 0 <= coi && coi < src.channels() );
const Mat* arrays[] = {&src, &dst, 0};
Mat planes[2];
NAryMatIterator it(arrays, planes);
size_t i, nplanes = it.nplanes;
size_t j, k, size0 = src.elemSize(), size1 = dst.elemSize(), total = planes[0].total();
for( i = 0; i < nplanes; i++, ++it )
{
const uchar* sptr = planes[0].ptr() + coi*size1;
uchar* dptr = planes[1].ptr();
for( j = 0; j < total; j++, sptr += size0, dptr += size1 )
{
for( k = 0; k < size1; k++ )
dptr[k] = sptr[k];
}
}
}
void transpose(const Mat& src, Mat& dst)
{
CV_Assert(src.dims == 2);
dst.create(src.cols, src.rows, src.type());
int i, j, k, esz = (int)src.elemSize();
for( i = 0; i < dst.rows; i++ )
{
const uchar* sptr = src.ptr(0) + i*esz;
uchar* dptr = dst.ptr(i);
for( j = 0; j < dst.cols; j++, sptr += src.step[0], dptr += esz )
{
for( k = 0; k < esz; k++ )
dptr[k] = sptr[k];
}
}
}
template<typename _Tp> static void
randUniInt_(RNG& rng, _Tp* data, size_t total, int cn, const Scalar& scale, const Scalar& delta)
{
for( size_t i = 0; i < total; i += cn )
for( int k = 0; k < cn; k++ )
{
int val = cvFloor( randInt(rng)*scale[k] + delta[k] );
data[i + k] = saturate_cast<_Tp>(val);
}
}
template<typename _Tp> static void
randUniFlt_(RNG& rng, _Tp* data, size_t total, int cn, const Scalar& scale, const Scalar& delta)
{
for( size_t i = 0; i < total; i += cn )
for( int k = 0; k < cn; k++ )
{
double val = randReal(rng)*scale[k] + delta[k];
data[i + k] = saturate_cast<_Tp>(val);
}
}
void randUni( RNG& rng, Mat& a, const Scalar& param0, const Scalar& param1 )
{
Scalar scale = param0;
Scalar delta = param1;
double C = a.depth() < CV_32F ? 1./(65536.*65536.) : 1.;
for( int k = 0; k < 4; k++ )
{
double s = scale.val[k] - delta.val[k];
if( s >= 0 )
scale.val[k] = s;
else
{
delta.val[k] = scale.val[k];
scale.val[k] = -s;
}
scale.val[k] *= C;
}
const Mat *arrays[]={&a, 0};
Mat plane;
NAryMatIterator it(arrays, &plane);
size_t i, nplanes = it.nplanes;
int depth = a.depth(), cn = a.channels();
size_t total = plane.total()*cn;
for( i = 0; i < nplanes; i++, ++it )
{
switch( depth )
{
case CV_8U:
randUniInt_(rng, plane.ptr<uchar>(), total, cn, scale, delta);
break;
case CV_8S:
randUniInt_(rng, plane.ptr<schar>(), total, cn, scale, delta);
break;
case CV_16U:
randUniInt_(rng, plane.ptr<ushort>(), total, cn, scale, delta);
break;
case CV_16S:
randUniInt_(rng, plane.ptr<short>(), total, cn, scale, delta);
break;
case CV_32S:
randUniInt_(rng, plane.ptr<int>(), total, cn, scale, delta);
break;
case CV_32F:
randUniFlt_(rng, plane.ptr<float>(), total, cn, scale, delta);
break;
case CV_64F:
randUniFlt_(rng, plane.ptr<double>(), total, cn, scale, delta);
break;
default:
CV_Assert(0);
}
}
}
template<typename _Tp> static void
erode_(const Mat& src, Mat& dst, const vector<int>& ofsvec)
{
int width = dst.cols*src.channels(), n = (int)ofsvec.size();
const int* ofs = &ofsvec[0];
for( int y = 0; y < dst.rows; y++ )
{
const _Tp* sptr = src.ptr<_Tp>(y);
_Tp* dptr = dst.ptr<_Tp>(y);
for( int x = 0; x < width; x++ )
{
_Tp result = sptr[x + ofs[0]];
for( int i = 1; i < n; i++ )
result = std::min(result, sptr[x + ofs[i]]);
dptr[x] = result;
}
}
}
template<typename _Tp> static void
dilate_(const Mat& src, Mat& dst, const vector<int>& ofsvec)
{
int width = dst.cols*src.channels(), n = (int)ofsvec.size();
const int* ofs = &ofsvec[0];
for( int y = 0; y < dst.rows; y++ )
{
const _Tp* sptr = src.ptr<_Tp>(y);
_Tp* dptr = dst.ptr<_Tp>(y);
for( int x = 0; x < width; x++ )
{
_Tp result = sptr[x + ofs[0]];
for( int i = 1; i < n; i++ )
result = std::max(result, sptr[x + ofs[i]]);
dptr[x] = result;
}
}
}
void erode(const Mat& _src, Mat& dst, const Mat& _kernel, Point anchor,
int borderType, const Scalar& _borderValue)
{
//if( _src.type() == CV_16UC3 && _src.size() == Size(1, 2) )
// putchar('*');
Mat kernel = _kernel, src;
Scalar borderValue = _borderValue;
if( kernel.empty() )
kernel = Mat::ones(3, 3, CV_8U);
else
{
CV_Assert( kernel.type() == CV_8U );
}
if( anchor == Point(-1,-1) )
anchor = Point(kernel.cols/2, kernel.rows/2);
if( borderType == BORDER_CONSTANT )
borderValue = getMaxVal(src.depth());
copyMakeBorder(_src, src, anchor.y, kernel.rows - anchor.y - 1,
anchor.x, kernel.cols - anchor.x - 1,
borderType, borderValue);
dst.create( _src.size(), src.type() );
vector<int> ofs;
int step = (int)(src.step/src.elemSize1()), cn = src.channels();
for( int i = 0; i < kernel.rows; i++ )
for( int j = 0; j < kernel.cols; j++ )
if( kernel.at<uchar>(i, j) != 0 )
ofs.push_back(i*step + j*cn);
if( ofs.empty() )
ofs.push_back(anchor.y*step + anchor.x*cn);
switch( src.depth() )
{
case CV_8U:
erode_<uchar>(src, dst, ofs);
break;
case CV_8S:
erode_<schar>(src, dst, ofs);
break;
case CV_16U:
erode_<ushort>(src, dst, ofs);
break;
case CV_16S:
erode_<short>(src, dst, ofs);
break;
case CV_32S:
erode_<int>(src, dst, ofs);
break;
case CV_32F:
erode_<float>(src, dst, ofs);
break;
case CV_64F:
erode_<double>(src, dst, ofs);
break;
default:
CV_Assert(0);
}
}
void dilate(const Mat& _src, Mat& dst, const Mat& _kernel, Point anchor,
int borderType, const Scalar& _borderValue)
{
Mat kernel = _kernel, src;
Scalar borderValue = _borderValue;
if( kernel.empty() )
kernel = Mat::ones(3, 3, CV_8U);
else
{
CV_Assert( kernel.type() == CV_8U );
}
if( anchor == Point(-1,-1) )
anchor = Point(kernel.cols/2, kernel.rows/2);
if( borderType == BORDER_CONSTANT )
borderValue = getMinVal(src.depth());
copyMakeBorder(_src, src, anchor.y, kernel.rows - anchor.y - 1,
anchor.x, kernel.cols - anchor.x - 1,
borderType, borderValue);
dst.create( _src.size(), src.type() );
vector<int> ofs;
int step = (int)(src.step/src.elemSize1()), cn = src.channels();
for( int i = 0; i < kernel.rows; i++ )
for( int j = 0; j < kernel.cols; j++ )
if( kernel.at<uchar>(i, j) != 0 )
ofs.push_back(i*step + j*cn);
if( ofs.empty() )
ofs.push_back(anchor.y*step + anchor.x*cn);
switch( src.depth() )
{
case CV_8U:
dilate_<uchar>(src, dst, ofs);
break;
case CV_8S:
dilate_<schar>(src, dst, ofs);
break;
case CV_16U:
dilate_<ushort>(src, dst, ofs);
break;
case CV_16S:
dilate_<short>(src, dst, ofs);
break;
case CV_32S:
dilate_<int>(src, dst, ofs);
break;
case CV_32F:
dilate_<float>(src, dst, ofs);
break;
case CV_64F:
dilate_<double>(src, dst, ofs);
break;
default:
CV_Assert(0);
}
}
template<typename _Tp> static void
filter2D_(const Mat& src, Mat& dst, const vector<int>& ofsvec, const vector<double>& coeffvec)
{
const int* ofs = &ofsvec[0];
const double* coeff = &coeffvec[0];
int width = dst.cols*dst.channels(), ncoeffs = (int)ofsvec.size();
for( int y = 0; y < dst.rows; y++ )
{
const _Tp* sptr = src.ptr<_Tp>(y);
double* dptr = dst.ptr<double>(y);
for( int x = 0; x < width; x++ )
{
double s = 0;
for( int i = 0; i < ncoeffs; i++ )
s += sptr[x + ofs[i]]*coeff[i];
dptr[x] = s;
}
}
}
void filter2D(const Mat& _src, Mat& dst, int ddepth, const Mat& kernel,
Point anchor, double delta, int borderType, const Scalar& _borderValue)
{
Mat src, _dst;
Scalar borderValue = _borderValue;
CV_Assert( kernel.type() == CV_32F || kernel.type() == CV_64F );
if( anchor == Point(-1,-1) )
anchor = Point(kernel.cols/2, kernel.rows/2);
if( borderType == BORDER_CONSTANT )
borderValue = getMinVal(src.depth());
copyMakeBorder(_src, src, anchor.y, kernel.rows - anchor.y - 1,
anchor.x, kernel.cols - anchor.x - 1,
borderType, borderValue);
_dst.create( _src.size(), CV_MAKETYPE(CV_64F, src.channels()) );
vector<int> ofs;
vector<double> coeff(kernel.rows*kernel.cols);
Mat cmat(kernel.rows, kernel.cols, CV_64F, &coeff[0]);
convert(kernel, cmat, cmat.type());
int step = (int)(src.step/src.elemSize1()), cn = src.channels();
for( int i = 0; i < kernel.rows; i++ )
for( int j = 0; j < kernel.cols; j++ )
ofs.push_back(i*step + j*cn);
switch( src.depth() )
{
case CV_8U:
filter2D_<uchar>(src, _dst, ofs, coeff);
break;
case CV_8S:
filter2D_<schar>(src, _dst, ofs, coeff);
break;
case CV_16U:
filter2D_<ushort>(src, _dst, ofs, coeff);
break;
case CV_16S:
filter2D_<short>(src, _dst, ofs, coeff);
break;
case CV_32S:
filter2D_<int>(src, _dst, ofs, coeff);
break;
case CV_32F:
filter2D_<float>(src, _dst, ofs, coeff);
break;
case CV_64F:
filter2D_<double>(src, _dst, ofs, coeff);
break;
default:
CV_Assert(0);
}
convert(_dst, dst, ddepth, 1, delta);
}
static int borderInterpolate( int p, int len, int borderType )
{
if( (unsigned)p < (unsigned)len )
;
else if( borderType == BORDER_REPLICATE )
p = p < 0 ? 0 : len - 1;
else if( borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101 )
{
int delta = borderType == BORDER_REFLECT_101;
if( len == 1 )
return 0;
do
{
if( p < 0 )
p = -p - 1 + delta;
else
p = len - 1 - (p - len) - delta;
}
while( (unsigned)p >= (unsigned)len );
}
else if( borderType == BORDER_WRAP )
{
if( p < 0 )
p -= ((p-len+1)/len)*len;
if( p >= len )
p %= len;
}
else if( borderType == BORDER_CONSTANT )
p = -1;
else
CV_Error( Error::StsBadArg, "Unknown/unsupported border type" );
return p;
}
void copyMakeBorder(const Mat& src, Mat& dst, int top, int bottom, int left, int right,
int borderType, const Scalar& borderValue)
{
dst.create(src.rows + top + bottom, src.cols + left + right, src.type());
int i, j, k, esz = (int)src.elemSize();
int width = src.cols*esz, width1 = dst.cols*esz;
if( borderType == BORDER_CONSTANT )
{
vector<uchar> valvec((src.cols + left + right)*esz);
uchar* val = &valvec[0];
scalarToRawData(borderValue, val, src.type(), (src.cols + left + right)*src.channels());
left *= esz;
right *= esz;
for( i = 0; i < src.rows; i++ )
{
const uchar* sptr = src.ptr(i);
uchar* dptr = dst.ptr(i + top) + left;
for( j = 0; j < left; j++ )
dptr[j - left] = val[j];
if( dptr != sptr )
for( j = 0; j < width; j++ )
dptr[j] = sptr[j];
for( j = 0; j < right; j++ )
dptr[j + width] = val[j];
}
for( i = 0; i < top; i++ )
{
uchar* dptr = dst.ptr(i);
for( j = 0; j < width1; j++ )
dptr[j] = val[j];
}
for( i = 0; i < bottom; i++ )
{
uchar* dptr = dst.ptr(i + top + src.rows);
for( j = 0; j < width1; j++ )
dptr[j] = val[j];
}
}
else
{
vector<int> tabvec((left + right)*esz + 1);
int* ltab = &tabvec[0];
int* rtab = &tabvec[left*esz];
for( i = 0; i < left; i++ )
{
j = borderInterpolate(i - left, src.cols, borderType)*esz;
for( k = 0; k < esz; k++ )
ltab[i*esz + k] = j + k;
}
for( i = 0; i < right; i++ )
{
j = borderInterpolate(src.cols + i, src.cols, borderType)*esz;
for( k = 0; k < esz; k++ )
rtab[i*esz + k] = j + k;
}
left *= esz;
right *= esz;
for( i = 0; i < src.rows; i++ )
{
const uchar* sptr = src.ptr(i);
uchar* dptr = dst.ptr(i + top);
for( j = 0; j < left; j++ )
dptr[j] = sptr[ltab[j]];
if( dptr + left != sptr )
{
for( j = 0; j < width; j++ )
dptr[j + left] = sptr[j];
}
for( j = 0; j < right; j++ )
dptr[j + left + width] = sptr[rtab[j]];
}
for( i = 0; i < top; i++ )
{
j = borderInterpolate(i - top, src.rows, borderType);
const uchar* sptr = dst.ptr(j + top);
uchar* dptr = dst.ptr(i);
for( k = 0; k < width1; k++ )
dptr[k] = sptr[k];
}
for( i = 0; i < bottom; i++ )
{
j = borderInterpolate(i + src.rows, src.rows, borderType);
const uchar* sptr = dst.ptr(j + top);
uchar* dptr = dst.ptr(i + top + src.rows);
for( k = 0; k < width1; k++ )
dptr[k] = sptr[k];
}
}
}
template<typename _Tp> static void
minMaxLoc_(const _Tp* src, size_t total, size_t startidx,
double* _minval, double* _maxval,
size_t* _minpos, size_t* _maxpos,
const uchar* mask)
{
_Tp maxval = saturate_cast<_Tp>(*_maxval), minval = saturate_cast<_Tp>(*_minval);