Search code examples
javaandroidconnected-components

How to get connectedComponents with stats in android java using opencv and how to use these stats to get left point, right pint, centroid, area etc


Please clear I am new at android and java I have used this code but don't know what is meaning of these four variables Imgproc.connectedComponentsWithStats(binarized, labeled, rectComponents, centComponents);


        Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);

        Utils.bitmapToMat(bmp, tmp);

        Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);

        Imgproc.threshold(tmp, tmp, 40, 255, Imgproc.THRESH_BINARY);

        Imgproc.GaussianBlur(tmp, tmp, new org.opencv.core.Size(5, 5), 0 ,  0);

        Imgproc.threshold(tmp,tmp,130,255,Imgproc.THRESH_OTSU);

        Utils.matToBitmap(tmp, bmp);
        imageView.setImageBitmap(bmp);```

I want to get connected components with stats of tmp.

Solution

  • Thanks guys, I am answering my own question. I have found how to do it. int a = Imgproc.connectedComponentsWithStats(image,labels,stats,centroid); this function returns an integer which is the number of connected components in the image(Mat OpenCV) which is passing to the function (first argument).

    labels is a Mat datatype array which is of the size of the input image with the value of each pixel is label of that pixel in the original image

    centroid is centroid of each label(x,y).

    stats tells us area of the label and position of the label(return leftmost pixel, topmost pixel,width,height)