I have a denoising algorithm that removes noise from images. I want to implement it in NDK.
I pass a 2D array containing the pixels of the image. process it and return another 2D array containing the denoised image.
I've tried the following code, but I still don't know how to wrap a 1D array inside a JObjectArray to return it as 2D array.
Here is my code, I'm not sure about the allocations and deallocation, and I fear about OutOfMemory problems when passing 3000*3000 image. hope you can help.:
JNIEXPORT jobjectArray JNICALL Java_com_example_Denoise (JNIEnv *env, jobject obj, jobjectArray arr,jint w, jint h){
double *img1 = new double[w*h];
int i, j, k;
///////////////////READING THE INPUT ARRAY////////////////////////
jsize dim1 = env->GetArrayLength(arr);
for (i = 0; i < dim1; i++) {
jdoubleArray oneDim= (jdoubleArray)env->GetObjectArrayElement(arr, i);
jdouble *element= env->GetDoubleArrayElements(oneDim, 0);
int dim2 = env->GetArrayLength(oneDim);
for(j=0; j<dim2; j++){
img1[i*dim2 +j] = element[j];
}
}
////////////////////Processing/////////////////////////////////////
double *y1 = new double[w];
double *y2 = new double[w];
double *img2 = new double[w * h];
for ( j = 0; j < h; j++) {
for ( i = 0; i < w; ++i) {
y1[i] = ///some basic processing using img1;
y2[i] = ///some other basic processing using img1;
}
for (int i = 0; i < w; i++) {
img2[j*w+i] = y1[i] + y2[i];
}
}
y1 = new double[h];
y2 = new double[h];
double *img3 = new double[w * h];
for ( i = 0; i < w; i++) {
for ( j = 0; j < h; ++j) {
y1[j] = ///some basic processing using img2;
y2[j] = ///some other basic processing using img2;
}
for ( j = 0; j < w; j++) {
img3[i*w+j] = y1[j] + y2[j];
}
}
/////////////////////////////Prepare the JObjectArray to return/////////////////////////////////////
//Here I don't know how can I put img3[] inside a 2D JobjectArray like array[w][h] and return it to android
//to receive it as array[w][h]. But I tried this:
jclass ArrayClass = env->FindClass( "[D");
jobjectArray outer = env->NewObjectArray( w, ArrayClass, 0);
for (i = 0; i < w; i++) {
jdoubleArray inner = env->NewDoubleArray( h);
env->SetDoubleArrayRegion( inner, 0, h, img3[i]);
env->SetObjectArrayElement( outer, i, inner);
env->DeleteLocalRef( inner);
}
return outer;
}
Aucun commentaire:
Enregistrer un commentaire