c++ - OpenCV sort points stored in Point2f vector -
i trying sort result returned minarearect
using the following algorithm:
here's code now:
void sortpoints(point2f* unsorted) { point2f sorted[4]; (int = 0; < 4; i++) sorted[i] = point(0, 0); float middlex = (unsorted[0].x + unsorted[1].x + unsorted[2].x + unsorted[3].x) / 4; float middley = (unsorted[0].y + unsorted[1].y + unsorted[2].y + unsorted[3].y) / 4; (int = 0; < 4; i++) { if (unsorted[i].x < middlex && unsorted[i].y < middley) sorted[0] = unsorted[i]; if (unsorted[i].x > middlex && unsorted[i].y < middley) sorted[1] = unsorted[i]; if (unsorted[i].x < middlex && unsorted[i].y > middley) sorted[2] = unsorted[i]; if (unsorted[i].x > middlex && unsorted[i].y > middley) sorted[3] = unsorted[i]; } unsorted = sorted; } ... vector<rotatedrect> minrect( contours.size() ); for( int = 0; < contours.size(); i++ ) { minrect[i] = minarearect( mat(contours[i]) ); } point2f rect_points[4]; for( int = 0; < contours.size(); i++ ) { minrect[i].points( rect_points ); sortpoints( rect_points ); /* ...they not sorted after calling sortpoints?!? */ }
but it's not working, no compile error, points not sorted. think there's wrong data types.
the algorithm provided works if 4 points belong rectangle parallel x-y axis. way try return result not work properly. try copying sorted
array unsorted
. for ( int i=0;i<4;++i ) unsorted[i] = sorted[i];
but there way can use
#include <algorithm> struct str{ bool operator() ( point2f a, point2f b ){ if ( a.y != b.y ) return a.y < b.y; return a.x <= b.x ; } } comp; int main() { point2f v[4]; v[0] = point2f(0,1); v[1] = point2f(-1,1); v[2] = point2f(2,1); v[3] = point2f(4,1); sort(v,v+4,comp); }
Comments
Post a Comment