2
votes

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 clickAfter click

Help.

1
Have you seen this? - sop
No, I didn't. Thanks. I'll look into it and tell you about the results - Oleksandr Firsov
Ok. Here's my MainActivity. There are three questions in the end that I can't solve. Overall - the thread was very useful. Thanks! - Oleksandr Firsov
this should explain more - sop
So if I want to do it without touch I should use this: public Mat onCameraFrame(CvCameraViewFrame inputFrame) { Mat rgba = inputFrame.rgba(); //my code here return rgba; } - Oleksandr Firsov

1 Answers

2
votes

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);
}