use of org.opencv.core.Point in project kifu-recorder by leonardost.
the class InitialBoardDetector method process.
/**
* Processes the provided image. Returns true if the complete processing ran with success, i.e.,
* if a Go board was detected in the image. Returns false otherwise.
*
* @return boolean
*/
public boolean process() {
if (image == null) {
// throw error
return false;
}
Mat imageWithBordersInEvidence = detectBorders();
// Se quiser ver a saída do detector de bordas Cammy
// Imgproc.cvtColor(imageWithBordersInEvidence, previewImage, Imgproc.COLOR_GRAY2BGR, 4); if (true) return false;
// previewImage = imageWithBordersInEvidence; if (true) return false;
List<MatOfPoint> contours = detectContours(imageWithBordersInEvidence);
if (contours.isEmpty()) {
Log.i("KifuRecorder", "> Image processing: contours were not found.");
return false;
}
// Se quiser ver a saída do detector de contours
// Imgproc.drawContours(previewImage, contours, -1, new Scalar(0, 0, 255), 2); if (true) return false;
List<MatOfPoint> quadrilaterals = detectQuadrilaterals(contours);
if (quadrilaterals.isEmpty()) {
Log.i("KifuRecorder", "> Image processing: quadrilaterals were not found.");
return false;
}
// Se quiser ver a saída do detector de quadriláteros
// for (MatOfPoint quadrilatero : quadrilaterals) { List<MatOfPoint> listaContorno = new ArrayList<MatOfPoint>(); listaContorno.add(quadrilatero); Imgproc.drawContours(previewImage, listaContorno, -1, new Scalar(255, 0, 0), 3); } if (true) return false;
MatOfPoint boardQuadrilateral = detectBoard(quadrilaterals);
if (boardQuadrilateral == null) {
Log.i("KifuRecorder", "> Image processing: board quadrilateral was not found.");
return false;
}
QuadrilateralHierarchy quadrilateralHierarchy = new QuadrilateralHierarchy(quadrilaterals);
double averageArea = 0;
for (MatOfPoint quadrilateral : quadrilateralHierarchy.hierarchy.get(boardQuadrilateral)) {
averageArea += Imgproc.contourArea(quadrilateral);
}
averageArea /= quadrilateralHierarchy.hierarchy.get(boardQuadrilateral).size();
double boardArea = Imgproc.contourArea(boardQuadrilateral);
double ratio = averageArea / boardArea;
// internal quadrilaterals and the area of the board quadrilateral
if (ratio <= 1.0 / 324.0) {
// 18 quadrados por 18
boardDimension = 19;
} else if (ratio <= 1.0 / 144.0) {
// 12 quadrados por 12
boardDimension = 13;
} else {
boardDimension = 9;
}
List<Point> boardCorners = orderCorners(boardQuadrilateral);
if (shouldDrawPreview) {
// Drawer.desenhaInterseccoesECantosDoTabuleiro(previewImage, intersecoes, boardCorners);
Drawer.drawBoardContour(previewImage, boardQuadrilateral);
}
positionOfBoardInImage = new Mat(4, 1, CvType.CV_32FC2);
positionOfBoardInImage.put(0, 0, (int) boardCorners.get(0).x, (int) boardCorners.get(0).y, (int) boardCorners.get(1).x, (int) boardCorners.get(1).y, (int) boardCorners.get(2).x, (int) boardCorners.get(2).y, (int) boardCorners.get(3).x, (int) boardCorners.get(3).y);
/*
for (int i = 0; i < positionOfBoardInImage.rows(); ++i) {
for (int j = 0; j < positionOfBoardInImage.cols(); ++j) {
double[] valor = positionOfBoardInImage.get(i, j);
Log.d("KifuRecorder", "(" + i + ", " + j + ") = " + valor[0] + ", " + valor[1]);
}
}
*/
processedWithSuccess = true;
return true;
}
Aggregations