结果显示图像上的衬里

时间:2014-08-23 20:57:05

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

我正在使用opencv和android ndk,下面是我的jni代码:

void Vignete(Mat& img1, Mat& img2, Mat& out)
{

    resize(img1, img1, img2.size());
    img1.convertTo(img1,CV_32FC4,1.0/255.0);
    img2.convertTo(img2, CV_32FC4, 1.0/255.0);
    vector<Mat> ch;
    Mat I1;
    split(img2,ch);
    Mat alpha1 = ch[3];
    Mat alpha = alpha1.clone();
    Mat ch0 = ch[0];
    Mat ch1 = ch[1];
    Mat ch_2 = ch[2];
    Mat ch_3 = ch[3];
    cv::multiply(alpha,ch0,ch0);
    cv::multiply(alpha,ch1,ch1);
    cv::multiply(alpha,ch_2,ch_2);
    cv::multiply(alpha,ch_3,ch_3);
    vector<Mat> newVec;
    newVec.push_back(ch0);
    newVec.push_back(ch1);
    newVec.push_back(ch_2);
    newVec.push_back(ch_3);
    merge(newVec, I1);
    vector<Mat> ch2;
    Mat I2;
    split(img2,ch2);
    Mat ch_0 = ch2[0];
    Mat ch_1 = ch2[1];
    Mat ch_21 = ch2[2];
    Mat ch_31 = ch2[3];
    cv::multiply(1.0-alpha,ch_0,ch_0);
    cv::multiply(1.0-alpha,ch_1,ch_1);
    cv::multiply(1.0-alpha,ch_21,ch_21);
    cv::multiply(1.0-alpha,ch_31,ch_31);
    vector<Mat> newVec1;
    newVec1.push_back(ch_0);
    newVec1.push_back(ch_1);
    newVec1.push_back(ch_21);
    newVec1.push_back(ch_31);
    merge(newVec1, I2);
    Mat result = I1+I2;
    result.convertTo(out, CV_8UC4, 255);
    }

以下是我的jni调用方法:

JNIEXPORT jint JNICALL Java_org_opencv_samples_NativeActivity_CvNativeActivity_Vig(
        JNIEnv* env, jobject, jint width, jint height, jint i, jint j, jintArray in,jintArray inn,
        jintArray out) {
    jint* _in = env->GetIntArrayElements(in, 0);
    jint* _inn = env->GetIntArrayElements(inn, 0);
    jint* _out = env->GetIntArrayElements(out, 0);

    Mat mSrc(height, width, CV_8UC4, (unsigned char*) _in);
    Mat nSrc(i, j, CV_8UC4, (unsigned char*) _inn);
    Mat bgra(height, width, CV_8UC4, (unsigned char*) _out);
    Vite(mSrc,nSrc, bgra);
    jint retVal;
    int ret = 1;
    retVal = jint(retVal);
    return retVal;
    }

结果:

enter image description here

My java calling,其中img1为image in snapshot,img2 image 2 is vignette of 4 channel为输入输出

InputStream is , Vign;
is = this.getResources().openRawResource(R.drawable.me);
final Bitmap bmInImg = BitmapFactory.decodeStream(is);
Vign = this.getResources().openRawResource(R.drawable.vig2);
final Bitmap bmInImg2 = BitmapFactory.decodeStream(Vign);

mPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
nPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
vPhotoIntArray = new int[bmInImg2.getWidth() * bmInImg2.getHeight()];
imageview_1.setImageBitmap(bmInImg);

bmInImg.getPixels(mPhotoIntArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());
bmInImg2.getPixels(vPhotoIntArray, 0, bmInImg2.getWidth(), 0, 0, bmInImg2.getWidth(), bmInImg2.getHeight());

mCannyOutArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
final Bitmap bmOutImg = Bitmap.createBitmap(bmInImg.getWidth(), bmInImg.getHeight(), Config.ARGB_8888);  
bmOutImg.setPixels(mCannyOutArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());

Vig(bmInImg.getHeight(),bmInImg.getWidth(),bmInImg2.getHeight(),bmInImg2.getWidth(), mPhotoIntArray,vPhotoIntArray, mCannyOutArray); 
Bitmap bmOutImg = Bitmap.createBitmap(bmInImg.getWidth(), bmInImg.getHeight(), Config.ARGB_8888);  
bmOutImg.setPixels(mCannyOutArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());   
imageview_2.setImageBitmap(bmOutImg);

IMG1:

enter image description here

img2:

enter image description here

我正在使用eclipse与opencv android版本2.4.8进行原生环境。

1 个答案:

答案 0 :(得分:1)

检查两个图像是否大小相同,如果没有,则先resize两个图像然后执行操作,您需要在Java端执行resize操作而不是jni操作1}}方。结果图像应该是输入图像的大小,但是第二图像尺寸也应该是输入图像尺寸,在这种情况下所有图像尺寸都很重要。