Skip to content

Instantly share code, notes, and snippets.

@nishanthbhat07
Created July 14, 2021 19:16
Show Gist options
  • Save nishanthbhat07/4ce3206f784734282ac5e817dd6366af to your computer and use it in GitHub Desktop.
Save nishanthbhat07/4ce3206f784734282ac5e817dd6366af to your computer and use it in GitHub Desktop.
Border identification and correction

Our first task is to detect the paper from the background. Algorithm:

Border Detection

Mat borderDetection(Mat ...)
{
    ...
    
    Imgproc.findContours(srcGray, contours, hierarchy,
    Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    ...
    
    Core.kmeans(samples, clusterCount, labels, new
    TermCriteria(TermCriteria.MAX_ITER |
    TermCriteria.EPS, 10000, 0.0001), attempts,
    Core.KMEANS_PP_CENTERS, centers);
    ...
    
    Mat drawing = Mat.zeros(srcRes.size(), CvType.CV_8UC1);
    Imgproc.drawContours(drawing, contours, index, new Scalar(255),1);
    ...
    
    Mat lines = new Mat();
    Imgproc.HoughLinesP(drawing, lines, 1, Math.PI/180, 70, 30, 10);

    return drawing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment