更新
您可以在我的GitHub上找到我用于测试的所有图像:
还有两个视频,检测应该也能正常工作。
原始问题
我尝试使用OpenCV 4.x.x查找黑板的边缘(如下所示的图像),但不知何故我无法成功。我目前的代码如下(具有OpenCV和实时相机输入的Android平台),其中imgMat是来自相机输入的Mat:
Mat gray = new Mat();
Imgproc.cvtColor(imgMat, gray, Imgproc.COLOR_RGB2BGR);
Mat blurred = new Mat();
Imgproc.blur(gray, blurred, new org.opencv.core.Size(3, 3));
Mat canny = new Mat();
Imgproc.Canny(blurred, canny, 80, 230);
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new org.opencv.core.Size(2, 2));
Mat dilated = new Mat();
Imgproc.morphologyEx(canny, dilated, Imgproc.MORPH_DILATE, kernel, new Point(0, 0), 10);
Mat rectImage = new Mat();
Imgproc.morphologyEx(dilated, rectImage, Imgproc.MORPH_CLOSE, kernel, new Point(0, 0), 5);
Mat endproduct = new Mat();
Imgproc.Canny(rectImage, endproduct, 120, 230);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(endproduct, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = 0;
boolean hasContour = false;
MatOfPoint2f biggestContour = new MatOfPoint2f();
Iterator<MatOfPoint> each = contours.iterator();
while (each.hasNext()) {
MatOfPoint wrapper = each.next();
double area = Imgproc.contourArea(wrapper);
if (area > maxArea) {
maxArea = area;
biggestContour = new MatOfPoint2f(wrapper.toArray());
hasContour = true;
}
}
if (hasContour) {
Mat output = imgMat.clone();
MatOfPoint2f approx = new MatOfPoint2f();
MatOfPoint poly = new MatOfPoint();
Imgproc.approxPolyDP(biggestContour, approx, Imgproc.arcLength(biggestContour, true) * .02, true);
approx.convertTo(poly, CvType.CV_32S);
Rect rect = Imgproc.boundingRect(poly);
}
虽然这段Python代码在我的电脑上使用视频时有效,但我现在无法使其正常工作。我从矩形中获取输出,并在我的移动屏幕上显示它,但是它会频繁闪烁并且无法正常工作。
这些是我尝试过Python程序的图像,并且它们可以正常工作:
我做错了什么?我不能持续检测黑板的边缘。
关于黑板的其他信息:
- 始终为矩形
- 可能有不同的光线
- 应忽略文本,仅应检测到主板
- 外部黑板也应该被忽略
- 仅应显示/返回主板的轮廓
感谢任何建议或代码!