android - 在 Android 中使用 OpenCV 检测图像中的圆圈

标签 android opencv

我正在开发一个Android应用程序,其中我必须检测从图库浏览或从相机捕获的现有图像上的圆圈。浏览/捕获的图像将显示在 ImageView 上。顺便说一句,我正在使用 OpenCVAndroid 库 并且我正确地编译了它。

对我的 Android 应用程序有任何帮助吗,我已经阅读了 C、C++ 等来检测圆圈,但我无法理解它,因为它的 Android 语言不同。

谢谢。

更新

好吧...我就是这么用的。

            if (requestCode == 1) { //Take Photo from Android Camera..

            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        bitmapOptions);

                // bitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);



                String path = android.os.Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                f.delete();
                OutputStream outFile = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try {
                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                    outFile.flush();
                    outFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                                    /* convert bitmap to mat */
                Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
                        CvType.CV_8UC1);
                Mat grayMat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
                        CvType.CV_8UC1);

                Utils.bitmapToMat(bitmap, mat);

                  /* convert to grayscale */
                int colorChannels = (mat.channels() == 3) ? Imgproc.COLOR_BGR2GRAY
                        : ((mat.channels() == 4) ? Imgproc.COLOR_BGRA2GRAY : 1);

                Imgproc.cvtColor(mat, grayMat, colorChannels);

                /* reduce the noise so we avoid false circle detection */
                Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 2, 2);

                // accumulator value
                double dp = 1.2d;
                // minimum distance between the center coordinates of detected circles in pixels
                double minDist = 20;

               // min and max radii (set these values as you desire)
                int minRadius = 0, maxRadius = 0;

               // param1 = gradient value used to handle edge detection
              // param2 = Accumulator threshold value for the
              // cv2.CV_HOUGH_GRADIENT method.
              // The smaller the threshold is, the more circles will be
              // detected (including false circles).
              // The larger the threshold is, the more circles will
              // potentially be returned.
                double param1 = 70, param2 = 72;

              /* create a Mat object to store the circles detected */
                Mat circles = new Mat(bitmap.getWidth(),
                        bitmap.getHeight(), CvType.CV_8UC1);

              /* find the circle in the image */
                Imgproc.HoughCircles(grayMat, circles,
                        Imgproc.CV_HOUGH_GRADIENT, dp, minDist, param1,
                        param2, minRadius, maxRadius);

              /* get the number of circles detected */
                int numberOfCircles = (circles.rows() == 0) ? 0 : circles.cols();

              /* draw the circles found on the image */
                for (int i=0; i<numberOfCircles; i++) {


              /* get the circle details, circleCoordinates[0, 1, 2] = (x,y,r)
               * (x,y) are the coordinates of the circle's center*/
                                      double[] circleCoordinates = circles.get(0, 0);


                    int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1];

                    Point center = new Point(x, y);

                    int radius = (int) circleCoordinates[2];

/* circle's outline */
                    Core.circle(mat, center, radius, new Scalar(0,
                            255, 0), 4);

/* circle's center outline */
                    Core.rectangle(mat, new Point(x - 5, y - 5),
                            new Point(x + 5, y + 5),
                            new Scalar(0, 128, 255), -1);
                }

              /* convert back to bitmap */
                Utils.matToBitmap(mat, bitmap);
                viewImage.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

我使用上面的代码运行我的应用程序,我的 Android 手机在我从相机拍照后崩溃,并且在 logcat 中出现以下错误:

          02-10 06:54:15.773    8914-8914/com.example.cloud.circle E/AndroidRuntime﹕ FATAL EXCEPTION: main

java.lang.UnsatisfiedLinkError: n_Mat
        at org.opencv.core.Mat.n_Mat(Native Method)
        at org.opencv.core.Mat.<init>(Mat.java:477)
        at com.example.cloud.circle.Image.onActivityResult(Image.java:152)
        at android.app.Activity.dispatchActivityResult(Activity.java:3908)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
        at android.app.ActivityThread.access$2000(ActivityThread.java:117)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:130)
        at android.app.ActivityThread.main(ActivityThread.java:3687)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
        at dalvik.system.NativeStart.main(Native Method)

