Ask Your Question

kevik's profile - activity

2017-04-11 16:27:49 -0500 received badge  Notable Question (source)
2016-07-11 12:59:05 -0500 received badge  Popular Question (source)
2014-05-08 00:40:11 -0500 asked a question processing images Asynchronously from onCameraFrame callback

I am getting the Mat image that is provided from onCameraFrame() callback method. this I get from CvCameraViewFrame inputFrame that is passed in the parameters.

This use of OpenCV in this example is the Java API version to be used in Android

the problem is that the Imgproc.HoughCircles() method for proccessing the image takes time. I want to use a background thread or Android AsyncTask for this. I tries this several time and it is not working. Is there some example that shows how to do this correctly or how can this be done.

running HoughCirles method inside onCamraFrame() really does slow down the camera frame rate. It looks like doing this on a separate thread is the best way to go. here is my attempt below. it does not work. it is using AsyncTaksk to run HoughCircles on a background thread.

i have tried using a boolean value as shown below to prevent any new frames from being started until the current thread running HoughCircles is finished. the other way i tried is to use a counter variable and only pull one frame out of 10 frames. but this did not work either.

he is what I tried as an example:

 public Mat onCameraFrame(final CvCameraViewFrame inputFrame) {

        mRgba = inputFrame.rgba(); // colo
        mGray = inputFrame.gray(); // grayscale required for detection

        // if detected circles is more than zero
        // or Animation settings page fragment is not visible on screen
        if((detected > 0)&&(!detectionRunning)) {  

        // prevent any more Asynchronous task threads from being launched
        // until this one is finished running
        detectionRunnning = true;

        // launnch new Asynchronous task that takes mGray Mat image
        // and checks to see of any circles exist by using
        // the OpenCV Imgproc.HoughCircles() method, passing
        // in the mGray as parameter to the doInBackground() method
        // of this class
        new CircleTask().execute(mGray);


        // mRgba must be returned with this callback method to show
        // up on screen of viewfinder
        return mRgba;

 } // onCameraFrame

 // background thread task takes Mat image from onCameraFrame and
 // runs the HoughCircles method on a background thread so UI thread is not blocked

 public class CircleTask extends AsyncTask<Mat, Void, Integer> {

 // run detection method in background thread
 // takes in parameter in the .execute(Mat mGray) call on the class that is created
 protected Integer doInBackground(Mat... params) {

   grayMatImage = params[0];

   MatOfRect circles = new MatOfRect();

   // doing a gaussian blur prevents getting a lot of false positives  
   Imgproc.GaussianBlur(mGray, mGray, new Size(5, 5), 2, 2);

  // for detection of circles  
  Imgproc.HoughCircles(mGray, circles, Imgproc.CV_HOUGH_GRADIENT,
                        1, mGray.rows()/8, 150, 60, 30, 0);

  // int detected is the number of circles detected                   
  int detected = circles.cols();  

  // this integer is passes to the onPostExecute method below
  return detected;

 // result Integer is passed here after
 // this method is run on maing UI thread
 protected void onPostExecute(Integer result) {

   Log.i("RESULT OF DETECTION", "number of circles detected " + result);

   // add methods here to be executed after circle is detected

   // stop blocking and allow the next frame to be started
   detectionRunning = false;


2013-12-10 01:38:49 -0500 asked a question CameraBridgeViewBase getWidth() and getHeight() null

how can i get getWidth() and getHeight() to work for a CameraBridgeViewBase object in OpenCV for Android/Java? it is returning null no matter where i put it

it gets null in onCreate for Android Activity method, so i put it in the onCameraViewStarted callback method and same null problem, i even tried the onContentChanged method for activity, any ideas?

2013-12-09 06:55:05 -0500 asked a question how to increase the sensitivity of the HoughCircle method
Imgproc.HoughCircles(mGray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 20);

i am using this method, the Java wrapper for the C++ function cvHoughCircles, using this with Android.

what are some ways i can make this method more sensitive for positive detection. i am using it to detect circular objects on a plain wooden desk background, so there is not much in that background to cause a false positive, but i have the opposite problem

i have some circular objects that it will not pick up and wanted to increase the accuracy. so more positive results would be better.

in addition i found that Imgproc.GaussianBlur(mGray, mGray, new Size(5, 5), 2, 2); was decreasing the sensitivity even more than it was before using it so i removed this method. i did see many examples on the internet that used it.

a little confused about the 4th parameter in the HoughCircle method shown above, in this case the 1, would that have any effect on how sensitive the method is?

from the documentation for the 4th parameter,

 dp - Inverse ratio of the accumulator resolution to the image resolution.
 For example, if dp=1, the accumulator has the same resolution as the input image. 
 If dp=2, the accumulator has half as big width and height.
2013-12-09 03:00:58 -0500 received badge  Editor (source)
2013-12-09 02:58:49 -0500 asked a question vary slow frame rate when calling HoughCircles() method from onCameraFrame, Android/Java

extremely slow frame rates when using the openCV Java method in android for detecting circular shaped objects in images

Imgproc.HoughCircles(mGray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 50); when i remove this method it runs fast, but after adding this method inside of this callback

public Mat onCameraFrame(final CvCameraViewFrame inputFrame) { the frame rate slows to 1 to 2 frames per second, I don't understand why it gets so slow, i tried putting this method in a separate thread and it would not help, the only thing that worked is to use a counter and and an if statement to run the method every 10 frames.

in the OpenCV examples there is a sample project called face detection and it has both a native C++ and Java camera versions and they both are vary fast, how is it possible that when I use similar code I get this slow constipated action from OpenCV?

is there something i am doing wrong here? In the face detection project from openCV examples they take every frame and they don't launch a separate thread. how do I fix this problem and make my code run fast like the sample projects in OpenCV?

in a different project I am also having the same problem of slow frame rate, in this practice project where I am not using openCV, it is just the android Camera class only, in that I am taking the image from the onPreviewFrame(byte[] data, Camera camera) method and doing some light processing like converting the YUV format from the byte array into a bitmap and putting that into another view on the same screen as the camera view, and the result is vary slow frame rate.

EDIT: In some additional experimentation i added the Imgproc.HoughCircles() method to the OpenCV face Detection sample project. putting this method inside the onCameraFrame method of the java detector.

the result is the same as in my project, it became vary slow. so the HoughCircles method probably takes more processing power than the face detection method CascadeClassifier.detectMultiScale(), however that does not explain the fact I watched other circle detection projects on youTube and in their videos the frame rate is not slowed down. that is why I think something is wrong with my code.

here is a sample of the code I am using

 public class CircleActivity extends Activity implements CvCameraViewListener2 {

Mat                    mRgba;
Mat                    mGray;
File                   mCascadeFile;
CascadeClassifier      mJavaDetector;
CameraBridgeViewBase   mOpenCvCameraView;
LinearLayout linearLayoutOne;
ImageView imageViewOne;
int counter = 0;

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

            } break;
            } break;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        if (!OpenCVLoader.initDebug()) {
            // Handle initialization error




        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(;



    public void onPause()
        if (mOpenCvCameraView != null)

    public void ...