Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV Matrix get wrong values JNI Java

I'm trying to pass OpenCV Matrix to a cpp file but the data I read from the Matix in the cpp is not right.

I don't know what I'm missing here, Maybe something with the pointer.

This is part of my cpp file:

 jintArray
Java_qc_yotm_mes_com_yo_1qc_MainActivity_stringFromJNI(JNIEnv *env,jclass cls,jint NUMBER_OF_HALF_LINES, jint HALF_LINE_LENGTH, jlong inMat) {

    cv::Mat* GraySubMat =  (cv::Mat*)inMat;
    unsigned char * MatData = GraySubMat->data;

    jdouble SumSpikesHeight = 0;
    jint ListOfPeaks = 0;

    jintArray ListSpikesCount = env->NewIntArray(2 * NUMBER_OF_HALF_LINES);
    jintArray ListSpikesCountHeight = env->NewIntArray(2 * NUMBER_OF_HALF_LINES);

    jint outarray[2 * NUMBER_OF_HALF_LINES];


    for(int line_number = 0 ; line_number <  2 * NUMBER_OF_HALF_LINES ; line_number++) {
        //Find Peaks on every 5 pixels
        for (int i = 2; i < (HALF_LINE_LENGTH * 2) - 3; i++) {

            if ((MatData[(line_number *HALF_LINE_LENGTH * 2)+i] > MatData[(line_number *HALF_LINE_LENGTH * 2)+i - 1]) && (MatData[(line_number *HALF_LINE_LENGTH * 2)+i] > MatData[(line_number *HALF_LINE_LENGTH * 2)+i - 2]) &&
                (MatData[(line_number *HALF_LINE_LENGTH * 2)+i] > MatData[(line_number *HALF_LINE_LENGTH * 2)+i + 1]) && (MatData[(line_number *HALF_LINE_LENGTH * 2)+i] > MatData[(line_number *HALF_LINE_LENGTH * 2)+i + 2]) &&
                (MatData[(line_number *HALF_LINE_LENGTH * 2)+i] > 0) ) {
                ListOfPeaks++;
                SumSpikesHeight += MatData[(line_number *HALF_LINE_LENGTH * 2)+i];

                i += 3;
            }
        }

        outarray[line_number] = ListOfPeaks*SumSpikesHeight;

        SumSpikesHeight = 0;
        ListOfPeaks=0;
    }
    env->SetIntArrayRegion(ListSpikesCountHeight, 0, 2 * NUMBER_OF_HALF_LINES, outarray);
    return ListSpikesCountHeight;
}

This is the Java call:

 int[] spikecountheight = stringFromJNI(NUMBER_OF_HALF_LINES, HALF_LINE_LENGTH, GraySubMat.getNativeObjAddr());