我试图在 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 图像已经是二进制格式了。
你检测到任何错误了吗?
更新: 我更新了代码,它看起来像这样:
public 垫子轮廓(Mat 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;
我仍然没有得到所有的轮廓,我得到的是浅白色。有任何更正的想法吗?
最佳答案
Imgproc.findContours
在内部改变了输入 Mat
,它创建了一个 Artifact ,如上所示,您可以将 Mat
作为克隆对象传递,因此更改不会反射(reflect)在输入 Mat
上。我不确定是否有更好的解决方案,因为克隆 Mat
肯定会占用一些时间和空间(不会太多)。所以你可以使用:
Imgproc.findContours(mat.clone(), contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
关于java - JAVA (Android) 中的 OpenCV 绘制轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44047948/