Java Android Opencv 2.3上的凸包

5

请帮我,

我在Android上遇到了凸包(Convex Hull)的问题。我使用Java和OpenCV 2.3

在我使用Java之前,我曾经在Visual Studio 2008上使用C++编写过此代码。

这段代码可以在C++上成功运行。

现在,我想将它从C++转换为Java并在Android上运行。但是当我在SDK Android模拟器上运行时,出现了"强制关闭"的错误。

以下是我的C++代码:

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
drawing = Mat::zeros( canny_output.size(), CV_64F );

/// Find the convex hull object for each contour
vector<vector<Point> > hull ( contours.size() );
for( int i = 0; i < contours.size(); i++ )
  {  convexHull( Mat(contours[i]), hull[i], false );
}

for(size_t i = 0; i < contours.size(); i++){
    drawContours( drawing, hull, i, Scalar(255, 255, 255), CV_FILLED ); // FILL WHITE COLOR
}

以下是我的Android代码:

Mat hierarchy = new Mat(img_canny.rows(),img_canny.cols(),CvType.CV_8UC1,new Scalar(0));
    List<Mat> contours =new ArrayList<Mat>();
    List<Mat> hull = new ArrayList<Mat>(contours.size());
    drawing = Mat.zeros(img_canny.size(), im_gray);

    Imgproc.findContours(img_dilasi, contours, hierarchy,Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));

    for(int i=0; i<contours.size(); i++){
        Imgproc.convexHull(contours.get(i), hull.get(i), false);

    }
    for(int i=0; i<contours.size(); i++){
        Imgproc.drawContours(drawing, hull, i, new Scalar(255.0, 255.0, 255.0), 5);
    }

提供信息,我在代码中对凸包进行了一些修改。 我在轮廓内填充了一种颜色

有人能帮我解决我的问题吗?

非常感谢您的帮助。


很难确定是什么导致了你的问题。你遇到了具体的错误吗?是否有与之相关的日志输出? - Aurelius
当我在SDK Android模拟器上运行它时,窗口错误出现了,“应用程序意外停止”,并告诉我通过点击“强制关闭”来停止。当我在Eclipse的Log Cat错误中进行检查时,我发现进程停在这一行上:“Imgproc.convexHull(contours.get(i),hull.get(i),false);”我不知道为什么进程会在那一行停止。你能帮忙解释一下吗? - Jenang
我觉得在声明“hull”变量时犯了一个错误。但是,我不知道如何解决这个问题。我认为我已经按照适用规定声明了该变量。你的意见呢? 你认为我的代码中声明变量“hull”是否正确? - Jenang
你的问题是否有任何回答能解决?如果是,你愿意接受其中之一吗?如果没有,请留下评论说明我们可以改进的地方。 - Aurelius
6个回答

10

我没有足够的声望来添加评论,只是想说上面两个答案帮助了我,在我的使用情况下让Imgproc.convexHull()正常工作,类似于这样(2.4.8):

MatOfPoint mopIn = ...
MatOfInt hull = new MatOfInt();
Imgproc.convexHull(mopIn, hull, false);

MatOfPoint mopOut = new MatOfPoint();
mopOut.create((int)hull.size().height,1,CvType.CV_32SC2);

for(int i = 0; i < hull.size().height ; i++)
{
    int index = (int)hull.get(i, 0)[0];
    double[] point = new double[] {
        mopIn.get(index, 0)[0], mopIn.get(index, 0)[1]
    };
    mopOut.put(i, 0, point);
}           
// do something interesting with mopOut

