【发布时间】:2021-06-02 06:39:01
【问题描述】:
更新
你可以在这里找到我在 GitHub 上测试的所有图片:
GitHub repository with sources
还有 2 个视频,也应该在其中进行检测
原始问题
我尝试使用 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 程序的图像,它们有效:
我做错了什么?我无法经常检测到黑板的边缘。
关于黑板的其他信息:
- 总是矩形
- 可能有不同的照明
- 应该忽略文本,只检测主板
- 外部黑板也应忽略
- 只应显示/返回主板的轮廓
感谢任何建议或代码!
【问题讨论】:
-
我建议您转换为 HSV 和绿色阈值以获得黑板。然后从阈值图像中获取轮廓。
-
这是一个选项,但我不仅想检测绿色板,还想检测黑色和其他颜色:/
标签: java python android opencv edge-detection