问题描述
我正在开发一个可通过相机解决数独的应用程序。我正在使用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 (将#修改为@)