I have to do a project for Android-studio and one of the objectives is detecting the pupils of a person. I'm trying to use OpenCV. I know there are many issues about the detection of circles or eyes in this site with OpenCV, but every time I try to execute my code, the app crashes for a particular error that nobody reports, which is:
2021-03-30 21:44:08.178 19272-19500/com.android.unipi.camerawifiprova E/AndroidRuntime: FATAL EXCEPTION: Thread-7
Process: com.android.unipi.camerawifiprova, PID: 19272
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
I share my code in the methods onCameraViewStarted() and onCameraFrame().
@Override
public void onCameraViewStarted(int width, int height) {
dst = new Mat();
matGray = new Mat();
circles = new Mat();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
Core.transpose(mRgba,mRgbaT);
Core.flip(mRgbaT, dst,1);
Imgproc.resize(mRgbaT,mRgbaT, mRgba.size());
mRgba.release();
mRgbaT.release();
Bitmap resultBitmap;
resultBitmap = Bitmap.createBitmap(dst.cols(), dst.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dst, resultBitmap);
matGray = new Mat(resultBitmap.getWidth(), resultBitmap.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(resultBitmap, matGray);
int colorChannels = (matGray.channels() == 3) ? Imgproc.COLOR_BGR2GRAY
: ((matGray.channels() == 4) ? Imgproc.COLOR_BGRA2GRAY : 1);
Imgproc.cvtColor(matGray, matGray, colorChannels);
circles = new Mat(resultBitmap.getWidth(), resultBitmap.getHeight(), CvType.CV_8UC1);
Imgproc.GaussianBlur(matGray, matGray, new Size(9,9),2,2);
double dp = 1d;
double minDist = 20;
int minRadius = 0, maxRadius = 0;
double param1 = 105;
double param2 = 40;
Imgproc.HoughCircles(matGray, circles, Imgproc.CV_HOUGH_GRADIENT, dp, minDist,
param1, param2, minRadius, maxRadius);
int numCircles = (circles.rows() == 0) ? 0 : circles.cols();
for (int i = 0; i < numCircles; i++) {
double[] circlePar = circles.get(0,i);
int x = (int) circlePar[0], y = (int) circlePar[1]; // -> ArrayIndexOutOfBoundsException!!
Point center = new Point(x,y);
int radius = (int) circlePar[2];
Imgproc.circle(dst,center,radius,new Scalar(0,0,255),4);
}
matGray.release();
circles.release();
return dst;
}
I print the variable circlePar
: it should have three values (center coordinates and radius) instead I got only one [0.0]. Maybe the program cannot detect any circles. I try an easy task (detect one single coin on a desk), but the app still crashes for the above reason.