如何解决使用打开的简历获取Sudoku网格线
我正在开发一个可通过相机解决数独的应用程序。我正在使用OpenCV库。我花了一些时间进行研究,并决定尝试this项目,因为我是图像识别的新手。
我在检测网格线时遇到问题。使用我拥有的代码,app只检测到一条水平线或一条垂直线(取决于阈值,在51以下,返回一条垂直线;在52以上,返回一条水平线)。这是我发现拐角的功能,在这里我正在检测线条:
private List<Point> findCorners(Mat mat) {
Mat lines = new Mat();
List<double[]> horizontalLines = new ArrayList<double[]>();
List<double[]> verticalLines = new ArrayList<double[]>();
//Imgproc.GaussianBlur( mat,lines,new Size(5,5),2,2 );
int threshold = 150;
int minLineSize = 0;
int lineGap = 10;
//Imgproc.Canny(mat,70,100);
Imgproc.HoughLinesP(mat,1,Math.PI / 180,threshold);
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);
}
}
String lineInfo = String.format(
"horizontal: %d,vertical: %d,total: %d",horizontalLines.size(),verticalLines.size(),lines.cols());
Log.d(TAG_HOUGHLINES,lineInfo);
// find the lines furthest from centre which will be the bounds for the
// grid
double[] topLine = horizontalLines.get(0);
double[] bottomLine = horizontalLines.get(0);
double[] leftLine = verticalLines.get(0);
double[] rightLine = verticalLines.get(0);
double xMin = 1000;
double xMax = 0;
double yMin = 1000;
double yMax = 0;
for (int i = 0; i < horizontalLines.size(); i++) {
if (horizontalLines.get(i)[1] < yMin
|| horizontalLines.get(i)[3] < yMin) {
topLine = horizontalLines.get(i);
yMin = horizontalLines.get(i)[1];
} else if (horizontalLines.get(i)[1] > yMax
|| horizontalLines.get(i)[3] > yMax) {
bottomLine = horizontalLines.get(i);
yMax = horizontalLines.get(i)[1];
}
}
for (int i = 0; i < verticalLines.size(); i++) {
if (verticalLines.get(i)[0] < xMin
|| verticalLines.get(i)[2] < xMin) {
leftLine = verticalLines.get(i);
xMin = verticalLines.get(i)[0];
} else if (verticalLines.get(i)[0] > xMax
|| verticalLines.get(i)[2] > xMax) {
rightLine = verticalLines.get(i);
xMax = verticalLines.get(i)[0];
}
}
// obtain four corners of sudoku grid
Point topLeft = ImgManipUtil.findCorner(topLine,leftLine);
Point topRight = ImgManipUtil.findCorner(topLine,rightLine);
Point bottomLeft = ImgManipUtil.findCorner(bottomLine,leftLine);
Point bottomRight = ImgManipUtil.findCorner(bottomLine,rightLine);
List<Point> corners = new ArrayList<Point>(4);
corners.add(topLeft);
corners.add(topRight);
corners.add(bottomLeft);
corners.add(bottomRight);
return corners;
}
Here是输入图像
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。