【问题标题】:Using __android_log_print to print the contents of an Opencv Mat, Android NDK使用 __android_log_print 打印 Opencv Mat、Android NDK 的内容
【发布时间】:2018-06-05 18:21:02
【问题描述】:

我有以下代码,我想用它来将 trainingLables Mat 的内容打印到 android 控制台:

#include <android/log.h>

JNIEXPORT jintArray JNICALL  Java_com_example_user_activity_MainActivity_generateAssets(JNIEnv* env, jobject thiz, jobject assetManager) {

    .....
    .....

    Mat_<int> trainingLabels(1,10);
    trainingLabels << 0, 0, 0, 1, 1, 1, 0, 1, 1, 1;

    __android_log_print(ANDROID_LOG_ERROR, "TESTING", "%d %d", trainingLabels.???????);

    ......
    ......


}

我有两个问题:

1) 我用什么方法打印来自 trainingLabels 的一行数据? (我在代码中用什么代替问号?)

2) 如何在 Android LogCat 中搜索来自 __android_log_print 的内容?

【问题讨论】:

    标签: android c++ opencv android-ndk


    【解决方案1】:

    您可以简单地预先准备一个包含您的数据的字符串

    char matRow[100] = ""; // You can calculate how much memory u need, but for debug prints just put a big enough number
    
    Mat_<int> trainingLabels(1,10);
    trainingLabels << 0, 0, 0, 1, 1, 1, 0, 1, 1, 1;
    
    for (int i = 0; i < 10; i++) {
        sprintf(matRow + strlen(matRow), "%d ", trainingLabels.at<int>(i)); // You can use data from row you need by accessing trainingLabels.row(...).data
    }
    
    android_log_print(ANDROID_LOG_ERROR, "SEARCH FOR THIS TAG", "%s", matRow);
    

    然后在您的 logcat 中查找标记,在我的示例中为“搜索此标记”

    关于它如何存储在 Mat 对象中的其他问题 - 因为您使用 int 类型创建了 Mat - Mat.data 是 int 数组,您可以通过类似的方式轻松查看:

    char matRow[100] = ""; // You can calculate how much memory u need, but for debug prints just put a big enough number
    
    Mat_<int> trainingLabels(1,10);
    trainingLabels << 0, 0, 0, 1, 1, 1, 0, 1, 1, 1;
    
    int * data = (int*)trainingLabels.data;
    
    for (int i = 0; i < 10; i++) {
        sprintf(matRow + strlen(matRow), "%d ", data[i]); // You can use data from row you need by accessing trainingLabels.row(...).data
    }
    android_log_print(ANDROID_LOG_ERROR, "SEARCH FOR THIS TAG", "%s", matRow);
    

    【讨论】:

    • 我已经尝试过了,使用 trainingLabels.data[i],它给出了 10 个 0,我尝试了 trainingLabels.row(0).data[i],它也给出了 10 个 0。还有其他方法可以访问 trainingLabels 中的数据吗?
    • 另外,如果你知道答案,你能解释一下 Mat 中的数据实际存储在哪里吗?我检查了 android 调试器上的数据元素,其中包含一堆 uchar* 值,如 \x05、\0、\0、\x02。我假设这是我的数据转换成其他格式?有没有办法通过检查数据元素或打印出来来读取我放入 Mat (0,0,0,1,1,1,0,1,1,1) 中的原始 int 值? - 提前致谢。
    • 嘿@Ber12345 我已经更新了我的答案,所以它可以打印正确的数据。
    • 还添加了如何使用 Mat 对象的数据字段的说明
    • 不客气。 char 数组中可能有一些垃圾。我已经编辑了答案(字符数组用“”初始化)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2013-05-13
    • 2014-11-26
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    相关资源
    最近更新 更多