Ask Your Question

Oleksandr's profile - activity

2015-07-06 02:44:51 -0600 asked a question Android OpenCV Improving detection quality

Currently I am developing an app, which will detect circles from photos. I have managed to write a code for this, but through some testing I noticed that it has trouble recognizing shape if I move phone away a bit. Can I somehow improve the result? Because I have seen as way smaller circles were detected in other peoples programs. Also I need my app to recon only 9 circles, which it does if I have more, but if the amount is smaller it crashes. Here's my detection code:

Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
                    CvType.CV_8UC1);
            Mat grayMat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
                    CvType.CV_8UC1);

            Utils.bitmapToMat(bitmap, mat);

            int colorChannels = (mat.channels() == 3) ? Imgproc.COLOR_BGR2GRAY
                    : ((mat.channels() == 4) ? Imgproc.COLOR_BGRA2GRAY : 1);

            Imgproc.cvtColor(mat, grayMat, colorChannels);


            Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 2, 2);

        // accumulator value
        double dp = 1.2d;
        // minimum distance between the center coordinates of detected circles in pixels
        double minDist = 100;


        int minRadius = 0, maxRadius = 0;

        double param1 = 70, param2 = 72;


        Mat circles = new Mat(bitmap.getWidth(),
                bitmap.getHeight(), CvType.CV_8UC1);


        Imgproc.HoughCircles(grayMat, circles,
                Imgproc.CV_HOUGH_GRADIENT, dp, minDist, param1,
                param2, minRadius, maxRadius);


        int numberOfCircles = 9;


        for (int i=0; i<numberOfCircles;  i++) {



            double[] circleCoordinates = circles.get(0, i);


            int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1];

            Point center = new Point(x, y);
            android.graphics.Point centerC = new android.graphics.Point(x, y);

            int radius = (int) circleCoordinates[2];



            Core.circle(mat, center, radius, new Scalar(0,
                    255, 0), 4);


            Core.rectangle(mat, new Point(x - 5, y - 5),
                    new Point(x + 5, y + 5),
                    new Scalar(0, 128, 255), -1);

Thanks in advance.