Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find largest contour in java opencv

I have used find contours and boundingrect and display it at my project. then I want to find the largest contours and display it. Is this possible? I am newbie to OpenCV java lang.

heres my code so far:

@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;

Hopefully, someone has some experience in this. Thanks..

like image 244
Bagus W Avatar asked Aug 04 '16 06:08

Bagus W


People also ask

Where is the biggest contour on OpenCV?

OpenCV is very dynamic in which we can first find all the objects (or contours) in an image using the cv2. findContours() function. We can then find the size of each object using the cv2. contourArea() function.

How do you find the contour area in OpenCV?

Contour area is given by the function cv. contourArea() or from moments, M['m00'].

How do you find the area of contours?

You can also find the area of the shapes in the given input images. To do so you need to invoke the contourArea() method of the Imgproc class. This method accepts the contour of a particular shape, finds and returns its area.


1 Answers

With function Imgproc.contourArea you can just simply find the areas of all of your contours and the contour with the largest area would simply be the largest one.

Code to draw the largest contour would be like this:

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);
like image 172
Dainius Šaltenis Avatar answered Sep 20 '22 20:09

Dainius Šaltenis