我有这个程序,试图找到图像中的圆圈。我现在正在尝试一段时间,但尚未取得成果。
我正在调试程序,我发现问题出在这一行: Mat mat = new Mat();
当我运行该程序时,它很遗憾地停止了。有谁知道为什么以及如何解决它?
MainActivity.java
package com.examp.filewithpic;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap image = BitmapFactory.decodeResource(this.getResources(),R.drawable.aaa);
// Drawable drawable = this.getResources().getDrawable(R.drawable.aaa);
ImageView image1= (ImageView) findViewById(R.id.iv1);
ImageView image2= (ImageView) findViewById(R.id.iv2);
image1.setImageBitmap(image);
int w = image.getWidth();
int h = image.getHeight();
Toast.makeText(this,"height:"+h+" width:"+w,Toast.LENGTH_LONG).show();
/**************/
/* Mat mat = new Mat(image.getWidth(), image.getHeight(),
CvType.CV_8U);
*/
Mat mat = new Mat();
mat.create(new Size(image.getWidth(), image.getHeight()), CvType.CV_8U);
Mat grayMat = new Mat();
grayMat.create(new Size(image.getWidth(), image.getHeight()), CvType.CV_8U);
Utils.bitmapToMat(image, mat,false);
/* 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 = 12;
// 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(image.getWidth(),
image.getHeight(), CvType.CV_8U);
/* 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, 125), 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, image,false);
}
}
XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.examp.filewithpic.MainActivity" >
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="60dp" />
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="58dp" />
</LinearLayout>
日志
enter code here
06-15 00:11:51.940: D/dalvikvm(1605): GC_FOR_ALLOC freed 75K, 27% free 2728K/3728K, paused 10ms, total 10ms
06-15 00:11:51.940: I/dalvikvm-heap(1605): Grow heap (frag case) to 3.660MB for 1012012-byte allocation
06-15 00:11:51.950: D/dalvikvm(1605): GC_FOR_ALLOC freed <1K, 22% free 3716K/4720K, paused 10ms, total 10ms
06-15 00:11:51.970: W/dalvikvm(1605): No implementation found for native Lorg/opencv/core/Mat;.n_Mat:()J
06-15 00:11:51.970: D/AndroidRuntime(1605): Shutting down VM
06-15 00:11:51.970: W/dalvikvm(1605): threadid=1: thread exiting with uncaught exception (group=0x95df5b20)
06-15 00:11:51.970: I/Process(1605): Sending signal. PID: 1605 SIG: 9
06-15 00:11:51.970: D/AndroidRuntime(1605): procName from cmdline: com.example.vvvv
06-15 00:11:51.970: E/AndroidRuntime(1605): in writeCrashedAppName, pkgName :com.example.vvvv
06-15 00:11:51.970: D/AndroidRuntime(1605): file written successfully with content: com.example.vvvv StringBuffer : ;com.example.vvvv
06-15 00:11:51.970: E/AndroidRuntime(1605): FATAL EXCEPTION: main
06-15 00:11:51.970: E/AndroidRuntime(1605): Process: com.example.vvvv, PID: 1605
06-15 00:11:51.970: E/AndroidRuntime(1605): java.lang.UnsatisfiedLinkError: Native method not found: org.opencv.core.Mat.n_Mat:()J
06-15 00:11:51.970: E/AndroidRuntime(1605): at org.opencv.core.Mat.n_Mat(Native Method)
06-15 00:11:51.970: E/AndroidRuntime(1605): at org.opencv.core.Mat.<init>(Mat.java:447)
06-15 00:11:51.970: E/AndroidRuntime(1605): at com.example.vvvv.MainActivity$1.onClick(MainActivity.java:37)
06-15 00:11:51.970: E/AndroidRuntime(1605): at android.view.View.performClick(View.java:4443)
06-15 00:11:51.970: E/AndroidRuntime(1605): at android.view.View$PerformClick.run(View.java:18433)
06-15 00:11:51.970: E/AndroidRuntime(1605): at android.os.Handler.handleCallback(Handler.java:733)
06-15 00:11:51.970: E/AndroidRuntime(1605): at android.os.Handler.dispatchMessage(Handler.java:95)
06-15 00:11:51.970: E/AndroidRuntime(1605): at android.os.Looper.loop(Looper.java:136)
06-15 00:11:51.970: E/AndroidRuntime(1605): at android.app.ActivityThread.main(ActivityThread.java:5021)
06-15 00:11:51.970: E/AndroidRuntime(1605): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 00:11:51.970: E/AndroidRuntime(1605): at java.lang.reflect.Method.invoke(Method.java:515)
06-15 00:11:51.970: E/AndroidRuntime(1605): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
06-15 00:11:51.970: E/AndroidRuntime(1605): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
06-15 00:11:51.970: E/AndroidRuntime(1605): at dalvik.system.NativeStart.main(Native Method)