4040#ifndef __ANNF_HPP__
4141#define __ANNF_HPP__
4242
43+ #include < vector>
44+ #include < stack>
45+ #include < limits>
46+ #include < algorithm>
47+ #include < iterator>
48+ #include < iostream>
49+ #include < fstream>
50+ #include < time.h>
51+ #include < functional>
52+
4353#include " norm2.hpp"
4454#include " whs.hpp"
4555
@@ -95,11 +105,12 @@ template <typename Tp, int cn> class KDTree
95105};
96106
97107template <typename Tp, int cn> int KDTree <Tp, cn>::
98- getMaxSpreadN (const int _left , const int _right ) const
108+ getMaxSpreadN (const int left , const int right ) const
99109{
100- cv::Vec<Tp, cn> maxValue = data[ idx[_left] ],
101- minValue = data[ idx[_left] ];
102- for (int i = _left + 1 ; i < _right; i += cn)
110+ cv::Vec<Tp, cn> maxValue = data[ idx[left] ],
111+ minValue = data[ idx[left] ];
112+
113+ for (int i = left + 1 ; i < right; ++i)
103114 for (int j = 0 ; j < cn; ++j)
104115 {
105116 minValue[j] = std::min ( minValue[j], data[idx[i]][j] );
@@ -143,6 +154,7 @@ KDTree(const cv::Mat &img, const int _leafNumber, const int _zeroThresh)
143154 }
144155
145156 int nth = _left + (_right - _left)/2 ;
157+
146158 int dimIdx = getMaxSpreadN (_left, _right);
147159 KDTreeComparator comp ( this , dimIdx );
148160
@@ -168,8 +180,8 @@ updateDist(const int leaf, const int &idx0, int &bestIdx, double &dist)
168180 if (abs (ny - y) < zeroThresh &&
169181 abs (nx - x) < zeroThresh)
170182 continue ;
171- if (nx > width - 1 || nx < 1 ||
172- ny > height - 1 || ny > 1 )
183+ if (nx >= width - 1 || nx < 1 ||
184+ ny >= height - 1 || ny < 1 )
173185 continue ;
174186
175187 double ndist = norm2 (data[idx0], data[idx[k]]);
@@ -184,9 +196,12 @@ updateDist(const int leaf, const int &idx0, int &bestIdx, double &dist)
184196
185197/* ************************* ANNF search **************************/
186198
187- static void dominantTransforms (const cv::Mat &img, std::vector <cv::Matx33f > &transforms,
199+ static void dominantTransforms (const cv::Mat &img, std::vector <cv::Point2i > &transforms,
188200 const int nTransform, const int psize)
189201{
202+ const int zeroThresh = 2 *psize;
203+ const int leafNum = 64 ;
204+
190205 /* * Walsh-Hadamard Transformation **/
191206
192207 std::vector <cv::Mat> channels;
@@ -204,7 +219,7 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &tr
204219 cv::Mat whs; // Walsh-Hadamard series
205220 cv::merge (channels, whs);
206221
207- KDTree <float , 24 > kdTree (whs, 16 , 32 );
222+ KDTree <float , 24 > kdTree (whs, leafNum, zeroThresh );
208223 std::vector <int > annf ( whs.total (), 0 );
209224
210225 /* * Propagation-assisted kd-tree search **/
@@ -217,13 +232,13 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &tr
217232
218233 int dy[] = {0 , 1 , 0 }, dx[] = {0 , 0 , 1 };
219234 for (int k = 0 ; k < int ( sizeof (dy)/sizeof (int ) ); ++k)
220- if (i - dy[k] >= 0 && j - dx[k] >= 0 )
235+ if ( i - dy[k] >= 0 && j - dx[k] >= 0 )
221236 {
222237 int neighbor = (i - dy[k])*whs.cols + (j - dx[k]);
223- int leafIdx = k == 0 ? neighbor :
224- annf[neighbor] + dy[k]*whs.cols + dx[k];
238+ int leafIdx = (dx[k] == 0 && dy[k] == 0 )
239+ ? neighbor : annf[neighbor] + dy[k]*whs.cols + dx[k];
225240 kdTree.updateDist (leafIdx, current,
226- annf[i*whs.cols + j], dist);
241+ annf[i*whs.cols + j], dist);
227242 }
228243 }
229244
@@ -232,8 +247,8 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &tr
232247 cv::Mat_<double > annfHist (2 *whs.rows - 1 , 2 *whs.cols - 1 , 0.0 ),
233248 _annfHist (2 *whs.rows - 1 , 2 *whs.cols - 1 , 0.0 );
234249 for (size_t i = 0 ; i < annf.size (); ++i)
235- ++annfHist ( ( annf[i] - int (i) )/whs.cols + whs.rows - 1 ,
236- ( annf[i] - int (i) )%whs.cols + whs.cols - 1 );
250+ ++annfHist ( annf[i]/whs. cols - int (i)/whs.cols + whs.rows - 1 ,
251+ annf[i]%whs. cols - int (i)%whs.cols + whs.cols - 1 );
237252
238253 cv::GaussianBlur ( annfHist, annfHist,
239254 cv::Size (0 , 0 ), std::sqrt (2.0 ), 0.0 , cv::BORDER_CONSTANT);
@@ -264,9 +279,7 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &tr
264279 for (int i = 0 ; i < nTransform; ++i)
265280 {
266281 int idx = amount[i].second ;
267- transforms[i] = cv::Matx33f (1 , 0 , float (shiftM[idx].x ),
268- 0 , 1 , float (shiftM[idx].y ),
269- 0 , 0 , 1 );
282+ transforms[i] = cv::Point2i ( shiftM[idx].x , shiftM[idx].y );
270283 }
271284}
272285
0 commit comments