vector<Point2f> findCorner(const Mat& img, int idx) { stringstream ss; ss << idx; string iname = ss.str(); Mat img_gray, d_edges; Mat line1 = img.clone(), line2 = img.clone(), line3 = img.clone(); Size imgSize = img.size(); cvtColor(img, img_gray, CV_BGR2GRAY); //GaussianBlur(img_gray, img_gray, Size(3, 3), 0, 0); // edges detector Canny(img_gray, d_edges, 50, 300); imwrite("imgEdge" + iname + ".jpg", d_edges); vector<Vec2f> lines; // hough lines detector HoughLines(d_edges, lines, 1, CV_PI/180, 50); line1 = drawHoughLines(lines, img); imwrite("imgSlines" + iname + ".jpg", line1); VHLINE vh; vector<Vec2f> vlines, hlines; vh = divideVHLines(lines); // sort two directional lines sort(vh.vlines.begin(), vh.vlines.end(), sortVLines); sort(vh.hlines.begin(), vh.hlines.end(), sortHLines); // get unique lines vlines = groupLines(vh.vlines, imgSize, 15.0); hlines = groupLines(vh.hlines, imgSize, 15.0); line2 = drawHoughLines(vlines, img); line3 = drawHoughLines(hlines, line2); imwrite("imglines" + iname + ".jpg", line3); vector<Point2f> corners; // get two orthogonal lines' intersection as corners for(size_t i = 0; i < hlines.size(); i++) { Point3d hline = getHoughHC(hlines[i]); for(size_t j = 0; j < vlines.size(); j++) { Point3f vline = getHoughHC(vlines[j]); Point3f corner = vline.cross(hline); normHC(corner); corners.push_back(Point2d(corner.x, corner.y)); } } // refine corners cornerSubPix(img_gray, corners, Size(15,15), Size(-1,-1), TermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 30, 0.01)); return corners; }
vector<Vec2f> groupLines(vector<Vec2f> lines, Size imgSize, double minDist) { size_t lsize = lines.size(); vector<Vec2f> unilines; unilines.push_back(lines[0]); for(size_t i = 1; i < lsize; i++) { Vec2f lastline = unilines.back(); Vec2f curline = lines[i]; Point3f unilineHC = getHoughHC(lastline); Point3f lineHC = getHoughHC(curline); Point3f crosspt = unilineHC.cross(lineHC); normHC(crosspt); // check if two lines intersect in the image if(crosspt.x >= 0 && crosspt.x < imgSize.width && crosspt.y >= 0 && crosspt.y < imgSize.height){ continue; } // check if two lines are parallel int ptype = checkParallel(lineHC, unilineHC); if(ptype){ double deltaDist = 0; if(ptype == 1){ deltaDist = abs(lineHC.z/lineHC.x - unilineHC.z/unilineHC.x); } if(ptype == 2){ deltaDist = abs(lineHC.z/lineHC.y - unilineHC.z/unilineHC.y); } if(ptype == 3){ double k = lineHC.x/unilineHC.x; double rt = sqrt(lineHC.x*lineHC.x + lineHC.y*lineHC.y); deltaDist = abs(k*unilineHC.z - lineHC.z)/rt; } if(deltaDist <= minDist){ continue; } } // the other cases are unique lines unilines.push_back(curline); } return unilines; }