VAT  3.0
Video Analysis Tool
RandUtils.h
1 #pragma once
2 
3 /*// gaussian 3x3 pattern, based on 'floor(fspecial('gaussian', 3, 1)*256)'
4 static const int s_nSamplesInitPatternWidth = 3;
5 static const int s_nSamplesInitPatternHeight = 3;
6 static const int s_nSamplesInitPatternTot = 256;
7 static const int s_anSamplesInitPattern[s_nSamplesInitPatternHeight][s_nSamplesInitPatternWidth] = {
8  {19, 32, 19,},
9  {32, 52, 32,},
10  {19, 32, 19,},
11 };*/
12 
13 // gaussian 7x7 pattern, based on 'floor(fspecial('gaussian',7,2)*512)'
14 static const int s_nSamplesInitPatternWidth = 7;
15 static const int s_nSamplesInitPatternHeight = 7;
16 static const int s_nSamplesInitPatternTot = 512;
17 static const int s_anSamplesInitPattern[s_nSamplesInitPatternHeight][s_nSamplesInitPatternWidth] = {
18  {2, 4, 6, 7, 6, 4, 2,},
19  {4, 8, 12, 14, 12, 8, 4,},
20  {6, 12, 21, 25, 21, 12, 6,},
21  {7, 14, 25, 28, 25, 14, 7,},
22  {6, 12, 21, 25, 21, 12, 6,},
23  {4, 8, 12, 14, 12, 8, 4,},
24  {2, 4, 6, 7, 6, 4, 2,},
25 };
26 
28 static inline void getRandSamplePosition(int& x_sample, int& y_sample, const int x_orig, const int y_orig, const int border, const cv::Size& imgsize) {
29  int r = 1+rand()%s_nSamplesInitPatternTot;
30  for(x_sample=0; x_sample<s_nSamplesInitPatternWidth; ++x_sample) {
31  for(y_sample=0; y_sample<s_nSamplesInitPatternHeight; ++y_sample) {
32  r -= s_anSamplesInitPattern[y_sample][x_sample];
33  if(r<=0)
34  goto stop;
35  }
36  }
37  stop:
38  x_sample += x_orig-s_nSamplesInitPatternWidth/2;
39  y_sample += y_orig-s_nSamplesInitPatternHeight/2;
40  if(x_sample<border)
41  x_sample = border;
42  else if(x_sample>=imgsize.width-border)
43  x_sample = imgsize.width-border-1;
44  if(y_sample<border)
45  y_sample = border;
46  else if(y_sample>=imgsize.height-border)
47  y_sample = imgsize.height-border-1;
48 }
49 
50 // simple 8-connected (3x3) neighbors pattern
51 static const int s_anNeighborPatternSize_3x3 = 8;
52 static const int s_anNeighborPattern_3x3[8][2] = {
53  {-1, 1}, { 0, 1}, { 1, 1},
54  {-1, 0}, { 1, 0},
55  {-1,-1}, { 0,-1}, { 1,-1},
56 };
57 
59 static inline void getRandNeighborPosition_3x3(int& x_neighbor, int& y_neighbor, const int x_orig, const int y_orig, const int border, const cv::Size& imgsize) {
60  int r = rand()%s_anNeighborPatternSize_3x3;
61  x_neighbor = x_orig+s_anNeighborPattern_3x3[r][0];
62  y_neighbor = y_orig+s_anNeighborPattern_3x3[r][1];
63  if(x_neighbor<border)
64  x_neighbor = border;
65  else if(x_neighbor>=imgsize.width-border)
66  x_neighbor = imgsize.width-border-1;
67  if(y_neighbor<border)
68  y_neighbor = border;
69  else if(y_neighbor>=imgsize.height-border)
70  y_neighbor = imgsize.height-border-1;
71 }
72 
73 // 5x5 neighbors pattern
74 static const int s_anNeighborPatternSize_5x5 = 24;
75 static const int s_anNeighborPattern_5x5[24][2] = {
76  {-2, 2}, {-1, 2}, { 0, 2}, { 1, 2}, { 2, 2},
77  {-2, 1}, {-1, 1}, { 0, 1}, { 1, 1}, { 2, 1},
78  {-2, 0}, {-1, 0}, { 1, 0}, { 2, 0},
79  {-2,-1}, {-1,-1}, { 0,-1}, { 1,-1}, { 2,-1},
80  {-2,-2}, {-1,-2}, { 0,-2}, { 1,-2}, { 2,-2},
81 };
82 
84 static inline void getRandNeighborPosition_5x5(int& x_neighbor, int& y_neighbor, const int x_orig, const int y_orig, const int border, const cv::Size& imgsize) {
85  int r = rand()%s_anNeighborPatternSize_5x5;
86  x_neighbor = x_orig+s_anNeighborPattern_5x5[r][0];
87  y_neighbor = y_orig+s_anNeighborPattern_5x5[r][1];
88  if(x_neighbor<border)
89  x_neighbor = border;
90  else if(x_neighbor>=imgsize.width-border)
91  x_neighbor = imgsize.width-border-1;
92  if(y_neighbor<border)
93  y_neighbor = border;
94  else if(y_neighbor>=imgsize.height-border)
95  y_neighbor = imgsize.height-border-1;
96 }