From 2f5f45fcadeff1ca2f62bb15b3dbe28afc8ee7de Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 12 Jan 2017 15:01:18 +0100 Subject: [PATCH] ready to change coordinates --- lines.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/lines.cpp b/lines.cpp index 1921e73..3f2df55 100644 --- a/lines.cpp +++ b/lines.cpp @@ -164,6 +164,29 @@ find_longest_line(std::vector hull, unsigned begin, unsigned end) //t return bestline; } +cv::Point +map_point(double theta, cv::Point origin, cv::Point tomap) +{ + //that's the algorithm: res = Rt*(tomap - origin) + cv::Vec p_as_vector; + cv::Point p; + cv::Point res; + cv::Matx Rt; + + p = tomap - origin; + p_as_vector[0] = p.x; + p_as_vector[1] = p.y; + //TODO: fai matrice + Rt(0,0) = cos(theta); + Rt(0,1) = sin(theta); + Rt(1,1) = Rt(0,0); + Rt(1,0) = -Rt(0,1); + cv::Vec res_as_vector = Rt*p_as_vector; + res.x = res_as_vector[0]; + res.y = res_as_vector[1]; + return res; +} + int main(int argc, char *argv[]) { char const *fname = "files/masckera.png"; @@ -208,15 +231,20 @@ int main(int argc, char *argv[]) //these are the "horizontal" lines int *maxdistances = max2_distance(hull); + cv::Point corn_1, corn_2, corn_3, corn_4; + corn_1 = hull[maxdistances[0]]; + corn_2 = hull[(maxdistances[0]+1)%hull.size()]; + corn_3 = hull[maxdistances[1]]; + corn_4 = hull[(maxdistances[1]+1)%hull.size()]; #ifdef _DEBUG - //img = cv::imread(fname,CV_LOAD_IMAGE_COLOR); // uncomment to this to display image with colors - std::cout << "Maxdist1:" << maxdistances[0] << " " << hull[maxdistances[0]] << hull[(maxdistances[0]+1)%hull.size()] << std::endl; - std::cout << "Maxdist2:" << maxdistances[1] << " " << hull[maxdistances[1]] << hull[(maxdistances[1]+1)%hull.size()] << std::endl; - cv::circle(img,hull[maxdistances[0]],dotwidth>>2,BROWN,2); - cv::circle(img,hull[(maxdistances[0]+1)%hull.size()],dotwidth>>2,BROWN,2); - cv::circle(img,hull[maxdistances[1]],dotwidth>>2,BROWN,2); - cv::circle(img,hull[(maxdistances[1]+1)%hull.size()],dotwidth>>2,BROWN,2); + img = cv::imread(fname,CV_LOAD_IMAGE_COLOR); // uncomment to this to display image with colors + std::cout << "Maxdist1:" << maxdistances[0] << " " << corn_1 << corn_2 << std::endl; + std::cout << "Maxdist2:" << maxdistances[1] << " " << corn_3 << corn_4 << std::endl; + cv::circle(img,corn_1,dotwidth>>1,MAGENTA,2); + cv::circle(img,corn_2,dotwidth>>2,MAGENTA,2); + cv::circle(img,corn_3,dotwidth>>2,BROWN,2); + cv::circle(img,corn_4,dotwidth>>2,BROWN,2); cv::namedWindow("win", CV_GUI_NORMAL); // cv::imshow("4 corners",img); // cv::waitKey(0); @@ -249,7 +277,13 @@ int main(int argc, char *argv[]) } #endif - + // theta is the angle of the line connecting point 1 and 2; it will be the + // rotation of our new coordinate system + double theta = atan(((double)corn_1.y - corn_2.y)/(corn_1.x - corn_2.x)); + std::cout << "Theta = " << theta << std::endl; + assert(map_point(theta, corn_1, corn_1).x == 0); + assert(map_point(theta, corn_1, corn_1).y == 0); + assert(map_point(theta, corn_1, corn_2).y == 0); return EXIT_SUCCESS; }