2
这个代码在我的应用程序中运行良好。在我的情况下,我有多个轮廓要处理,因此你会注意到有很多列表,但如果你只有一个轮廓,只需调整它以在没有.get(i)迭代的情况下工作即可。
这个线程更简单地解释了这个过程。 android java opencv 2.4 convexhull convexdefect
   // Find the convex hull
            List<MatOfInt> hull = new ArrayList<MatOfInt>();
            for(int i=0; i < contours.size(); i++){
                hull.add(new MatOfInt());
            }
            for(int i=0; i < contours.size(); i++){
                Imgproc.convexHull(contours.get(i), hull.get(i));
            }

            // Convert MatOfInt to MatOfPoint for drawing convex hull

            // Loop over all contours
            List<Point[]> hullpoints = new ArrayList<Point[]>();
            for(int i=0; i < hull.size(); i++){
                Point[] points = new Point[hull.get(i).rows()];

                // Loop over all points that need to be hulled in current contour
                for(int j=0; j < hull.get(i).rows(); j++){
                    int index = (int)hull.get(i).get(j, 0)[0];
                    points[j] = new Point(contours.get(i).get(index, 0)[0], contours.get(i).get(index, 0)[1]);
                }

                hullpoints.add(points);
            }

            // Convert Point arrays into MatOfPoint
            List<MatOfPoint> hullmop = new ArrayList<MatOfPoint>();
            for(int i=0; i < hullpoints.size(); i++){
                MatOfPoint mop = new MatOfPoint();
                mop.fromArray(hullpoints.get(i));
                hullmop.add(mop);
            }


            // Draw contours + hull results
            Mat overlay = new Mat(binaryImage.size(), CvType.CV_8UC3);
            Scalar color = new Scalar(0, 255, 0);   // Green
            for(int i=0; i < contours.size(); i++){
                Imgproc.drawContours(overlay, contours, i, color);
                Imgproc.drawContours(overlay, hullmop, i, color);
            }

2

Java示例(OpenCV 2.4.11)

hullMat包含由convexHull方法确定的gray子矩阵。您可能希望根据其面积过滤您真正需要的轮廓。

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
MatOfInt4 hierarchy = new MatOfInt4();
MatOfInt hull = new MatOfInt();

void foo(Mat gray) {
    Imgproc.findContours(gray, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);        
    for (int i = 0; i < contours.size(); i++) {
        Imgproc.convexHull(contours.get(i), hull);
        MatOfPoint hullContour = hull2Points(hull, contours.get(i));
        Rect box = Imgproc.boundingRect(hullContour);
        Mat hullMat = new Mat(gray, box);
        ...
    }
}

MatOfPoint hull2Points(MatOfInt hull, MatOfPoint contour) {
    List<Integer> indexes = hull.toList();
    List<Point> points = new ArrayList<>();
    MatOfPoint point= new MatOfPoint();
    for(Integer index:indexes) {
        points.add(contour.toList().get(index));
    }
    point.fromList(points);
    return point;
}

1

查看 findContours()convexHull() 的文档,似乎您错误地声明了变量 contourshull

尝试将声明更改为:

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
List<MatOfInt> hull = new ArrayList<MatOfInt>();

然后,当您调用convexHull()之后,hull包含contours中构成凸包的点的索引。为了使用drawContours()绘制这些点,您需要填充一个新的MatOfPoint,其中只包含凸包上的点,并将其传递给drawContours()。我将此留作练习给您完成。

1
为了补充Aurelius所说的,您在C++实现中使用了一个点向量,因此hull矩阵包含实际的凸壳点:
“在第一种情况下[整数索引向量],hull元素是原始数组中凸壳点的基于0的索引(因为凸壳点集是原始点集的子集)。在第二种情况下[点向量],hull元素是凸壳点本身。” - convexHull 这就是为什么您能够调用的原因。
drawContours( drawing, hull, i, Scalar(255, 255, 255), CV_FILLED );

在您的Android版本中,外壳输出仅是与原始轮廓.get(i)矩阵中的点相对应的索引数组。因此,您需要在原始矩阵中查找凸点。以下是一个非常粗略的想法:
MatOfInt hull = new MatOfInt();
MatOfPoint tempContour = contours.get(i);
Imgproc.convexHull(tempContour, hull, false); // O(N*Log(N))
//System.out.println("hull size: " + hull.size() + " x" + hull.get(0,0).length);
//System.out.println("Contour matrix size: " + tempContour.size() + " x" + tempContour.get(0,0).length);

int index = (int) hull.get(((int) hull.size().height)-1, 0)[0];
Point pt, pt0 = new Point(tempContour.get(index, 0)[0], tempContour.get(index, 0)[1]);
for(int j = 0; j < hull.size().height -1 ; j++){
    index = (int) hull.get(j, 0)[0];
    pt = new Point(tempContour.get(index, 0)[0], tempContour.get(index, 0)[1]);
    Core.line(frame, pt0, pt, new Scalar(255, 0, 100), 8);
    pt0 = pt;
}

-1
请使用此 fillconvexPoly
 for( int i = 0; i < contours.size(); i++ ){
          Imgproc.fillConvexPoly(image_2, point,new Scalar(255, 255, 255));
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接