1
$\begingroup$

Goodmorning, I have to do a circle detection with android studio using opencv, i follow the example find in internet but it didn't work and i don't understand why, i'm new of android. Can someone help me?

This is the code that I found in interent:

public class MainActivity extends Activity implements CvCameraViewListener2 {
private static final String    TAG = "OpencvEsempio";

private static final int       VIEW_MODE_RGBA              = 0;
private static final int       VIEW_MODE_HOUGH_CIRCLES     = 1;
private static final int       VIEW_MODE_CANNY             = 2;
private static final int       VIEW_MODE_HOUGH_LINES       = 5;

private int                    mViewMode;
private Mat                    mRgba;
private Mat                    mIntermediateMat;
private Mat                    mGray;

private MenuItem               mItemPreviewRGBA;
private MenuItem               mItemPreviewHoughCircles;
private MenuItem               mItemPreviewCanny;
private MenuItem               mItemPreviewFeatures;

private CameraBridgeViewBase   mOpenCvCameraView;

private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");


                System.loadLibrary("mixed_sample");

                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

public MainActivity() {
    Log.i(TAG, "Instantiated new " + this.getClass());
}


@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.circle_detection_cameraView);
    mOpenCvCameraView.setCvCameraViewListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.i(TAG, "called onCreateOptionsMenu");
    mItemPreviewRGBA = menu.add("Preview RGBA");
    mItemPreviewHoughCircles = menu.add("Hough Circles");
    mItemPreviewCanny = menu.add("Canny");
    mItemPreviewFeatures = menu.add("Hough Line");
    return true;
}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

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

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    mIntermediateMat = new Mat(height, width, CvType.CV_8UC4);
    mGray = new Mat(height, width, CvType.CV_8UC1);
}

public void onCameraViewStopped() {
    mRgba.release();
    mGray.release();
    mIntermediateMat.release();
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    final int viewMode = mViewMode;
    WindowManager wm = (WindowManager) this.getSystemService(this.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int width = display.getHeight();
    int height = display.getWidth();

    switch (viewMode) {
        case VIEW_MODE_HOUGH_CIRCLES:
            mRgba = inputFrame.rgba();
            Mat mGrayScale = new Mat(height + height / 2, width, CvType.CV_8UC1);
            Mat circles = new Mat();

            int iCannyUpperThreshold = 100;
            int iMinRadius = 20;
            int iMaxRadius = 400;
            int iAccumulator = 100;

            Imgproc.cvtColor(mRgba, mGrayScale, Imgproc.COLOR_RGB2GRAY, 4);
            Imgproc.HoughCircles(mGrayScale, circles, Imgproc.CV_HOUGH_GRADIENT,
                    1.0, mGrayScale.rows() / 8, iCannyUpperThreshold, iAccumulator,
                    iMinRadius, iMaxRadius);

            if (circles.cols() > 0)
                for (int x = 0; x < circles.cols(); x++)
                {
                    double vCircle[] = circles.get(0,x);

                    if (vCircle == null)
                        break;

                    Point pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
                    int radius = (int)Math.round(vCircle[2]);


                    Core.circle(mRgba, pt, radius, new Scalar(0,255,0), 3);
                    Core.circle(mRgba, pt, 3, new Scalar(0,0,255), 3);
                }
            break;

        case VIEW_MODE_RGBA:

            mRgba = inputFrame.rgba();
            break;

        case VIEW_MODE_CANNY:

            mRgba = inputFrame.rgba();
            Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100);
            Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
            break;

        case VIEW_MODE_HOUGH_LINES:
            mRgba = inputFrame.rgba();

            Mat thresholdImage = new Mat(height + height / 2, width, CvType.CV_8UC1);
            Imgproc.cvtColor(mRgba, thresholdImage, Imgproc.COLOR_RGB2GRAY, 4);

            Imgproc.Canny(thresholdImage, thresholdImage, 80, 100, 3,false);


            Mat lines = new Mat();
            int threshold = 50;
            int minLineSize = 20;
            int lineGap = 20;

            Imgproc.HoughLinesP(thresholdImage, lines, 1, Math.PI/180, threshold, minLineSize, lineGap);


            for (int x = 0; x < lines.cols() && x < 1; x++){
                double[] vec = lines.get(0, x);
                double x1 = vec[0],
                        y1 = vec[1],
                        x2 = vec[2],
                        y2 = vec[3];
                Point start = new Point(x1, y1);
                Point end = new Point(x2, y2);

                Core.line(mRgba, start, end, new Scalar(255,0,0), 3);
            }
            break;
    }

    return mRgba;
}

public boolean onOptionsItemSelected(MenuItem item) {
    Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);

    if (item == mItemPreviewRGBA) {
        mViewMode = VIEW_MODE_RGBA;
    } else if (item == mItemPreviewHoughCircles) {
        mViewMode = VIEW_MODE_HOUGH_CIRCLES;
    } else if (item == mItemPreviewCanny) {
        mViewMode = VIEW_MODE_CANNY;
    } else if (item == mItemPreviewFeatures) {
        mViewMode = VIEW_MODE_HOUGH_LINES;
    }

    return true;
}


//public native void FindFeatures(long matAddrGr, long matAddrRgba);}

and it give me a lot of error, when i run it on my nexus 5 the application it's stopped. Can someone help me please? thanks

$\endgroup$
  • $\begingroup$ I think the problem is in the method public void onResume() { super.onResume(); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this, mLoaderCallback); } $\endgroup$ – user3231800 Aug 18 '15 at 10:31
  • $\begingroup$ This seems more programming related. If you can give more detail about the errors it might be better on stackoverflow. $\endgroup$ – geometrikal Aug 19 '15 at 7:16
  • $\begingroup$ @geometrikal: Teah sorry, the problem is in resume, i have substitute OPENCV_VERSION_2_4_10 with OPENCV_VERSION_2_4_9 and now the camera work but don't detect anything. Thank, i post it on stackoverflow. $\endgroup$ – user3231800 Aug 22 '15 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.