Issue
I want to use this library inside Android project with integrated OpenCV module.
Native function code:
extern "C" JNIEXPORT void JNICALL
Java_my_package_MyActivity_featherEdges(
JNIEnv *env,
jobject /* this */,
cv::Mat &I,
cv::Mat &p,
cv::Mat &q
) {
int r = 60;
double eps = 1e-6;
eps *= 255 * 255;
q = guidedFilter(I, p, r, eps);
}
Kotlin-side mask Bitmap to Mat converter:
fun Bitmap.maskToMat(): Mat {
val mat = Mat(this.width, this.height, CvType.CV_8UC1)
val obj = copy(Bitmap.Config.ARGB_8888, true)
Utils.bitmapToMat(obj, mat)
Imgproc.cvtColor(mat, mat, CvType.CV_8UC1)
Imgproc.cvtColor(mat, mat, Imgcodecs.IMREAD_GRAYSCALE)
return mat
}
Original image Bitmap to Mat converter:
fun Bitmap.objToMat(): Mat {
val mat = Mat(this.width, this.height, CvType.CV_8UC1)
val obj = copy(Bitmap.Config.ARGB_8888, true)
Utils.bitmapToMat(obj, mat)
return mat
}
I'm recieving this error:
terminating with uncaught exception of type cv::Exception: OpenCV(4.1.0) D:\BGErase\app\src\main\cpp\guidedfilter.cpp:191: error: (-215:Assertion failed) I.channels() == 1 || I.channels() == 3 in function 'GuidedFilter'
So how to properly convert Bitmap to Mat? Firstly, I wanted to pass Bitmaps into native functions, however this was very complicated.
Solution
I decided to just save Bitmaps on external storage, because bitmap conversion process is 30-40% slower, and then just pass absolute paths into native function:
extern "C" JNIEXPORT jint JNICALL
Java_my_package_name_MyActivity_featherEdges(
JNIEnv *env,
jobject /* this */,
jstring obj_path,
jstring mask_path,
jstring result_path) {
cv::Mat I = cv::imread(ConvertJString(env, obj_path), cv::IMREAD_COLOR);
cv::Mat p = cv::imread(ConvertJString(env, mask_path), cv::IMREAD_GRAYSCALE);
int r = 60;
double eps = 1e-6;
eps *= 255 * 255;
cv::Mat q = guidedFilter(I, p, r, eps);
cv::imwrite(ConvertJString(env, result_path), q);
return 0;
}
Answered By - Alexnadr Dorofeev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.