Ask Your Question

Revision history [back]

O forgive me I forgot a step. After making it a binary image on dynamic thresholding, look for straight lines first, then apply a convex hull on that set of lines. Something like this code snippet should do the trick, adapting filters to your needs.

// Grayscale image and smoothing
Mat grayscale;
cvtColor(half_beeld, grayscale, CV_RGB2GRAY);
GaussianBlur(grayscale, grayscale, Size(5,5), 0, 0);
imshow("grayscale blur gaussian", grayscale);

// Adaptive thresholding
Mat adaptive;
adaptiveThreshold(grayscale, adaptive, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 5, 4);
// REMARK --> inverse image needed for HoughLines
bitwise_not(adaptive, adaptive);
imshow("adaptive threshold", adaptive);

// Parameters for convex hull definement
vector<Point> pointset;

// Calculate Hough lines
Mat show = original.clone();
vector<Vec4i> lines;
HoughLinesP(adaptive, lines, 1, CV_PI/180, 70, 30, 10);
for( size_t i = 0; i < lines.size(); i++ )
{
    line( show, Point(lines[i][0], lines[i][1]), Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 1, 8 );
    // Sla ook telkens het begin en eindpunt op van de lijn, want die moet uiteindelijk zeker in de convex hull zitten!
    pointset.push_back(Point(lines[i][0], lines[i][1]));
    pointset.push_back(Point(lines[i][2], lines[i][3]));
}
imshow("Hough Lines", show);

// Calculate convex hull
vector<Point> convex_hull;
convexHull(pointset, convex_hull);

O forgive me I forgot a step. After making it a binary image on dynamic thresholding, look for straight lines first, then apply a convex hull on that set of lines. Something like this code snippet should do the trick, adapting filters to your needs.

// Grayscale image and smoothing
Mat grayscale;
cvtColor(half_beeld, cvtColor(original, grayscale, CV_RGB2GRAY);
GaussianBlur(grayscale, grayscale, Size(5,5), 0, 0);
imshow("grayscale blur gaussian", grayscale);

// Adaptive thresholding
Mat adaptive;
adaptiveThreshold(grayscale, adaptive, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 5, 4);
// REMARK --> inverse image needed for HoughLines
bitwise_not(adaptive, adaptive);
imshow("adaptive threshold", adaptive);

// Parameters for convex hull definement
vector<Point> pointset;

// Calculate Hough lines
Mat show = original.clone();
vector<Vec4i> lines;
HoughLinesP(adaptive, lines, 1, CV_PI/180, 70, 30, 10);
for( size_t i = 0; i < lines.size(); i++ )
{
    line( show, Point(lines[i][0], lines[i][1]), Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 1, 8 );
    // Sla ook telkens het begin en eindpunt op van de lijn, want die moet uiteindelijk zeker in de convex hull zitten!
    pointset.push_back(Point(lines[i][0], lines[i][1]));
    pointset.push_back(Point(lines[i][2], lines[i][3]));
}
imshow("Hough Lines", show);

// Calculate convex hull
vector<Point> convex_hull;
convexHull(pointset, convex_hull);