line segment detector giving large number of lines

asked 2016-09-19 13:17:33 -0500


I'm new to OpenCV and new to extracting info from images. I have implemented the line segment detector to operate on an image that I have skeletonized (edges do not work for my purposes, I need centers of objects). The skeleton image is 8 bit single channel with values of 0 or 255. I want to find the lines in this image but the line segment detector (and the Hough transform) are returning a HUGE number of lines when there are really not that many. Since both the LSD and Hough are failing in similar ways, I suspect the input skeletonized image is the problem. I'm uncertain how to remedy the problem however since it is a fairly simple image with mostly zeros in it.

Below is the code, I didn't include how I read in the images but you get the idea, I hope. the skeleton comes out exactly as I need, here is a pic.image description

Mat img8bit(ysize, xsize, CV_8U);

Mat imgthresh(ysize, xsize, CV_8U);
threshold(img8bit, imgthresh, 200, 255, CV_THRESH_BINARY);

//this creates a Mat for storing the skeleton and sets it to zero
Mat skel(img.size(), CV_8UC1, cv::Scalar(0));
//this Mat is for intermediate storage for computations
Mat temp;
//stores eroded image
Mat eroded;

Mat element = getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3));

Mat closing(ysize, xsize, CV_8U);
//there are some holes in the circles, try to fill them in
morphologyEx(imgthresh, closing, MORPH_CLOSE, element);

    //get skeleton
bool done = false;
while (!done)
    erode(closing, eroded, element);
    dilate(eroded, temp, element); // temp = open(img)

    subtract(closing, temp, temp);

    bitwise_or(skel, temp, skel);


    done = (countNonZero(closing) == 0);

Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_STD);

vector<Vec4f> lines_std;

// Detect the lines
ls->detect(skel, lines_std);

// Show found lines
Mat drawnLines(img8bit);
ls->drawSegments(drawnLines, lines_std);
imshow("Standard refinement", drawnLines);
edit retag flag offensive close merge delete