From f7c0a9a62f8a3f009dea9d804ba7921a263fd6af Mon Sep 17 00:00:00 2001 From: boyska Date: Fri, 6 Jan 2017 00:55:17 +0100 Subject: [PATCH] refactor code that get more distant points --- lines.cpp | 65 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/lines.cpp b/lines.cpp index e5d0bd8..0e9f68f 100644 --- a/lines.cpp +++ b/lines.cpp @@ -5,6 +5,34 @@ auto WHITE = cv::Scalar(255,255,255); auto BLUE = cv::Scalar(200,10,10); auto MAGENTA = cv::Scalar(110,10,200); +// takes the two points that are more distant from the next, ie: +// i st distance(hull[i], hull[i+1]) is maximum and 2nd maximum +int* max2_distance(std::vector hull) +{ + int *idx = (int*) calloc(2, sizeof(int)); + int distance[2]; + for( unsigned i=0; hull.size()>i; i++ ) + { + cv::Point thispoint=hull[i]; + cv::Point nextpoint=hull[(i+1)%hull.size()]; + int d=pow(thispoint.x-nextpoint.x,2)+pow(thispoint.y-nextpoint.y,2); + if( d>distance[0] ) //the biggest till now + { + idx[1]=idx[0]; + idx[0]=i; + distance[1]=distance[0]; + distance[0]=d; + } + else if( d>distance[1] ) // it is the second biggest till now + { + idx[1]=i; + distance[1]=d; + } + } + + return idx; +} + int main(int argc, char *argv[]) { char const *fname = "files/masckera.png"; @@ -37,7 +65,6 @@ int main(int argc, char *argv[]) // choosing the biggest contour in the image // you can test it with files/2contours.png for(auto cont: contours) { - std::cout << " - " << cont.size() << std::endl; if(cont.size() > contour.size()) contour = cont; } @@ -46,40 +73,14 @@ int main(int argc, char *argv[]) std::vector hull; cv::convexHull(cv::Mat(contour),hull,false); - // takes the two points that are more distant from the next, ie: - // i st distance(hull[i], hull[i+1]) is maximum and 2nd maximum - int idx[2]={0}; - int distance[2]={0}; - for( unsigned i=0; hull.size()>i; i++ ) - { - cv::Point thispoint=hull[i]; - cv::Point nextpoint=hull[(i+1)%hull.size()]; - int d=pow(thispoint.x-nextpoint.x,2)+pow(thispoint.y-nextpoint.y,2); - if( d>distance[0] ) //the biggest till now - { - idx[1]=idx[0]; - idx[0]=i; - distance[1]=distance[0]; - distance[0]=d; - } - else if( d>distance[1] ) // it is the second biggest till now - { - idx[1]=i; - distance[1]=d; - } - } - - - std::cout - << "(" << idx[0] << "," << distance[0] << ") -- " - << "(" << idx[1] << "," << distance[1] << ")" << std::endl; + int *maxdistances = max2_distance(hull); //img = cv::imread(fname,CV_LOAD_IMAGE_COLOR); // uncomment to this to display image with colors unsigned short dotwidth = img.cols >> 6; // divide by 64, so efficient - cv::circle(img,hull[idx[0]],dotwidth,MAGENTA,-1); - cv::circle(img,hull[(idx[0]+1)%hull.size()],dotwidth,MAGENTA,-1); - cv::circle(img,hull[idx[1]],dotwidth>>1,BLUE,-1); - cv::circle(img,hull[(idx[1]+1)%hull.size()],dotwidth>>1,BLUE,-1); + cv::circle(img,hull[maxdistances[0]],dotwidth,MAGENTA,-1); + cv::circle(img,hull[(maxdistances[0]+1)%hull.size()],dotwidth,MAGENTA,-1); + cv::circle(img,hull[maxdistances[1]],dotwidth>>1,BLUE,-1); + cv::circle(img,hull[(maxdistances[1]+1)%hull.size()],dotwidth>>1,BLUE,-1); cv::namedWindow("4 corners",CV_WINDOW_NORMAL); cv::imshow("4 corners",img);