2

I have the following code, which I would like to use to print the contents of the trainingLables Mat to the android console:

#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.???????);

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


}

I have 2 questions:

1) What method do I use to print one row of the data from trainingLabels? (What do I put in place of the question marks in my code?)

2) How do I search for content from __android_log_print inside Android LogCat?

Ber12345
  • 89
  • 1
  • 12

1 Answers1

3

You can simply prepare a string with your your data beforehand

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);

And in your logcat look for tag which is "SEARCH FOR THIS TAG" in my example

As for your other question regarding how it is stored inside Mat object - since you created Mat with int type - Mat.data is int array which you can easily see with something like that:

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);
Dmitrii Z.
  • 2,287
  • 3
  • 19
  • 29
  • I've tried this, using trainingLabels.data[i], which gives 10 0's and I tried trainingLabels.row(0).data[i] which also gives 10 0's. is there another way that I can access the data in trainingLabels? – Ber12345 Dec 24 '17 at 11:17
  • Also if you know the answer, would it be possible for you to explain where the data in a Mat is actually stored? I have inspected the data element on the android debugger, which contains a bunch of uchar* values like \x05, \0, \0, \x02. I'm assuming that this is my data converted into some other format? Is there a way to read the raw int values that I put inside the Mat (0,0,0,1,1,1,0,1,1,1) by either inspecting the data element, or printing them out? - Thanks in advance. – Ber12345 Dec 24 '17 at 12:21
  • 1
    Hey @Ber12345 I've updated my answer so it prints correct data. – Dmitrii Z. Dec 24 '17 at 16:28
  • 2
    Also added explanation to how to use data field of your Mat object – Dmitrii Z. Dec 24 '17 at 16:33
  • Thank you so much! It works. However it does also print a bunch of strange characters before it : J��=شM.J��=شM.�3�7z Any idea what this is? – Ber12345 Dec 24 '17 at 21:13
  • 1
    You're welcome. There was probably some garbage in char array. Ive edited answer (char array is initialized with "") – Dmitrii Z. Dec 24 '17 at 23:10
  • How would I go about printing the data if the Mat was in a format other than int? I have just posted a question where I am trying to access the data inside an OutputArray: https://stackoverflow.com/questions/47997363/accessing-a-returned-value-from-opencv-function-c-ndk – Ber12345 Dec 27 '17 at 20:24