この C/C++ コードを Java for Android に変換しようとしました。http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html#sobel-derivatives 残念ながら、動作しません。
私のコード:`public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
if (status == LoaderCallbackInterface.SUCCESS ) {
// now we can call opencv code !
try {
krawedz();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("moje", "brak obrazka");
e.printStackTrace();
}
} else {
super.onManagerConnected(status);
}
}
};
@Override
public void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
}
public void krawedz() throws IOException{
Mat src, src_gray = null;
Mat grad_x = null, grad_y = null;
Mat abs_grad_x = null, abs_grad_y = null;
Mat grad = null;
int skala = 1;
int delta = 0;
int depth = 3; //CV16_S
//załaduj obrazek
src = Utils.loadResource(this, R.drawable.obraz);
if(src.empty()){
Log.e("moje", "brak obrazka");
}
//na szaro
Imgproc.GaussianBlur(src, src_gray, new org.opencv.core.Size(3,3), 0, 0);
Imgproc.cvtColor(src, src_gray, 7 ); //7=CV_RGB2GRAY
//Gradient x
//Imgproc.Scharr(src_gray, grad_x, depth, 1, 0, skala, delta, Imgproc.BORDER_DEFAULT);
Imgproc.Sobel(src_gray, grad_x, depth, 1, 0, 3, skala, delta, Imgproc.BORDER_DEFAULT);
Core.convertScaleAbs(grad_x, abs_grad_x);
//Gradient Y
//Imgproc.Scharr(src_gray, grad_y, depth, 0, 1, skala, delta, Imgproc.BORDER_DEFAULT);
Imgproc.Sobel(src_gray, grad_y, depth, 0, 1, 3, skala, delta, Imgproc.BORDER_DEFAULT);
Core.convertScaleAbs(grad_y, abs_grad_y);
Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
// zamiana macierzy na bitmape:
Bitmap bm = Bitmap.createBitmap(grad.cols(), grad.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(grad, bm);
// rysowanie bitmapy
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bm);
}
}`
このコードについて LogCat は次のように考えています。
03-14 16:45:52.944: E/AndroidRuntime(25349): FATAL EXCEPTION: main
03-14 16:45:52.944: E/AndroidRuntime(25349): Process: com.example.wykrywanie_krawedzi, PID: 25349
03-14 16:45:52.944: E/AndroidRuntime(25349): java.lang.NullPointerException
03-14 16:45:52.944: E/AndroidRuntime(25349): at org.opencv.imgproc.Imgproc.GaussianBlur(Imgproc.java:533)
03-14 16:45:52.944: E/AndroidRuntime(25349): at com.example.wykrywanie_krawedzi.MainActivity.krawedz(MainActivity.java:73)
03-14 16:45:52.944: E/AndroidRuntime(25349): at com.example.wykrywanie_krawedzi.MainActivity$1.onManagerConnected(MainActivity.java:38)
03-14 16:45:52.944: E/AndroidRuntime(25349): at org.opencv.android.AsyncServiceHelper$1.onServiceConnected(AsyncServiceHelper.java:318)
03-14 16:45:52.944: E/AndroidRuntime(25349): at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1110)
03-14 16:45:52.944: E/AndroidRuntime(25349): at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1127)
03-14 16:45:52.944: E/AndroidRuntime(25349): at android.os.Handler.handleCallback(Handler.java:733)
03-14 16:45:52.944: E/AndroidRuntime(25349): at android.os.Handler.dispatchMessage(Handler.java:95)
03-14 16:45:52.944: E/AndroidRuntime(25349): at android.os.Looper.loop(Looper.java:136)
03-14 16:45:52.944: E/AndroidRuntime(25349): at android.app.ActivityThread.main(ActivityThread.java:5086)
03-14 16:45:52.944: E/AndroidRuntime(25349): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:45:52.944: E/AndroidRuntime(25349): at java.lang.reflect.Method.invoke(Method.java:515)
03-14 16:45:52.944: E/AndroidRuntime(25349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-14 16:45:52.944: E/AndroidRuntime(25349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-14 16:45:52.944: E/AndroidRuntime(25349): at dalvik.system.NativeStart.main(Native Method)