【发布时间】:2025-12-10 20:25:03
【问题描述】:
我正在尝试在 JAVA (Android) 中绘制图像的轮廓,但它似乎什么也没做(该函数不绘制任何东西)这是代码:
public Mat contours(Mat mat)
{
ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Mat balele = new Mat();
mat.convertTo(balele, CvType.CV_32SC1);
// find contours:
Imgproc.findContours(balele, contours, hierarchy, Imgproc.RETR_FLOODFILL, Imgproc.CHAIN_APPROX_SIMPLE);
Mat source = new Mat(balele.size(), balele.type());
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
{
Imgproc.drawContours(source, contours, contourIdx, new Scalar(0,0,255), -1);
}
return source;
}
当我们将mat图像传递给方法时,它已经是二进制格式了。
您是否发现任何错误?
更新: 我更新了代码,它看起来像这样:
公共垫子轮廓(垫子垫) {
ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
// find contours:
Imgproc.findContours(mat, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
System.out.println(contours.size() + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
Mat source = new Mat();
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
{
Imgproc.drawContours(source, contours, contourIdx, new Scalar(0,0,255), -1);
}
return source;
}
我仍然没有得到所有的轮廓并且我得到了浅白色。有更正的想法吗?
【问题讨论】:
-
你能打印
contours.size()吗? -
是的,我测试的图像结果是:5850
-
好的。函数返回什么?黑矩阵?
-
我编辑了原帖,你可以看看吗?
-
我现在也有同样的问题。无论您设置什么颜色,drawContours() 似乎都只能绘制黑色。是 OpenCV 的 bug 吗?