gpt4 book ai didi

java - OpenCV:有关 Sobel Dritevatives 的教程在 Android 上不起作用,出现 nullpointerException

转载 作者:行者123 更新时间:2023-12-02 04:52:53 25 4
gpt4 key购买 nike

我尝试将此 C/C++ 代码转换为适用于 Android 的 Java。 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)

最佳答案

替换

grad_x = null, grad_y = null, abs_grad_x = null, abs_grad_y = null, grad = null 

grad_x = new Mat(), grad_y = new Mat(), abs_grad_x = new Mat(), abs_grad_y = new Mat(), grad = new Mat();

关于java - OpenCV:有关 Sobel Dritevatives 的教程在 Android 上不起作用,出现 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29051021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com