c++ - Merge HoughLines -
i stuck @ 1 point in code. firstly short clarification i'm doing: input there's image of floor. canny , houghlinesp algorithm want segment whole wall in many "small" parts, see here, @ same time the ideal output (here without canny), get - segment between 2 red lines.
alright, since actually outout here
i wonder how merge lines, cloose each other. example lines 2,4,3,5,6 should 1 line , count one. line 7 till 15 should one, second line.
of course, did research , tried this:
#include <opencv/cv.h> #include <opencv/highgui.h> #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include <experimental/filesystem> using namespace cv; using namespace std; mat srcreal = //here's image http://imgur.com/a/kcjp6 mat src, dst, cdst; vector<vec4i> lines; void wallmapping(mat src) { scalar mu, sigma; meanstddev(src, mu, sigma); canny(src, dst, mu.val[0] - sigma.val[0], mu.val[0] + sigma.val[0], 3, false); cvtcolor(dst, cdst, cv_gray2bgr); houghlinesp(dst, lines, 1, cv_pi / 2, 50, 50, 200); sort(lines.begin(), lines.end(), vec4isortbyx()); ///sort lines number (size_t = 1; < lines.size(); i++) { vec4i current = lines[i]; ///set current lines point pt1 = point(current[0], current[1]); point pt2 = point(current[2], current[3]); vec4i previous = lines[i - 1]; ///set previous lines point ppt1 = point(previous[0], previous[1]); point ppt2 = point(previous[2], previous[3]); int gradient1, gradient2; if (pt1.x - pt2.x != 0) { gradient1 = (pt1.y - pt2.y) / (pt1.x - pt2.x); gradient2 = (ppt1.y - ppt2.y) / (ppt1.x - ppt2.x); } point avrgpt1, avrgpt2; if (gradient1 == gradient2) { avrgpt1.x = (pt1.x + ppt1.x) / 2; avrgpt2.x = (pt2.x + ppt2.x) / 2; avrgpt1.y = (pt1.y + ppt1.y) / 2; avrgpt2.y = (pt2.y + ppt2.y) / 2; } double angle = atan2(ppt2.y - ppt1.y, ppt2.x - ppt1.x) * 180.0 / cv_pi; ///draw vertical lines (90 degree) if (angle) { std::vector<int> linelabel; int numlines = cv::partition(lines, linelabel, isequal); line(cdst, avrgpt1, avrgpt2, scalar(0, 0, 255), 2, cv_aa); } cv::puttext(cdst, to_string(i+1), pt2 + point(0, 10), 4, 1, scalar(0, 255, 0), 1, 8, false); //some other stuff } //main int main(int argc, char *argv[]) { wallmapping(srcreal); waitkey(0); return 0; }
but @ picture above, see merging idea in code doesn't work. i'd glad idea, approaches or corrections of code! thanks!
draw lines in separate binary image, perfom morphological closing on them ones close each other merge, , erode image small kernel, e.g. 3x3 leave thin lines.
after that, non-zero coordinates binary image you've created , draw these coordinates in desired image.
in order these new lines vector, may find contours within binary image findcontours()
, find 2 extreme points (points of extreme x or y values) of each contour - these points describing line.
Comments
Post a Comment