【问题标题】:how to find largest contour in java opencv如何在java opencv中找到最大的轮廓
【发布时间】:2016-12-10 03:20:25
【问题描述】:

我使用了 find contours 和 boundingrect 并将其显示在我的项目中。然后我想找到最大的轮廓并显示它。这可能吗?我是 OpenCV java lang 的新手。

这是我目前的代码:

@Override
public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    mHsv = new Mat(height,width,CvType.CV_8UC3);
    hierarchy = new Mat();
    mHsvMask = new Mat();
    mDilated = new Mat();
    mEroded = new Mat();
}

@Override
public void onCameraViewStopped() {
    mRgba.release();
    mHsv.release();
    mHsvMask.release();
    mDilated.release();
    hierarchy.release();

}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    mRgba =inputFrame.rgba();
    contours = new ArrayList<MatOfPoint>();
    hierarchy =new Mat();
    mHsv = new Mat();
    mHsvMask =new Mat();

    Imgproc.cvtColor(mRgba, mHsv, Imgproc.COLOR_RGB2HSV);

    Scalar lowerThreshold = new Scalar ( 0, 0, 0 ); // Blue color – lower hsv values
    Scalar upperThreshold = new Scalar ( 179, 255, 10 ); // Blue color – higher hsv values
    Core.inRange ( mHsv, lowerThreshold , upperThreshold, mHsvMask );

     //just some filter
   //Imgproc.dilate ( mHsvMask, mDilated, new Mat() );
    //Imgproc.erode(mDilated,mEroded,new Mat());


    Imgproc.findContours(mHsvMask, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

    for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
    {
        //Minimun size allowed for consideration
        MatOfPoint2f approxCurve = new MatOfPoint2f();
        MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(contourIdx).toArray());

      //Processing on mMOP2f1 which is in type MatOfPoint2f
        double approxDistance = Imgproc.arcLength(contour2f,true)*0.02;
        Imgproc.approxPolyDP(contour2f,approxCurve,approxDistance,true);

        //convert to MatofPoint
        MatOfPoint point = new MatOfPoint(approxCurve.toArray());

        //get boundingrect from contour
        Rect rect = Imgproc.boundingRect(point);

        Imgproc.rectangle(mRgba,new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0, 255),3);
        //bisa Imgproc.rectangle(mRgba, rect.tl(), rect.br(), new Scalar(255, 0, 0),1, 8,0);


        //show contour kontur
        if(Imgproc.contourArea(contours.get(contourIdx))>100) {
            Imgproc.drawContours(mRgba, contours, contourIdx, new Scalar(0,255,0), 5);
        }
    }
    return mRgba;

希望有人对此有一些经验。谢谢..

【问题讨论】:

  • 是的,这肯定是可能的,你能更准确地定义最大轮廓是什么意思吗?您想找到面积最大的轮廓(白色像素)还是只需要面积最大的轮廓boundingRect()
  • @ZadR 我想展示最大的边界框。希望你能帮助我。对不起,但我是新手。

标签: java android opencv opencv4android opencv-contour


【解决方案1】:

使用函数Imgproc.contourArea,您只需找到所有轮廓的面积,面积最大的轮廓就是最大的。

绘制最大轮廓的代码如下:

double maxVal = 0;
int maxValIdx = 0;
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
{
    double contourArea = Imgproc.contourArea(contours.get(contourIdx));
    if (maxVal < contourArea)
    {
        maxVal = contourArea;
        maxValIdx = contourIdx;
    }
}

Imgproc.drawContours(mRgba, contours, maxValIdx, new Scalar(0,255,0), 5);

【讨论】:

  • ehmm,我试试看,但我想找到最大的矩形区域。不是contourArea,你能帮我吗?使用 boundingrect 显示最大的一个边界框。谢谢
  • 因此,您可以通过同样的方式找到边界矩形的最大区域。只需通过所有矩形,通过乘以它们的高度和宽度来计算它们的面积,然后找到最大的一个。
  • 一旦你有了这个,是否有可能得到它的角落?详细问题->stackoverflow.com/questions/55311775/…
猜你喜欢
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 2017-04-06
  • 2019-07-04
  • 2021-09-01
  • 2012-01-06
相关资源
最近更新 更多