1

I am trying to view the response returned from an opencv function knn. I've tried using the cout function like most examples show, but this doesnt work in the Android NDK.

The response should be 3 values, [1,2,3], and when I do response.cols, the answer is 3 so I know that the data is in there, but I cannot find the correct format to print out or inspect the data to see if the right response has been returned.

The value "response" returned from knn->findNearest is of format OutputArray.

I've tried the below methods, but they output weird data:

int K=3;
Mat response;
Mat dist;
knn->findNearest(testFeature, K, noArray(), response, dist);

int *responseToInt = (int*)response.data;
int nearestNeighbors[3];

for(int i = 0; i < response.cols; i++){
    nearestNeighbors[i] = responseToInt[i];
}

output:

nearestNeighbors[0] = 1077936128

nearestNeighbors[1] = 1077936128

nearestNeighbors [2] = 1077936128

int K=3;
Mat response;
Mat dist;

knn->findNearest(testFeature, K, noArray(), response, dist);

uchar* responseToChar = response.data;
char nearestNeighbors[3] = "";
for(int i = 0; i < response.cols; i++){
    nearestNeighbors[i] = responseToChar[i];
}

output:

nearestNeighbors[0] = \0

nearestNeighbors[1] = \0

nearestNeighbors[2] = @

Ber12345
  • 89
  • 1
  • 12
  • Possible duplicate of [Any simple or easy way to debug Android NDK code?](https://stackoverflow.com/questions/4629308/any-simple-or-easy-way-to-debug-android-ndk-code) – Richard Critten Dec 28 '17 at 13:07
  • @RichardCritten This is not a duplicate of that question & also it is not easy to find what type opencv Mat has using debugger. – Dmitrii Z. Dec 28 '17 at 13:15

1 Answers1

1

You can see what function returns in its documentation. For example take a look into findNearest

neighborResponses – Optional output values for corresponding neighbors. It is a single-precision floating-point matrix of * k size.

Note single-precision floating-point.

Which means result is Mat with data stored as float, which means you can access its data with

float* res = (float*)response.data;

Dmitrii Z.
  • 2,287
  • 3
  • 19
  • 29
  • I have tried this but it says "incompatible pointer types "float*" and "uchar*". However accessing as the uchar gives the strange data. – Ber12345 Dec 28 '17 at 13:16
  • 1
    I've edited answer, you just need to cast it to float * since you're using c++ – Dmitrii Z. Dec 28 '17 at 13:18