Как правильно вернуть иерархию в методе Imgproc.findContours OpenCV в Android для просмотра различных иерархий - PullRequest
0 голосов
/ 22 сентября 2019

Попытка получить иерархию после выполнения Imgproc.findContours, чтобы получить представление иерархии моих контуров, однако, кажется, нет никакого прямого способа распечатать иерархию в отличие от Python.Кроме того, мои результаты, похоже, возвращают много контуров, несмотря на использование Imgproc.RETR_EXTERNAL в качестве режима поиска контуров вместо Imgproc.RETR_TREE / LIST.

Я попытался создать массив и передать каждую иерархию (i, j) вывод в массив для распечатки, однако, как вы можете видеть, вывод уже имеет форму вложенного массива типа double [].Есть ли способ печатать каждую иерархию линейно?

    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

        InputFrame = inputFrame.rgba();

        Core.transpose(InputFrame,mat1); //transpose mat1(src) to mat2(dst), sorta like a Clone!
        Imgproc.resize(mat1,mat2,InputFrame.size(),0,0,0);    // params:(Mat src, Mat dst, Size dsize, fx, fy, interpolation)   Extract the dimensions of the new Screen Orientation, obtain the new orientation's surface width & height.  Try to resize to fit to screen.
        Core.flip(mat2,InputFrame,-1);   // mat3 now get updated, no longer is the Origi inputFrame.rgba BUT RATHER the transposed, resized, flipped version of inputFrame.rgba().

        int rowWidth = InputFrame.rows();
        int colWidth = InputFrame.cols();

        Imgproc.cvtColor(InputFrame,InputFrame,Imgproc.COLOR_RGBA2RGB);
        Imgproc.cvtColor(InputFrame,InputFrame,Imgproc.COLOR_RGB2HSV);


        Lower_Yellow = new Scalar(21,150,150);    //HSV color scale  H to adjust color, S to coontrol color variation, V is indicator of amt of light required to be shine on object to be seen.
    Upper_Yellow = new Scalar(31,255,360);    //HSV color scale


        Core.inRange(InputFrame,Lower_Yellow, Upper_Yellow, maskForYellow);


        final Size kernelSize = new Size(5, 5);  //must be odd num size & greater than 1.
        final Point anchor = new Point(-1, -1);   //default (-1,-1) means that the anchor is at the center of the structuring element.
        final int iterations = 1;   //number of times dilation is applied.  https://docs.opencv.org/3.4/d4/d76/tutorial_js_morphological_ops.html

        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, kernelSize);

        Imgproc.morphologyEx(maskForYellow, yellowMaskMorphed, Imgproc.MORPH_CLOSE, kernel, anchor, iterations);   //dilate first to remove then erode.  White regions becomes more pronounced, erode away black regions



        Mat mIntermediateMat = new Mat();
        Imgproc.GaussianBlur(yellowMaskMorphed,mIntermediateMat,new Size(9,9),0,0);   //better result than kernel size (3,3, maybe cos reference area wider, bigger, can decide better whether inrange / out of range.
        Imgproc.Canny(mIntermediateMat, mIntermediateMat, 5, 120);   //try adjust threshold   //https://stackoverflow.com/questions/25125670/best-value-for-threshold-in-canny

        List<MatOfPoint> contours = new ArrayList<>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(mIntermediateMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));   

            byte[] arr = new byte[100];
            //List<double>hierarchyHolder = new ArrayList<>();
            int cols = hierarchy.cols();
            int rows = hierarchy.rows();
            for (int i=0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                   //hierarchyHolder.add(hierarchy.get(i,j));
                   //hierarchy.get(i,j) is a double[] type, not byte.
                    Log.d("hierarchy"," " + hierarchy.get(i,j).toString());   

                }
            }

Один из множества возвращаемых выходов LogCat для иерархии, как показано ниже:

    D/hierarchy:  [D@2a6818f
             [D@ff8ec1c
             [D@e954025
             [D@b5c5fa
             [D@913a7ab
             [D@3b6ae08
             [D@ffeb9a1
             [D@f8a47c6
             [D@8e31387
             [D@a0ca2b4
             [D@c2c4edd
             [D@ad31a52
             [D@b536123
             [D@e2b3620
             [D@fa3fbd9
             [D@264899e
             [D@4f0ec7f
             [D@bdd944c
             [D@f4f7c95
             [D@d0ba1aa
             [D@cb1d19b
             [D@e75a938
             [D@af84d11
             [D@f5a2e76
             [D@8d1ec77
             [D@27820e4
             [D@b83a94d
        D/hierarchy:  [D@332bc02
             [D@d6ed913
             [D@6086750
             [D@8ee8d49
             [D@c14964e
             [D@8e3f36f
             [D@414a87c
             [D@409b505
             [D@427c95a
             [D@9e6578b
             [D@641d068
             [D@ff59c81
             [D@e092126
             [D@b60e167
             [D@9978b14
             [D@75e7fbd
             [D@75629b2
             [D@2102d03
             [D@1ec4480

Изображение вывода, созданного на CameraFrame: 1

...