代码未使用android ndk显示灰度图像

时间:2014-07-25 22:29:13

标签: opencv android-ndk java-native-interface

我的代码没有将图像显示为灰度,因为我在eclipse中使用android ndk

jni part :

以下为亮度工作的部分

   jint* _in = env->GetIntArrayElements(in, 0);
   jint* _out = env->GetIntArrayElements(out, 0);

   Mat mSrc(height, width, CV_8UC4, (unsigned char*)_in);
   Mat bgra(height, width, CV_8UC4, (unsigned char*)_out);
   vector<Mat> sChannels;
   split(mSrc, sChannels);

        for(int i=0; i<sChannels.size(); i++)
        {
            Mat channel = sChannels[i];
            equalizeHist(channel, channel);
        }
        merge(sChannels, bgra);
   env->ReleaseIntArrayElements(in, _in, 0);
   env->ReleaseIntArrayElements(out, _out, 0);

   jint retVal;
   int ret = 1;
   retVal = jint(retVal);
   return retVal;

但是,当我将上面的代码更改为将图像转换为灰度时,它没有显示输出

       jint* _in = env->GetIntArrayElements(in, 0);
       jint* _out = env->GetIntArrayElements(out, 0);

       Mat mSrc(height, width, CV_8UC4, (unsigned char*)_in);
       Mat bgra(height, width, CV_8UC4, (unsigned char*)_out);
       cvtColor(mSrc , bgra , CV_BGRA2GRAY);
       env->ReleaseIntArrayElements(in, _in, 0);
       env->ReleaseIntArrayElements(out, _out, 0);

       jint retVal;
       int ret = 1;
       retVal = jint(retVal);
       return retVal;

1 个答案:

答案 0 :(得分:2)

CV_BGRA2GRAY期望输出 CV_8UC1 。要生成BGRA输出,您可以使用临时图像和2次转换:

   Mat mSrc(height, width, CV_8UC4, (unsigned char*)_in);
   Mat gray(height, width, CV_8UC1);
   Mat bgra(height, width, CV_8UC4, (unsigned char*)_out);
   cvtColor(mSrc , gray , CV_BGRA2GRAY);
   cvtColor(gray , bgra , CV_GRAY2BGRA);