JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
Surprise.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
19
20// ##############################################################################################################
21Surprise::Surprise(std::string const & instance) :
22 jevois::Component(instance)
23{
24 itsSaliency = addSubComponent<Saliency>("saliency");
25}
26
27// ##############################################################################################################
30
31
32// ##############################################################################################################
33namespace
34{
35// ######################################################################
36// ##### Fast Log Function
37// ######################################################################
38 static const double TBL_LOG_P[] = {
39 /* ONE */ 1.0,
40 /* TWO52 */ 4503599627370496.0,
41 /* LN2HI */ 6.93147180369123816490e-01, /* 3fe62e42, fee00000 */
42 /* LN2LO */ 1.90821492927058770002e-10, /* 3dea39ef, 35793c76 */
43 /* A1 */ -6.88821452420390473170286327331268694251775741577e-0002,
44 /* A2 */ 1.97493380704769294631262255279580131173133850098e+0000,
45 /* A3 */ 2.24963218866067560242072431719861924648284912109e+0000,
46 /* A4 */ -9.02975906958474405783476868236903101205825805664e-0001,
47 /* A5 */ -1.47391630715542865104339398385491222143173217773e+0000,
48 /* A6 */ 1.86846544648220058704168877738993614912033081055e+0000,
49 /* A7 */ 1.82277370459347465292410106485476717352867126465e+0000,
50 /* A8 */ 1.25295479915214102994980294170090928673744201660e+0000,
51 /* A9 */ 1.96709676945198275177517643896862864494323730469e+0000,
52 /* A10 */ -4.00127989749189894030934055990655906498432159424e-0001,
53 /* A11 */ 3.01675528558798333733648178167641162872314453125e+0000,
54 /* A12 */ -9.52325445049240770778453679668018594384193420410e-0001,
55 /* B1 */ -1.25041641589283658575482149899471551179885864258e-0001,
56 /* B2 */ 1.87161713283355151891381127914642725337613123482e+0000,
57 /* B3 */ -1.89082956295731507978530316904652863740921020508e+0000,
58 /* B4 */ -2.50562891673640253387134180229622870683670043945e+0000,
59 /* B5 */ 1.64822828085258366037635369139024987816810607910e+0000,
60 /* B6 */ -1.24409107065868340669112512841820716857910156250e+0000,
61 /* B7 */ 1.70534231658220414296067701798165217041969299316e+0000,
62 /* B8 */ 1.99196833784655646937267192697618156671524047852e+0000,
63 };
64
65#define ONE TBL_LOG_P[0]
66#define TWO52 TBL_LOG_P[1]
67#define LN2HI TBL_LOG_P[2]
68#define LN2LO TBL_LOG_P[3]
69#define A1 TBL_LOG_P[4]
70#define A2 TBL_LOG_P[5]
71#define A3 TBL_LOG_P[6]
72#define A4 TBL_LOG_P[7]
73#define A5 TBL_LOG_P[8]
74#define A6 TBL_LOG_P[9]
75#define A7 TBL_LOG_P[10]
76#define A8 TBL_LOG_P[11]
77#define A9 TBL_LOG_P[12]
78#define A10 TBL_LOG_P[13]
79#define A11 TBL_LOG_P[14]
80#define A12 TBL_LOG_P[15]
81#define B1 TBL_LOG_P[16]
82#define B2 TBL_LOG_P[17]
83#define B3 TBL_LOG_P[18]
84#define B4 TBL_LOG_P[19]
85#define B5 TBL_LOG_P[20]
86#define B6 TBL_LOG_P[21]
87#define B7 TBL_LOG_P[22]
88#define B8 TBL_LOG_P[23]
89
90 static const double _TBL_log[] = {
91 9.47265623608246343e-02, 1.05567010464380857e+01, -2.35676082856530300e+00,
92 9.66796869131412717e-02, 1.03434344062203838e+01, -2.33635196153499791e+00,
93 9.86328118117651004e-02, 1.01386139321308306e+01, -2.31635129573594156e+00,
94 1.00585936733578435e-01, 9.94174764856737347e+00, -2.29674282498938709e+00,
95 1.02539062499949152e-01, 9.75238095238578850e+00, -2.27751145544242561e+00,
96 1.04492186859904843e-01, 9.57009351656812157e+00, -2.25864297726331742e+00,
97 1.06445312294918631e-01, 9.39449543094380957e+00, -2.24012392529694537e+00,
98 1.08398437050250693e-01, 9.22522526350104144e+00, -2.22194160843615762e+00,
99 1.10351562442130582e-01, 9.06194690740703912e+00, -2.20408398741152212e+00,
100 1.12304686894746625e-01, 8.90434787407592943e+00, -2.18653968262558962e+00,
101 1.14257811990227776e-01, 8.75213679118525256e+00, -2.16929787526329321e+00,
102 1.16210936696872255e-01, 8.60504207627572093e+00, -2.15234831939887172e+00,
103 1.18164061975360682e-01, 8.46280995492959498e+00, -2.13568126444263484e+00,
104 1.20117187499996322e-01, 8.32520325203277523e+00, -2.11928745022706622e+00,
105 1.22070312499895098e-01, 8.19200000000703987e+00, -2.10315806829801133e+00,
106 1.24023436774175100e-01, 8.06299217317146599e+00, -2.08728472499318229e+00,
107 1.26953123746900931e-01, 7.87692315467275872e+00, -2.06393736501443570e+00,
108 1.30859374098123454e-01, 7.64179109744297769e+00, -2.03363201254049386e+00,
109 1.34765623780674720e-01, 7.42028992220936967e+00, -2.00421812948999545e+00,
110 1.38671874242985771e-01, 7.21126764500034501e+00, -1.97564475345722457e+00,
111 1.42578124148616536e-01, 7.01369867201821506e+00, -1.94786518986246371e+00,
112 1.46484374166731490e-01, 6.82666670549979404e+00, -1.92083651719164372e+00,
113 1.50390624434435488e-01, 6.64935067435644189e+00, -1.89451920694646070e+00,
114 1.54296874339723084e-01, 6.48101268596180624e+00, -1.86887677685174936e+00,
115 1.58203124999987427e-01, 6.32098765432149001e+00, -1.84387547036714849e+00,
116 1.62109374999815342e-01, 6.16867469880220742e+00, -1.81948401724404896e+00,
117 1.66015624243955634e-01, 6.02352943919619310e+00, -1.79567337310324682e+00,
118 1.69921874302298687e-01, 5.88505749542848644e+00, -1.77241651049093640e+00,
119 1.73828124315277527e-01, 5.75280901142480605e+00, -1.74968825924644555e+00,
120 1.77734374286237506e-01, 5.62637364896854919e+00, -1.72746512253855222e+00,
121 1.81640624146994889e-01, 5.50537636993989743e+00, -1.70572513658236602e+00,
122 1.85546874316304788e-01, 5.38947370406942916e+00, -1.68444773712372431e+00,
123 1.89453124405085355e-01, 5.27835053203882509e+00, -1.66361364967629299e+00,
124 1.93359374570531595e-01, 5.17171718320401652e+00, -1.64320477712600699e+00,
125 1.97265624263334577e-01, 5.06930694962380368e+00, -1.62320411193263148e+00,
126 2.01171874086291030e-01, 4.97087380898513764e+00, -1.60359564135180399e+00,
127 2.05078123979995308e-01, 4.87619050044336610e+00, -1.58436427985572159e+00,
128 2.08984373896073439e-01, 4.78504675424820736e+00, -1.56549579585994181e+00,
129 2.12890623963011144e-01, 4.69724772930228163e+00, -1.54697674768135762e+00,
130 2.16796874723889421e-01, 4.61261261848719517e+00, -1.52879442500076479e+00,
131 2.20703124198150608e-01, 4.53097346778917753e+00, -1.51093680996032553e+00,
132 2.24609374375627030e-01, 4.45217392541970725e+00, -1.49339249945607477e+00,
133 2.28515625000094036e-01, 4.37606837606657528e+00, -1.47615069024134016e+00,
134 2.32421873924349737e-01, 4.30252102831546246e+00, -1.45920113655598627e+00,
135 2.36328123935216378e-01, 4.23140497774241098e+00, -1.44253408394829741e+00,
136 2.40234375000066919e-01, 4.16260162601510064e+00, -1.42614026966681173e+00,
137 2.44140623863132178e-01, 4.09600001907347711e+00, -1.41001089239381727e+00,
138 2.48046874999894917e-01, 4.03149606299383390e+00, -1.39413753858134015e+00,
139 2.53906248590769879e-01, 3.93846156032078243e+00, -1.37079018013412401e+00,
140 2.61718748558906533e-01, 3.82089554342693294e+00, -1.34048483059486401e+00,
141 2.69531249159214337e-01, 3.71014493910979404e+00, -1.31107094300173976e+00,
142 2.77343749428383191e-01, 3.60563381024826013e+00, -1.28249756949928795e+00,
143 2.85156249289339359e-01, 3.50684932380819214e+00, -1.25471800582335113e+00,
144 2.92968749999700462e-01, 3.41333333333682321e+00, -1.22768933094427446e+00,
145 3.00781248554318814e-01, 3.32467534065511261e+00, -1.20137202743229921e+00,
146 3.08593748521894806e-01, 3.24050634463533127e+00, -1.17572959680235023e+00,
147 3.16406249999639899e-01, 3.16049382716409077e+00, -1.15072828980826181e+00,
148 3.24218749999785061e-01, 3.08433734939963511e+00, -1.12633683668362750e+00,
149 3.32031248841858584e-01, 3.01176471638753718e+00, -1.10252619147729547e+00,
150 3.39843749265406558e-01, 2.94252874199264314e+00, -1.07926932798654107e+00,
151 3.47656249999834799e-01, 2.87640449438338930e+00, -1.05654107474789782e+00,
152 3.55468749999899247e-01, 2.81318681318761055e+00, -1.03431793796299587e+00,
153 3.63281249999864997e-01, 2.75268817204403371e+00, -1.01257795132667816e+00,
154 3.71093749064121570e-01, 2.69473684890124421e+00, -9.91300555400967731e-01,
155 3.78906249999751032e-01, 2.63917525773369288e+00, -9.70466465976836723e-01,
156 3.86718748879039009e-01, 2.58585859335407608e+00, -9.50057597243619156e-01,
157 3.94531249999987899e-01, 2.53465346534661240e+00, -9.30056927638333697e-01,
158 4.02343749999485523e-01, 2.48543689320706163e+00, -9.10448456251205407e-01,
159 4.10156249578856991e-01, 2.43809524059864202e+00, -8.91217095348825872e-01,
160 4.17968749447214571e-01, 2.39252336765021800e+00, -8.72348611340208357e-01,
161 4.25781248601723117e-01, 2.34862386092395203e+00, -8.53829565534445223e-01,
162 4.33593749393073047e-01, 2.30630630953458038e+00, -8.35647244566987801e-01,
163 4.41406248572254134e-01, 2.26548673299152270e+00, -8.17789629001761220e-01,
164 4.49218749348472501e-01, 2.22608695975035964e+00, -8.00245317566669279e-01,
165 4.57031249277175089e-01, 2.18803419149470768e+00, -7.83003511263371976e-01,
166 4.64843748529596368e-01, 2.15126051100659366e+00, -7.66053954531254355e-01,
167 4.72656248830947701e-01, 2.11570248457175136e+00, -7.49386901356188240e-01,
168 4.80468748609962581e-01, 2.08130081902951236e+00, -7.32993092000230995e-01,
169 4.88281249241778237e-01, 2.04800000318021258e+00, -7.16863708730099525e-01,
170 4.96093748931098810e-01, 2.01574803583926521e+00, -7.00990360175606675e-01,
171 5.07812497779701388e-01, 1.96923077784079825e+00, -6.77642998396260410e-01,
172 5.23437498033319737e-01, 1.91044776837204044e+00, -6.47337648285891021e-01,
173 5.39062498006593560e-01, 1.85507247062801328e+00, -6.17923763020271188e-01,
174 5.54687498964024250e-01, 1.80281690477552603e+00, -5.89350388745976339e-01,
175 5.70312499806522322e-01, 1.75342465812909332e+00, -5.61570823110474571e-01,
176 5.85937497921867001e-01, 1.70666667271966777e+00, -5.34542153929987052e-01,
177 6.01562498226483444e-01, 1.66233766723853860e+00, -5.08224845014116688e-01,
178 6.17187498682654212e-01, 1.62025316801528496e+00, -4.82582413587029357e-01,
179 6.32812500000264566e-01, 1.58024691357958624e+00, -4.57581109246760320e-01,
180 6.48437499353274216e-01, 1.54216867623689291e+00, -4.33189657120379490e-01,
181 6.64062498728508976e-01, 1.50588235582451335e+00, -4.09379009344016609e-01,
182 6.79687498865382267e-01, 1.47126437027210688e+00, -3.86122146934356092e-01,
183 6.95312498728747119e-01, 1.43820224982050338e+00, -3.63393896015796081e-01,
184 7.10937499999943157e-01, 1.40659340659351906e+00, -3.41170757402847080e-01,
185 7.26562499999845568e-01, 1.37634408602179792e+00, -3.19430770766573779e-01,
186 7.42187500000120126e-01, 1.34736842105241350e+00, -2.98153372318914478e-01,
187 7.57812499999581890e-01, 1.31958762886670744e+00, -2.77319285416786077e-01,
188 7.73437498602746576e-01, 1.29292929526503420e+00, -2.56910415591577124e-01,
189 7.89062500000142664e-01, 1.26732673267303819e+00, -2.36909747078176913e-01,
190 8.04687500000259015e-01, 1.24271844660154174e+00, -2.17301275689659512e-01,
191 8.20312499999677036e-01, 1.21904761904809900e+00, -1.98069913762487504e-01,
192 8.35937499999997113e-01, 1.19626168224299478e+00, -1.79201429457714445e-01,
193 8.51562499999758749e-01, 1.17431192660583728e+00, -1.60682381690756770e-01,
194 8.67187500000204725e-01, 1.15315315315288092e+00, -1.42500062607046951e-01,
195 8.82812500000407896e-01, 1.13274336283133503e+00, -1.24642445206814556e-01,
196 8.98437499999816813e-01, 1.11304347826109651e+00, -1.07098135556570995e-01,
197 9.14062499999708455e-01, 1.09401709401744296e+00, -8.98563291221800009e-02,
198 9.29687500000063949e-01, 1.07563025210076635e+00, -7.29067708080189947e-02,
199 9.45312499999844014e-01, 1.05785123966959604e+00, -5.62397183230410880e-02,
200 9.60937500000120459e-01, 1.04065040650393459e+00, -3.98459085470743157e-02,
201 9.76562499999976685e-01, 1.02400000000002445e+00, -2.37165266173399170e-02,
202 9.92187500000169420e-01, 1.00787401574785940e+00, -7.84317746085513856e-03,
203 1.01562500000004907e+00, 9.84615384615337041e-01, 1.55041865360135717e-02,
204 1.04687500000009237e+00, 9.55223880596930641e-01, 4.58095360313824362e-02,
205 1.07812500000002154e+00, 9.27536231884039442e-01, 7.52234212376075018e-02,
206 1.10937499999982481e+00, 9.01408450704367703e-01, 1.03796793681485644e-01,
207 1.14062500000007416e+00, 8.76712328767066285e-01, 1.31576357788784293e-01,
208 1.17187500000009659e+00, 8.53333333333263000e-01, 1.58605030176721007e-01,
209 1.20312499999950173e+00, 8.31168831169175393e-01, 1.84922338493597849e-01,
210 1.23437500000022027e+00, 8.10126582278336449e-01, 2.10564769107528083e-01,
211 1.26562500000064615e+00, 7.90123456789720069e-01, 2.35566071313277448e-01,
212 1.29687500000144706e+00, 7.71084337348537208e-01, 2.59957524438041876e-01,
213 1.32812499999945932e+00, 7.52941176470894757e-01, 2.83768173130237500e-01,
214 1.35937500055846350e+00, 7.35632183605830825e-01, 3.07025035705735583e-01,
215 1.39062499999999467e+00, 7.19101123595508374e-01, 3.29753286372464149e-01,
216 1.42187500000017564e+00, 7.03296703296616421e-01, 3.51976423157301710e-01,
217 1.45312500161088876e+00, 6.88172042247866766e-01, 3.73716410902152685e-01,
218 1.48437500134602307e+00, 6.73684209915422660e-01, 3.94993809147663466e-01,
219 1.51562499999932343e+00, 6.59793814433284220e-01, 4.15827895143264570e-01,
220 1.54687500000028200e+00, 6.46464646464528614e-01, 4.36236766775100371e-01,
221 1.57812500000061906e+00, 6.33663366336385092e-01, 4.56237433481979870e-01,
222 1.60937500243255216e+00, 6.21359222361793417e-01, 4.75845906381452632e-01,
223 1.64062500000026312e+00, 6.09523809523711768e-01, 4.95077266798011895e-01,
224 1.67187500000027911e+00, 5.98130841121395473e-01, 5.13945751102401260e-01,
225 1.70312500224662178e+00, 5.87155962528224662e-01, 5.32464800188589216e-01,
226 1.73437500283893620e+00, 5.76576575632799071e-01, 5.50647119589526390e-01,
227 1.76562500399259092e+00, 5.66371680135198341e-01, 5.68504737613959144e-01,
228 1.79687500443862880e+00, 5.56521737755718449e-01, 5.86049047473771623e-01,
229 1.82812500114411280e+00, 5.47008546666207462e-01, 6.03290852063923744e-01,
230 1.85937500250667465e+00, 5.37815125325376786e-01, 6.20240411099985067e-01,
231 1.89062500504214515e+00, 5.28925618424108568e-01, 6.36907464903988974e-01,
232 1.92187500371610143e+00, 5.20325202245941476e-01, 6.53301273946326866e-01,
233 1.95312500494870611e+00, 5.11999998702726389e-01, 6.69430656476366792e-01,
234 1.98437500351688123e+00, 5.03937006980894941e-01, 6.85304004871206018e-01,
235 2.03125000000003997e+00, 4.92307692307682621e-01, 7.08651367095930240e-01,
236 2.09375000579615866e+00, 4.77611938976327366e-01, 7.38956719359554093e-01,
237 2.15625000000061062e+00, 4.63768115941897652e-01, 7.68370601797816022e-01,
238 2.21875000323311955e+00, 4.50704224695355204e-01, 7.96943975698769513e-01,
239 2.28125000853738547e+00, 4.38356162743050726e-01, 8.24723542091080120e-01,
240 2.34374999999916556e+00, 4.26666666666818573e-01, 8.51752210736227866e-01,
241 2.40625000438447856e+00, 4.15584414827170512e-01, 8.78069520876078258e-01,
242 2.46875000884389584e+00, 4.05063289688167072e-01, 9.03711953249632494e-01,
243 2.53124999999940403e+00, 3.95061728395154743e-01, 9.28713251872476775e-01,
244 2.59375000434366632e+00, 3.85542168029044230e-01, 9.53104706671537905e-01,
245 2.65625000734081196e+00, 3.76470587194880080e-01, 9.76915356454189698e-01,
246 2.71875000787161980e+00, 3.67816090889081959e-01, 1.00017221875016560e+00,
247 2.78125001557333462e+00, 3.59550559784484969e-01, 1.02290047253181449e+00,
248 2.84375001147093220e+00, 3.51648350229895601e-01, 1.04512360775085789e+00,
249 2.90625000771072894e+00, 3.44086020592463127e-01, 1.06686359300668343e+00,
250 2.96875001371853831e+00, 3.36842103706616824e-01, 1.08814099342179560e+00,
251 3.03125000512624965e+00, 3.29896906658595002e-01, 1.10897507739479018e+00,
252 3.09375001373132807e+00, 3.23232321797685962e-01, 1.12938395177327244e+00,
253 3.15625001204422961e+00, 3.16831681959289180e-01, 1.14938461785752644e+00,
254 3.21875000888250318e+00, 3.10679610793130057e-01, 1.16899308818952186e+00,
255 3.28125000000102052e+00, 3.04761904761809976e-01, 1.18822444735810784e+00,
256 3.34375001587649123e+00, 2.99065419140752298e-01, 1.20709293641028914e+00,
257 3.40625000791328070e+00, 2.93577980969346064e-01, 1.22561198175258212e+00,
258 3.46875000615970519e+00, 2.88288287776354346e-01, 1.24379430028837845e+00,
259 3.53125000516822674e+00, 2.83185840293502689e-01, 1.26165191737618265e+00,
260 3.59375001425228779e+00, 2.78260868461675415e-01, 1.27919622952937750e+00,
261 3.65625001719730669e+00, 2.73504272217836075e-01, 1.29643803670156643e+00,
262 3.71875000856489324e+00, 2.68907562405871714e-01, 1.31338759261496740e+00,
263 3.78125001788371806e+00, 2.64462808666557803e-01, 1.33005464752659286e+00,
264 3.84375001532508964e+00, 2.60162600588744020e-01, 1.34644845655970613e+00,
265 3.90625000429340918e+00, 2.55999999718627136e-01, 1.36257783560168733e+00,
266 3.96875001912740766e+00, 2.51968502722644594e-01, 1.37845118847836900e+00,
267 4.06250002536431332e+00, 2.46153844616978895e-01, 1.40179855389937913e+00,
268 4.18750001743208244e+00, 2.38805969155131859e-01, 1.43210390131407017e+00,
269 4.31250002253733200e+00, 2.31884056759177282e-01, 1.46151778758352613e+00,
270 4.43750000671406397e+00, 2.25352112335092170e-01, 1.49009115631456268e+00,
271 4.56250002627485340e+00, 2.19178080929562313e-01, 1.51787072466748185e+00,
272 4.68750001185115028e+00, 2.13333332793974317e-01, 1.54489939382477459e+00,
273 4.81250001682742301e+00, 2.07792207065640028e-01, 1.57121670311050998e+00,
274 4.93750000000042366e+00, 2.02531645569602875e-01, 1.59685913022732606e+00,
275 5.06249999999927613e+00, 1.97530864197559108e-01, 1.62186043243251454e+00,
276 5.18750002327641901e+00, 1.92771083472381588e-01, 1.64625189004383721e+00,
277 5.31250002381002329e+00, 1.88235293273997795e-01, 1.67006253873242194e+00,
278 5.43750000000577405e+00, 1.83908045976816203e-01, 1.69331939641586438e+00,
279 5.56250002193114934e+00, 1.79775280190080267e-01, 1.71604765143503712e+00,
280 5.68749999999938005e+00, 1.75824175824194989e-01, 1.73827078427695980e+00,
281 5.81250002749782002e+00, 1.72043009938785768e-01, 1.76001077564428243e+00,
282 5.93749999999874767e+00, 1.68421052631614471e-01, 1.78128816936054868e+00,
283 6.06250001966917473e+00, 1.64948453073088669e-01, 1.80212225950800153e+00,
284 6.18750003004243609e+00, 1.61616160831459688e-01, 1.82253113275015188e+00,
285 6.31250002448351388e+00, 1.58415840969730465e-01, 1.84253179848005466e+00,
286 6.43750001359968849e+00, 1.55339805497076044e-01, 1.86214026810242750e+00,
287 6.56250003345742350e+00, 1.52380951604072529e-01, 1.88137163301601618e+00,
288 6.68750002403557531e+00, 1.49532709742937614e-01, 1.90024011581622965e+00,
289 6.81250003423489581e+00, 1.46788990088028509e-01, 1.91875916501466826e+00,
290 6.93750003062940923e+00, 1.44144143507740546e-01, 1.93694148348760287e+00,
291 7.06250002747386052e+00, 1.41592919803171097e-01, 1.95479910036266347e+00,
292 7.18750003617887856e+00, 1.39130434082284093e-01, 1.97234341115705192e+00,
293 7.31250000000050537e+00, 1.36752136752127301e-01, 1.98958521255804399e+00,
294 7.43750002212249761e+00, 1.34453781112678528e-01, 2.00653477384620160e+00,
295 7.56250003604752941e+00, 1.32231404328381430e-01, 2.02320182812357530e+00,
296 7.68750005007207449e+00, 1.30081299965731312e-01, 2.03959563964607682e+00,
297 7.81249996125652668e+00, 1.28000000634773070e-01, 2.05572501010335529e+00,
298 7.93750005224239974e+00, 1.25984251139310915e-01, 2.07159837080052966e+00,
299 8.12500004244456164e+00, 1.23076922433975874e-01, 2.09494573343974722e+00,
300 8.37500006149772425e+00, 1.19402984197849338e-01, 2.12525108505414195e+00,
301 8.62500006593247370e+00, 1.15942028099206410e-01, 2.15466497056176820e+00,
302 8.87500007743793873e+00, 1.12676055354884341e-01, 2.18323834408688100e+00,
303 9.12500001754142609e+00, 1.09589040885222130e-01, 2.21101790139090326e+00,
304 9.37500007707016181e+00, 1.06666665789779500e-01, 2.23804658007729174e+00,
305 9.62500004426353151e+00, 1.03896103418305616e-01, 2.26436388477265638e+00,
306 9.87500006518495788e+00, 1.01265822116353585e-01, 2.29000631738819393e+00,
307 1.01250000000026539e+01, 9.87654320987395445e-02, 2.31500761299286495e+00,
308 1.03750000409819823e+01, 9.63855417879450060e-02, 2.33939907006683256e+00,
309 1.06250000362555337e+01, 9.41176467376672460e-02, 2.36320971822276604e+00,
310 1.08750000879032314e+01, 9.19540222452362582e-02, 2.38646658505780351e+00,
311 1.11250000697274576e+01, 8.98876398860551373e-02, 2.40919483431994053e+00,
312 1.13750000462194141e+01, 8.79120875548795450e-02, 2.43141796890025930e+00,
313 1.16250000714972366e+01, 8.60215048472860316e-02, 2.45315795762371991e+00,
314 1.18750000788855150e+01, 8.42105257563797310e-02, 2.47443535656369562e+00,
315 1.21250000895724916e+01, 8.24742261948517991e-02, 2.49526944421096886e+00,
316 1.23750000985058719e+01, 8.08080801648427965e-02, 2.51567831641482442e+00,
317 1.26250000894226950e+01, 7.92079202310506381e-02, 2.53567898224440924e+00,
318 1.28750000768594433e+01, 7.76699024489580225e-02, 2.55528745251946532e+00,
319 1.31250000578007420e+01, 7.61904758549435401e-02, 2.57451881288155349e+00,
320 1.33750000809310077e+01, 7.47663546877819496e-02, 2.59338729883298669e+00,
321 1.36250000915049636e+01, 7.33944949199294983e-02, 2.61190634726526838e+00,
322 1.38750000830616607e+01, 7.20720716406179490e-02, 2.63008866561892418e+00,
323 1.41249999999960103e+01, 7.07964601770111474e-02, 2.64794627703222218e+00,
324 1.43750000290097564e+01, 6.95652172509168693e-02, 2.66549058870148414e+00,
325 1.46250000868097665e+01, 6.83760679702078294e-02, 2.68273239905363070e+00,
326 1.48750000966053975e+01, 6.72268903196987927e-02, 2.69968195792617394e+00,
327 1.51250001097012756e+01, 6.61157019998031836e-02, 2.71634901116988203e+00,
328 1.53750000510427132e+01, 6.50406501905787804e-02, 2.73274281701243282e+00,
329 1.56250001080665442e+01, 6.39999995573594382e-02, 2.74887220253872400e+00,
330 1.58750000434989929e+01, 6.29921258116476201e-02, 2.76474554751884938e+00,
331 1.62500000641781739e+01, 6.15384612954199342e-02, 2.78809291272517257e+00,
332 1.67500001015987401e+01, 5.97014921751882754e-02, 2.81839826433667184e+00,
333 1.72500001048300184e+01, 5.79710141404578272e-02, 2.84781214955447126e+00,
334 1.77500001262529885e+01, 5.63380277682904579e-02, 2.87638552303426920e+00,
335 1.82500001543340602e+01, 5.47945200845665337e-02, 2.90416508848516131e+00,
336 1.87500001096404212e+01, 5.33333330214672482e-02, 2.93119375826390893e+00,
337 1.92500001680268191e+01, 5.19480514946147609e-02, 2.95751106946245912e+00,
338 1.97500000329124035e+01, 5.06329113080278073e-02, 2.98315349301358168e+00,
339 2.02500001270002485e+01, 4.93827157396732261e-02, 3.00815479982416534e+00,
340 2.07500001519906796e+01, 4.81927707313324349e-02, 3.03254625400155930e+00,
341 2.12500001425219267e+01, 4.70588232137922752e-02, 3.05635690207734001e+00,
342 2.17500000758314478e+01, 4.59770113339538697e-02, 3.07961376102119644e+00,
343 2.22500001767207358e+01, 4.49438198677525880e-02, 3.10234201655475417e+00,
344 2.27500001365873317e+01, 4.39560436921389575e-02, 3.12456515140079816e+00,
345 2.32500001697599998e+01, 4.30107523741288036e-02, 3.14630513933487066e+00,
346 2.37500001766865303e+01, 4.21052628446554611e-02, 3.16758253792008304e+00,
347 };
348
349#define HIWORD 1
350#define LOWORD 0
351
352 // Fast Log - Stripped version of libm's Log(x)
353 /* This is the libm log stripped down with slightly less precision and no handling for inf, 0 etc.
354 For an idea of how this works see: http://www.informatik.uni-trier.de/Reports/TR-08-2004/rnc6_07_dinechin.pdf
355 This is about 15% faster than the standard log() */
356 inline double fastLog(const double x)
357 {
358 union
359 {
360 int i[2];
361 double x;
362 } u;
363
364 u.x = x;
365
366 /* get exponent part, store in dn */
367 const int hx = u.i[HIWORD]; // Get the top half of the double
368 const int ix = hx & 0x7fffffff; // condition ix
369 double dn = (double)((ix >> 20) - 0x3ff); // Shift to get E
370
371 /* Now we take advantage of the float point identity:
372
373 log(x) = E × log(2) + log(y)
374
375 Where E is the exponent part of the float and y is from the mentisa.
376
377 y is then set to be on the interval from 11/16 to 23/16 using
378 a bit shift to divide it by 2.
379
380 log(y) needs to be scaled and approximated on a table. Then we use the
381 Horner scheme polynomial to approximated the final log */
382
383 const double dn1 = dn * LN2HI; // Low Order E * log(2)
384 int i = (ix & 0x000fffff) | 0x3ff00000; // scale x to [1,2]
385 u.i[HIWORD] = i; // copy back E
386 i = (i - 0x3fb80000) >> 15; // mantisa table addr
387 const double *tb = (double *)_TBL_log + (i + i + i); // Get table const's
388 const double s = (u.x - tb[0]) * tb[1]; // initial estimate
389 dn = dn * LN2LO + tb[2]; // High order E * log(2)
390
391 // 8th order Polynomial in Horner form
392 return (dn1 + (dn + ((B1 * s) * (B2 + s * (B3 + s))) *
393 (((B4 + s * B5) + (s * s) * (B6 + s)) *
394 (B7 + s * (B8 + s)))));
395
396 };
397
398 /* data for a Chebyshev series over a given interval */
399 struct cheb_series_struct {
400 double * c; /* coefficients */
401 int order; /* order of expansion */
402 double a; /* lower interval point */
403 double b; /* upper interval point */
404 int order_sp; /* effective single precision order */
405 };
406 typedef struct cheb_series_struct cheb_series;
407
408 /* Chebyshev fits from SLATEC code for psi(x) */
409 static double psics_data[23] = {
410 -.038057080835217922,
411 .491415393029387130,
412 -.056815747821244730,
413 .008357821225914313,
414 -.001333232857994342,
415 .000220313287069308,
416 -.000037040238178456,
417 .000006283793654854,
418 -.000001071263908506,
419 .000000183128394654,
420 -.000000031353509361,
421 .000000005372808776,
422 -.000000000921168141,
423 .000000000157981265,
424 -.000000000027098646,
425 .000000000004648722,
426 -.000000000000797527,
427 .000000000000136827,
428 -.000000000000023475,
429 .000000000000004027,
430 -.000000000000000691,
431 .000000000000000118,
432 -.000000000000000020
433 };
434 const static cheb_series psi_cs = { psics_data, 22, -1, 1, 17 };
435
436 static double apsics_data[16] = {
437 -.0204749044678185,
438 -.0101801271534859,
439 .0000559718725387,
440 -.0000012917176570,
441 .0000000572858606,
442 -.0000000038213539,
443 .0000000003397434,
444 -.0000000000374838,
445 .0000000000048990,
446 -.0000000000007344,
447 .0000000000001233,
448 -.0000000000000228,
449 .0000000000000045,
450 -.0000000000000009,
451 .0000000000000002,
452 -.0000000000000000
453 };
454 const static cheb_series apsi_cs = { apsics_data, 15, -1, 1, 9 };
455
456 inline double cheb_eval_psi(const double x)
457 {
458 double d = 0.0, dd = 0.0;
459
460 const double y2 = 2.0 * x;
461
462 for (uint j = 22; j >= 1; j--)
463 {
464 const double temp = d;
465 d = y2*d - dd + psics_data[j];
466 dd = temp;
467 }
468
469 return x*d - dd + 0.5 * -.038057080835217922;
470 }
471
472 inline double cheb_eval_apsi(const double x)
473 {
474 double d = 0.0, dd = 0.0;
475
476 const double y2 = 2.0 * x;
477
478 for (uint j = 15; j >= 1; j--)
479 {
480 const double temp = d;
481 d = y2*d - dd + apsics_data[j];
482 dd = temp;
483 }
484
485 return x*d - dd + 0.5 * -.0204749044678185;
486 }
487
488
489
490 inline double fast_psi(double x)
491 {
492 if (x >= 2.0)
493 {
494 const double t = 8.0 / (x * x) - 1.0;
495 const double cheb = cheb_eval_apsi(t);
496 return fastLog(x) - 0.5 / x + cheb;
497 }
498 else if (x < 1.0)
499 {
500 const double t1 = 1.0 / x;
501 const double cheb = cheb_eval_psi(2.0 * x - 1.0);
502 return cheb - t1;
503 }
504 else
505 {
506 const double v = x - 1.0;
507 return cheb_eval_psi(2.0 * v - 1.0);
508 }
509 }
510
511 // Natural Log of 2 DEFINED in cmath as M_LN2, but is not 128 bit
512#define D_LOG_2 0.6931471805599453094172321214581766
513
514 // Convert a number into Wows, for instance a KL distance
515 /* Wow's in this computation is the same as the conversion from Nats to Bits in KL */
516 template <class T> inline T wow(const T& k)
517 {
518 // surprise should always be positive but rounding may mess that up: This is OK since surprise is negative with
519 // equality if and only if two distribution functions are equal. Thus, we gain no insite with negative surprise.
520
521 if (k < 0.0) return 0.0;
522 // return the surprise, in units of wows (KL Bits):
523 return k * (1.0F / D_LOG_2);
524 }
525
526 // Compute the KL distance for a single gamma PDF
527 template <class T> inline T KLgamma(const T a, const T b,
528 const T A, const T B,
529 const bool doWow = false)
530 {
531 /* We will compute the joint KL for P(x), P(X), P(y) and P(Y) where one is a gamma distribution and the other
532 gaussian as:
533
534 surprise is KL(new || old):
535
536 P(X)
537 KL = Integrate[P(x) log[----] ]
538 P(x)
539
540 a - New Alpha
541 b - New Beta
542 A - Current Alpha
543 B - Current Beta
544
545 a B b Gamma[A]
546 KL = -a + --- + A Log[-] + Log[--------] + a PolyGamma[0, a] - A * PolyGamma[0, a]
547 b B Gamma[a]
548
549 Note: Since the pure Gamma function can get large very quickly, we avoid large numbers by computing the log gamma
550 directly as lgamma. */
551
552 T const k = - a + a * B / b + A * fastLog(b / B) + lgamma(A) - lgamma(a) + (a - A) * fast_psi(a);
553
554 if (doWow) return wow(k); else return k;
555 }
556}
557
558// ##############################################################################################################
560{
561 std::string const chans = channels::get();
562
563 // Compute feature maps and saliency maps, possibly gist. Results are stored in the Saliency class:
564 itsSaliency->process(input, (chans.find('G') != chans.npos));
565
566 // Aggregate our data values from all maps. These maps are small, no need to parallelize:
567 std::vector<float> data; std::string done;
568
569 for (char c : chans)
570 {
571 if (done.find(c) != done.npos) continue; // skip duplicates
572 done += c; // mark this channel as done
573 intg32 * pix; size_t siz;
574
575 switch (c)
576 {
577 case 'S': pix = itsSaliency->salmap.pixels; siz = env_img_size(&itsSaliency->salmap); break;
578 case 'I': pix = itsSaliency->intens.pixels; siz = env_img_size(&itsSaliency->intens); break;
579 case 'C': pix = itsSaliency->color.pixels; siz = env_img_size(&itsSaliency->color); break;
580 case 'O': pix = itsSaliency->ori.pixels; siz = env_img_size(&itsSaliency->ori); break;
581 case 'F': pix = itsSaliency->flicker.pixels; siz = env_img_size(&itsSaliency->flicker); break;
582 case 'M': pix = itsSaliency->motion.pixels; siz = env_img_size(&itsSaliency->motion); break;
583 case 'G':
584 {
585 unsigned char const * g = itsSaliency->gist;
586 for (size_t i = 0; i < itsSaliency->gist_size; ++i) data.push_back(g[i]);
587 continue;
588 }
589 default: continue; // should never happen given our regex spec for the parameter
590 }
591
592 // Concatenate the data if it was not gist:
593 for (size_t i = 0; i < siz; ++i) data.push_back(pix[i]);
594 }
595
596 size_t const datasiz = data.size(); // final data size
597 float const ufac = updatefac::get(); // get() is somewhat expensive (requires mutex lock), so cache it here.
598 float const initfac = 1.0F / (1.0F - ufac);
599
600 // Initialize the prior if this is our first frame, or frame size or map size just changed somehow. We initialize
601 // alpha and beta as in the SurpriseModelSP of the iLab Neuromorphic C++ Vision Toolkit, from which this
602 // implementation is derived. Also see Itti & Baldi, Vis Res, 2009, for details:
603
604 if (itsAlpha.size() != datasiz)
605 {
606 itsAlpha.clear(); itsBeta.clear();
607 for (float d : data)
608 {
609 itsAlpha.push_back(d * initfac);
610 itsBeta.push_back(initfac);
611 }
612 }
613
614 // Compute posterior and KL, independently for every entry in our vectors. Here we assume Poisson data and a Gamma
615 // conjugate prior, as in Itti & Baldi, Vision Research, 2009. Note: we compute surprise using double precision, then
616 // store alpha and beta using float:
617 double surprise = 0.0;
618
619 for (size_t i = 0; i < datasiz; ++i)
620 {
621 // First, decay alpha and beta. Make sure alpha does not decay all the way to 0:
622 double alpha = itsAlpha[i] * ufac, beta = itsBeta[i] * ufac;
623 if (alpha < 1.0e-5) alpha = 1.0e-5;
624
625 // Compute the posterior:
626 double const newAlpha = alpha + data[i];
627 double const newBeta = beta + 1.0F;
628
629 // Surprise is KL(new || old). Keep track of the max value found over the data array:
630 double const s = std::abs(KLgamma<double>(newAlpha, newBeta, alpha, beta, true));
631 if (s > surprise) surprise = s;
632
633 // The posterior becomes our new prior for the next video frame:
634 itsAlpha[i] = newAlpha; itsBeta[i] = newBeta;
635 }
636
637 // Return max number of wows found over the whole data array:
638 return surprise;
639}
640
#define B4
Definition Surprise.C:84
#define LN2HI
Definition Surprise.C:67
#define D_LOG_2
Definition Surprise.C:512
#define LN2LO
Definition Surprise.C:68
#define B2
Definition Surprise.C:82
#define B7
Definition Surprise.C:87
#define B1
Definition Surprise.C:81
#define B6
Definition Surprise.C:86
#define B5
Definition Surprise.C:85
#define B8
Definition Surprise.C:88
#define B3
Definition Surprise.C:83
#define HIWORD
Definition Surprise.C:349
unsigned int uint
Canonical unsigned int.
Definition Types.H:134
std::shared_ptr< Saliency > itsSaliency
Definition Surprise.H:89
Surprise(std::string const &instance)
Constructor.
Definition Surprise.C:21
std::vector< float > itsAlpha
Definition Surprise.H:90
std::vector< float > itsBeta
Definition Surprise.H:90
double process(jevois::RawImage const &input)
Compute surprise from a YUYV video frame and return the surprise value in wows.
Definition Surprise.C:559
~Surprise()
Virtual destructor for safe inheritance.
Definition Surprise.C:28
ENV_INTG32_TYPE intg32
32-bit signed integer
Definition env_types.h:52