c++ - How can I find the largest quadrangle -


how can find largest quadrangle in case?

in attached image can see have (in left) , wantto (in rigth). enter image description here

this code won't work because largest rectangle has crosses instead of corners.

int gamecontroller::getindexofexternalcontour(vector<vector<point>> contours) {     int largest_area=0; int largest_contour_index=0;  for( int = 0; i< contours.size(); i++ )           // iterate through each contour.  {     double = contourarea(contours[i], false);     //  find area of contour     if(a > largest_area)     {         largest_area = a;         largest_contour_index = i;                  //store index of largest contour     } } 

i wanted add convexity defects approach.

find largest contour, defect points, connect extremes.

// stl #include <algorithm> #include <iterator> #include <limits> using namespace std;  // cv #include <opencv2/opencv.hpp> using namespace cv;  int main() {     mat sample = imread("path/to/sample.jpg");     imshow("window", sample);     waitkey(0);      // images work on     mat black = mat(sample.rows, sample.cols, cv_8uc1, scalar(0));     mat clone = sample.clone();      // binarization     mat gray;     cvtcolor(sample, gray, cv_bgr2gray);     threshold(gray, gray, 127, 255, cv_thresh_otsu);      // find , fill largest contour     vector<vector<point> > contours;     vector<double> areas;     findcontours(gray, contours, cv_retr_list, cv_chain_approx_simple);     for(unsigned int = 0; < contours.size(); i++)     {         areas.push_back(abs(contourarea(contours[i])));     }     vector<double>::iterator biggest = max_element(areas.begin(), areas.end());     unsigned int id = distance(areas.begin(), biggest);     drawcontours(black, contours, id, scalar(255), -1);     imshow("window", black);     waitkey(0);      // convexity defects of thelargest contour     vector<point> external = contours[id];     vector<int> hull;     vector<vec4i> defects;     convexhull(external, hull);     convexitydefects(external, hull, defects);      // show defect points     for(unsigned int = 0; < defects.size(); i++)     {         circle(clone, external[defects[i][1]], 1, scalar(0, 255, 255), 3);     }     imshow("window", clone);     waitkey(0);      // find extremes     point tl, tr, bl, br;     point p;     double d_tl, d_tr, d_bl, d_br;     double m_tl = numeric_limits<double>::max();     double m_tr = numeric_limits<double>::max();     double m_bl = numeric_limits<double>::max();     double m_br = numeric_limits<double>::max();     for(unsigned int = 0; < defects.size(); i++)     {         p = external[defects[i][2]];         d_tl = (double)sqrt((double)pow((double)(p.x),2) + pow((double)(p.y),2));         d_tr = (double)sqrt((double)pow((double)(sample.cols - p.x),2) + pow((double)(p.y),2));         d_bl = (double)sqrt((double)pow((double)(p.x),2) + pow((double)(sample.rows - p.y),2));         d_br = (double)sqrt((double)pow((double)(sample.cols - p.x),2) + pow((double)(sample.rows - p.y),2));         if(d_tl < m_tl)         {             tl = p;             m_tl = d_tl;         }         if(d_tr < m_tr)         {             tr = p;             m_tr = d_tr;         }         if(d_bl < m_bl)         {             bl = p;             m_bl = d_bl;         }         if(d_br < m_br)         {             br = p;             m_br = d_br;         }     }      // draw rectangle     line(sample, tl, tr, scalar(0, 255, 255), 3);     line(sample, tr, br, scalar(0, 255, 255), 3);     line(sample, br, bl, scalar(0, 255, 255), 3);     line(sample, bl, tl, scalar(0, 255, 255), 3);     imshow("window", sample);     waitkey(0);      return 0; } 

enter image description here

enter image description here

enter image description here

enter image description here

you need try approach last step (find extreme defects)


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -