I am making an Android app for detecting a sudoku puzzle and finding the answer for the same. In the first part I need to extract the sudoku from a given image. I was successfully able to use HoughLinesP to find the containing box(of sudoku) but because of the thick lines present in the image, a very large number of lines are being generated. I need to merge houghLines that belong to the same "thick line" into a single line but I'm not able to do so. My code so far :
Mat inputMat = new Mat(inputBitmap.getHeight(), inputBitmap.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(inputBitmap, inputMat, false);
Mat grayMat = new Mat();
Imgproc.cvtColor(inputMat, grayMat, Imgproc.COLOR_BGR2GRAY);
Mat blurMat = new Mat();
Imgproc.blur(grayMat, blurMat, new Size(1, 1));
Mat cannyEdges = new Mat();
Imgproc.Canny(blurMat, cannyEdges, 50, 200);
Mat lines = new Mat();
Imgproc.HoughLinesP(cannyEdges, lines, 1, Math.PI / 180, 150);
List<double[]> horizontalLines = new ArrayList<>();
List<double[]> verticalLines = new ArrayList<>();
for(int i = 0 ; i < lines.cols() ; i++) {
double[] line = lines.get(0, i);
double x1 = line[0];
double y1 = line[1];
double x2 = line[2];
double y2 = line[3];
if (Math.abs(y2 - y1) < Math.abs(x2 - x1)) {
horizontalLines.add(line);
} else if (Math.abs(x2 - x1) < Math.abs(y2 - y1)) {
verticalLines.add(line);
}
}
So, technically I need 10 Horizontal lines and 10 vertical lines. PS : Only the sudoku is in view i.e no other lines are present in the image, just the plain sudoku. Please help me solve this issue and thankyou very much for your time :)