如何通过Android NDK(C ++)将图像放入openCV中的MAT中 [英] How to put an image into a MAT in openCV, through the android NDK (c++)

查看:48
本文介绍了如何通过Android NDK(C ++)将图像放入openCV中的MAT中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Android NDK(使用c ++)使用 OpenCv 我想将图像保存为MAT格式,然后将其显示在我的android应用程序中.

I'm using OpenCv through the Android NDK (using c++) I would like to save an image into MAT format and then display it on my android application.

我已将图像保存在资产中. imread()在NKD中不起作用,因为它找不到图像的文件路径,但是,我可以使用AssetManager加载资产,并且它可以完美地找到路径.此方法将数据保存到char *缓冲区中.

I have saved the image in assets. imread() does not work in the NKD because it cannot find the file path to the image, however, I can use AssetManager to load an asset and it finds the path perfectly. This method saves the data into a char* buffer.

我该如何使用类似于 imread()的方式将图像保存到MAT中,或将char *缓冲区数据转换为MAT以便在屏幕上显示它,然后再将其显示在屏幕上与其他 openCV 函数一起操作?

How can I, either use something similar to imread() to save the image into a MAT, or convert the char* buffer data into a MAT in order to display it on screen and later on manipulate with other openCV functions?

推荐答案

使用代码段

extern "C"
JNIEXPORT int JNICALL
Java_com_example_opencv_test_NativeLib_checkNativeImage(JNIEnv *env,
                                                    jclass type,
                                                    jobject jBitmap,
                                                    jint width,
                                                    jint height) {

void *pPixelData = nullptr;
if (AndroidBitmap_lockPixels(env, jBitmap, &pPixelData) ==
    ANDROID_BITMAP_RESULT_SUCCESS) {

    // Android bitmap (Bitmap.Config.ARGB_8888) to Mat
    // Android and OpenCV have different color channels order, just swap it
    // You need cppFlags "-std=c++14" for this code
    // See [opencv_src]\modules\java\generator\src\cpp\utils.cpp for details about PremultiplyAlpha
    // Use https://docs.opencv.org/java/2.4.2/org/opencv/android/Utils.html for Mat in Java

    cv::Mat temp = cv::Mat(cvSize(width, height), CV_8UC4, pPixelData); // 4 channels
    cv::Mat img;
    cv::cvtColor(temp, img, CV_BGRA2RGBA);

    // ... use Mat Image


    // back to Android Bitmap
    cvtColor(img, temp, CV_RGBA2BGRA); // Swap colors back
    memcpy(pPixelData, temp.data, static_cast<size_t>(width * height * 4));


    AndroidBitmap_unlockPixels(env, jBitmap);

    return EXIT_SUCCESS;
}

return EXIT_FAILURE;

}

这篇关于如何通过Android NDK(C ++)将图像放入openCV中的MAT中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