Home OpenCV (java) detected circle and crop in android studio
Reply: 0

OpenCV (java) detected circle and crop in android studio

awa
1#
awa Published in 2018-02-14 02:12:18Z

I am using the openCV Hough Circle to detect the circle on a image. (Code as following)

I would like to do :

1) Using the Hough Circle algorithm to detect the circle on the image (done)

2) Shows the detected circle on the image to the user (done)

3) When user clicks on "crop"it will only crop the portion that the coin is on the image and eliminate the rest such as the background

4) When the detection of the coin is incorrect, the user is able to customize the it and crop the customize portion

5) The portion of the "Cropped circle/image" has to be shown on the imageView.

Please kindly help and suggest how can i go about doing it. Thank you !

Code to circle detection :

private void circleDetection() {

        Bitmap bmp = ((BitmapDrawable) capturedPhoto.getDrawable()).getBitmap();
        Mat mat = new Mat();
        Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
        Utils.bitmapToMat(bmp32, mat);

        // convert to grayscale
        Mat grayMat = new Mat();
        int colorChannels = (mat.channels() == 3) ? Imgproc.COLOR_BGR2GRAY : ((mat.channels() == 4) ? Imgproc.COLOR_BGR2GRAY : 1);
        Imgproc.cvtColor(mat, grayMat, colorChannels,1);

        //reduce the noise to avoid false circle detection
        Imgproc.GaussianBlur(grayMat, grayMat, new
                Size(7, 7), 2, 2);

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

        //min and max radii
        int minRadius = 0, maxRadius = 0;
        double param1 = 70, param2 = 180;

        // create a Mat object to store the circles detected
        Mat circles = new Mat(bmp32.getWidth(), bmp32.getHeight(), CvType.CV_8UC1);

        //find the circle in the image
        Imgproc.HoughCircles(grayMat, circles, Imgproc.CV_HOUGH_GRADIENT, dp, minDist, param1, param2, minRadius, maxRadius);

        //get the number of circles detected
        int numberOfCircles = (circles.rows() == 0) ? 0 : circles.cols();

        //draw the circles found on the image
        for (
                int i = 0;
                i < numberOfCircles; i++) {
            //get the co-ordinates of circle
            double[] circleCoordinates = circles.get(0, i);
            int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1];
            Point center = new Point(x, y);
            int radius = (int) circleCoordinates[2];

            //circle outline
            Imgproc.circle(mat, center, radius, new Scalar(0,
                    255, 0), 4);
            double pi = 3.14;
            Coinarea = pi * radius * radius;
            Log.d("Touched: ", "( "+ Coinarea );
            GlobalVariables.AreaOfDetectedCircle=Coinarea;
        }
        // convert back to bitmap
        Utils.matToBitmap(mat, bmp);
    }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.392625 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO