Ask Your Question

When I use 'cvtColor' with JNI, Console says: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa4400000 in tid 3689

asked 2016-07-05 05:15:03 -0500

Yuruxuan gravatar image

Recently, I try using OpenCV at Android.I write a native method,and use it at java.

JNIEXPORT jintArray JNICALL Java_yu_myself_opencv_jni_OpenCVHelper_canny(
JNIEnv *env, jclass obj, jintArray buf, int w, int h){

jint *cbuf;
cbuf = env->GetIntArrayElements(buf, JNI_FALSE);
if (cbuf == NULL) {
    return 0;

Mat srcImage(h, w, CV_8UC4, (unsigned char *) cbuf);

Mat grayImage(srcImage.size(), srcImage.type());
cvtColor(srcImage, grayImage, COLOR_BGRA2GRAY);
jint* ptr = (jint*)grayImage.ptr(0);
int size = w * h;
jintArray result = env->NewIntArray(size);
env->SetIntArrayRegion(result, 0, size, ptr);
env->ReleaseIntArrayElements(buf, cbuf, 0);
return result;

But it crashed.I see the result of console:

07-05 17:32:16.721 3689-3689/yu.myself.opencv D/NDK/OpenCVHelper: 1
07-05 17:32:16.727 3689-3689/yu.myself.opencv D/NDK/OpenCVHelper: 2
07-05 17:32:16.728 3689-3689/yu.myself.opencv D/NDK/OpenCVHelper: 3
07-05 17:32:16.728 3689-3689/yu.myself.opencv D/NDK/OpenCVHelper: 4
07-05 17:32:16.739 3689-3689/yu.myself.opencv D/NDK/OpenCVHelper: 5
07-05 17:32:16.743 3689-3689/yu.myself.opencv A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa4400000 in tid 3689 (u.myself.opencv)

I don't know why there is such a problem. What should I do?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2016-07-06 07:30:52 -0500

Daniil Osokin gravatar image

The obtained grayscale image is of type CV_8UC1, so use Byte instead of Int infix in function names (e.g. NewByteArray, ...), or convert grayscale to BGRA once again and use it instead.

edit flag offensive delete link more


perfect!Because Android does not support one channel,I must convert twice:RGB->Gray->RGB.Then the gray image is display normal.

Yuruxuan gravatar imageYuruxuan ( 2016-07-06 08:31:20 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-07-05 05:15:03 -0500

Seen: 522 times

Last updated: Jul 06 '16