【问题标题】:OpenCV Android, drawing largest contour, something is wrongOpenCV Android,绘制最大轮廓,有问题
【发布时间】:2015-02-25 09:52:44
【问题描述】:

我正在尝试绘制最大的轮廓,我正在实时工作,处理每个相机帧。 我的输入图像将是手的,我有一些限制 - 黑色背景,没有镜面光。

我希望最大轮廓尽可能接近下图

我的代码如下,我参考了很多网上可用的示例,我几乎认为我的代码没有任何问题。

        mRgba = inputFrame.gray();
        contours = new ArrayList<MatOfPoint>();
        mcontours = new ArrayList<MatOfPoint>();
        List<Mat> hull = new ArrayList<Mat>(contours.size());

        hierarchy = new Mat();
        Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 50, 50);
        Imgproc.findContours(mIntermediateMat, contours, hierarchy,
                Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);


        for (int idx = 0; idx != contours.size(); ++idx)
        {
            Mat contour = contours.get(idx);
            double contourarea = Imgproc.contourArea(contour);
            Log.i(TAG, "maxAreaIdx =" + idx + "contourarea = " + contourarea);
            if (contourarea > maxArea)
            {
                maxArea = contourarea;
                maxAreaIdx = idx;
                Log.i(TAG, "maxAreaIdx =" + maxAreaIdx + "MaxArea = " + maxArea);

            }

        }

        if(maxAreaIdx > 0 && maxAreaIdx < contours.size())
            mcontours.add(contours.get(maxAreaIdx));
        else
            break;

        Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA,
                4);
        hierarchy.release();
        Imgproc.drawContours(mRgba, mcontours, -1, CONTOUR_COLOR, -1);

现在,我得到的只是其中的一小部分。我目前的理解是 OpenCV 本身无法检测到大的连续轮廓。请参考下图。

不仅如此,我还对其他图像进行了测试,但不知何故无法获得大而大的连续轮廓。

我先申请 Canny 有错吗?有没有更好的技术?我希望得到手部轮廓的轮廓。将来,我想将它与保存的类似轮廓数据库进行比较。我在正确的轨道上吗?

【问题讨论】:

    标签: java android opencv contour canny-operator


    【解决方案1】:

    试试这个:

    List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    
    //"tes" is binary image from threshold result       
    Imgproc.findContours(tes, mContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0,0));
        Mat overlay = contImg; //this is source image RGB
        for(int i = 0; i < mContours.size(); i++)
        {
                Imgproc.drawContours(overlay, mContours, -1, red, 2); //draw contour
        }
        return overlay;             
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      相关资源
      最近更新 更多