请帮助我。谢谢。

最佳答案

圆形检测的详细说明可以参见herehere (尽管不是在 Java 中)。

我在下面提供了一个用于圆形检测的示例代码 fragment ,该代码 fragment 是从我上面提供的 2 个链接中获取的。代码有注释,所以很容易理解。我假设图像位图 bitmap 已经包含您要分析的图像。

/* convert bitmap to mat */
Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
        CvType.CV_8UC1);
Mat grayMat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
        CvType.CV_8UC1);

Utils.bitmapToMat(bitmap, mat);

/* convert to grayscale */
int colorChannels = (mat.channels() == 3) ? Imgproc.COLOR_BGR2GRAY
        : ((mat.channels() == 4) ? Imgproc.COLOR_BGRA2GRAY : 1);

Imgproc.cvtColor(mat, grayMat, colorChannels);

/* reduce the noise so we avoid false circle detection */
Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 2, 2);

// accumulator value
double dp = 1.2d;
// minimum distance between the center coordinates of detected circles in pixels
double minDist = 100;

// min and max radii (set these values as you desire)
int minRadius = 0, maxRadius = 0;

// param1 = gradient value used to handle edge detection
// param2 = Accumulator threshold value for the
// cv2.CV_HOUGH_GRADIENT method.
// The smaller the threshold is, the more circles will be
// detected (including false circles).
// The larger the threshold is, the more circles will
// potentially be returned.
double param1 = 70, param2 = 72;

/* create a Mat object to store the circles detected */
Mat circles = new Mat(bitmap.getWidth(),
        bitmap.getHeight(), CvType.CV_8UC1);

/* find the circle in the image */
Imgproc.HoughCircles(grayMat, circles,
        Imgproc.CV_HOUGH_GRADIENT, dp, minDist, param1,
        param2, minRadius, maxRadius);

/* get the number of circles detected */
int numberOfCircles = (circles.rows() == 0) ? 0 : circles.cols();

/* draw the circles found on the image */
for (int i=0; i<numberOfCircles; i++) {


/* get the circle details, circleCoordinates[0, 1, 2] = (x,y,r)
 * (x,y) are the coordinates of the circle's center
 */
    double[] circleCoordinates = circles.get(0, i);


    int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1];

    Point center = new Point(x, y);

    int radius = (int) circleCoordinates[2];

    /* circle's outline */
    Core.circle(mat, center, radius, new Scalar(0,
            255, 0), 4);

    /* circle's center outline */
    Core.rectangle(mat, new Point(x - 5, y - 5),
            new Point(x + 5, y + 5),
            new Scalar(0, 128, 255), -1);
}

/* convert back to bitmap */
Utils.matToBitmap(mat, bitmap);

更新

为了防止出现UnsatisfiedLinkError,在使用OpenCV库的函数之前,请确保加载库文件,就像我在下面所做的那样:

if (!OpenCVLoader.initDebug()) {
    Log.e(TAG, "Cannot connect to OpenCV Manager");
}

关于android - 在 Android 中使用 OpenCV 检测图像中的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28401343/

相关文章:

opencv - OPENCV(不受支持的格式或组合格式)SURF

java - 在获取 Android 屏幕 View 后比较位图 - 比较方法不起作用

Android Studio 2.2不打包第三方库到apk

android - 在 Dropbox 上上传文件并检索上传的文件 URL : Android

java - Android - Opencv 增加 FPS

python - cv2、scipy.misc 和 skimage 之间的区别

python - Opencv 提取最大感兴趣区域

python - 如何使用 mask 更改图像的颜色?

android - 一个简单的绘图示例

android - BroadcastReceiver ClassNotFound 异常