Oleksandr Firsov
Oleksandr Firsov

Reputation: 1478

Android OpenCV Can't Detect Circles

Currently I'm developing android app to detect circles in camera view. I'm new to OpenCV and right now I'm trying to detect circles in Image not Camera for starters. I have written this code:

Bitmap photo = BitmapFactory.decodeResource(getResources(), 
        R.drawable.circle);

Toast.makeText(getBaseContext(), "It works!", Toast.LENGTH_LONG).show();
Mat imgSource = new Mat(), imgCirclesOut = new Mat();

Utils.bitmapToMat(photo , imgSource);

Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur( imgSource, imgSource, new Size(9, 9), 2, 2 );
Imgproc.HoughCircles( imgSource, imgCirclesOut, Imgproc.CV_HOUGH_GRADIENT, 1, imgSource.rows()/8, 200, 100, 0, 0 );

float circle[] = new float[3];

for (int i = 0; i < imgCirclesOut.cols(); i++)
{
        imgCirclesOut.get(0, i, circle);
    org.opencv.core.Point center = new org.opencv.core.Point();
    center.x = circle[0];
    center.y = circle[1];
    Core.circle(imgSource, center, (int) circle[2], new Scalar(0,0,255), 5);
    }
    Bitmap bmp = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);

    Utils.matToBitmap(imgSource, bmp);




image.setImageBitmap(bmp);

That works when I press a button. According to all questions, tutorials and such that I checked this should work and detect my circle, but all it does is only process my image to grayscale. Here's how it looks in my app:

Before click After click

Help.

Upvotes: 2

Views: 894

Answers (1)

berak
berak

Reputation: 39796

you need to load the opencv native so's , before you can execute any opencv related code:

private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                // either call your opencv code from **here**
                // or from onCameraViewStarted(). either way, you will have 
                // to wait, until this thing finished (async!)
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};


@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}

Upvotes: 2

Related Questions