@@ -166,11 +166,13 @@ KDTree(const cv::Mat &img, const int _leafNumber, const int _zeroThresh)
166166 int dimIdx = getMaxSpreadN (_left, _right);
167167 KDTreeComparator comp ( this , dimIdx );
168168
169+ std::vector<int > _idx (idx.begin (), idx.end ());
169170 std::nth_element (/* */
170- idx .begin () + _left,
171- idx .begin () + nth,
172- idx .begin () + _right, comp
171+ _idx .begin () + _left,
172+ _idx .begin () + nth,
173+ _idx .begin () + _right, comp
173174 /* */ );
175+ idx = _idx;
174176
175177 left.push (_left); right.push (nth + 1 );
176178 left.push (nth + 1 ); right.push (_right);
@@ -280,11 +282,12 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Point2i> &tr
280282 }
281283 }
282284
283- std::partial_sort ( amount.begin (), amount.begin () + nTransform,
285+ int num = std::min ((int )amount.size (), (int )nTransform);
286+ std::partial_sort ( amount.begin (), amount.begin () + num,
284287 amount.end (), std::greater< std::pair<double , int > >() );
285288
286- transforms.resize (nTransform );
287- for (int i = 0 ; i < nTransform ; ++i)
289+ transforms.resize (num );
290+ for (int i = 0 ; i < num ; ++i)
288291 {
289292 int idx = amount[i].second ;
290293 transforms[i] = cv::Point2i ( shiftM[idx].x , shiftM[idx].y );
0 commit comments