Reputation: 1610
I am using OpenCV for adaptiveThreshold
. My code for image processing using OpenCV is like this:
imageMat=new Mat();
Utils.bitmapToMat(bmp, imageMat);
Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);
Imgproc.adaptiveThreshold(imageMat, imageMat, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 5, 4);
But I am getting an error while declaring Mat
:
No implementation found for long org.opencv.core.Mat.n_Mat() (tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__)
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:24)
at com.example.pial_pc.instantbookreview.ImageCapture$3.onPictureTaken(ImageCapture.java:105)
As I'm new to OpenCV, I am not clearly understanding the error. The entire Java class where this code belongs is here.
What should I do to eliminate the error?
Upvotes: 57
Views: 54301
Reputation: 58
En mi caso, use este código en onCreate para Kotlin.
if (OpenCVLoader.initLocal()) {
println("OpenCV loaded successfully")
} else {
println("OpenCV initialization failed!")
return
}
Upvotes: 0
Reputation: 23
As of the 2020 to 25 update, you only need to add the following code in your activity's onResume
or onCreate
method before calling any OpenCV-related functions or methods:
OpenCVLoader.initLocal();
// or
OpenCVLoader.initDebug();
And boom, this will resolve the error.
Upvotes: 1
Reputation: 71
This worked for me:
Make sure when linking the module dependency copy the file in native/libs opencv to a jniLibs directory in your project then set the following in your build.gradle(app) the build.
sourceSets {
main {
jni {
srcDirs 'src/main/jni', 'src/main/jniLibs'
}
}
}
After that add this to your main activity
OpenCVLoader.initDebug()
Upvotes: 2
Reputation: 591
Use this to resolve error.
public class MainActivity : Activity
{
Mat m;
Mat grayM;
ImageView imageView;
public MainActivity()
{
if (!OpenCVLoader.InitDebug())
{
System.Console.WriteLine("GG");
}
}
}
Upvotes: 3
Reputation: 1541
Initialize openCV library in your activity add this code before onCreate() method
static {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error
}
}
add this lib in your project : https://github.com/hschott/Camdroid
Upvotes: 21
Reputation: 187
I put this line on onCreate method and make sure openCvManager is installed. This worked fine for me.
OpenCVLoader.initDebug();
Upvotes: 16
Reputation: 396
Maybe you are forgetting to include the openCV library.
Include
static {
System.loadLibrary("opencv_java");
}
for OpenCV version 3 you should instead add:
static {
System.loadLibrary("opencv_java3");
}
Good Luck :)
Upvotes: 8
Reputation: 1610
I have successfully eliminate the error and my app does not crash while executing this line imageMat=new Mat();
The reason for the error is that Android calls the "onCreate" method before loading the OpenCV4Android library.So i have used Async Initialization of OpenCV using OpenCVManager. I have created BaseLoaderCallback before onCreate method. And inside that i have declared new Mat() like this:
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i("OpenCV", "OpenCV loaded successfully");
imageMat=new Mat();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
Then in onResume() i have checked if OpenCV library have been loaded and initialized from within current application package or not. The codes are as follows:
public void onResume()
{
super.onResume();
if (!OpenCVLoader.initDebug()) {
Log.d("OpenCV", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
} else {
Log.d("OpenCV", "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
And my error is gone. My full updated java class is here.
Reference links :
Upvotes: 77