0

I'm developing an Android application to find the largest contour from an input image.

I found a working example in c++.

I tried to adapt it to my project and I'm trying to make a JNI function to return th results.

I'm stuck at returning the list of found contours from JNI to Java.

Here is my JNI code which isn't working for now

extern "C"
jobjectArray
Java_com_grimg_coffretpfe_Activities_CompareActivity_getQuadrilateral(
    JNIEnv *env,
    jobject /* this */,
    jlong grayscale,
    jlong output) {

Mat* mGray = (cv::Mat*) grayscale;
Mat* mOutput = (cv::Mat*) output;

Mat convexHull_mask(mGray->rows, mGray->cols, CV_8UC1);
convexHull_mask = Scalar(0);

vector<vector<Point>> contours;
findContours(*mGray, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
vector<int> indices(contours.size());
iota(indices.begin(), indices.end(), 0);

sort(indices.begin(), indices.end(), [&contours](int lhs, int rhs) {
    return contours[lhs].size() > contours[rhs].size();
});

/// Find the convex hull object
vector<vector<Point> >hull(1);
convexHull(Mat(contours[indices[0]]), hull[0], false);

vector<Vec4i> lines;
HoughLinesP(convexHull_mask, lines, 1, CV_PI / 200, 50, 50, 10);

if (lines.size() == 4) // we found the 4 sides
{
    vector<Vec3f> params(4);
    for (int l = 0; l < 4; l++)
    {
        params.push_back(calcParams(Point(lines[l][0], lines[l][1]), Point(lines[l][2], lines[l][3])));
    }

    vector<Point> corners;
    for (int i = 0; i < params.size(); i++)
    {
        for (int j = i; j < params.size(); j++) // j starts at i so we don't have duplicated points
        {
            Point intersec = findIntersection(params[i], params[j]);
            if ((intersec.x > 0) && (intersec.y > 0) && (intersec.x < mGray->cols) && (intersec.y < mGray->rows))
            {
                cout << "corner: " << intersec << endl;
                corners.push_back(intersec);
            }
        }
    }

    if (corners.size() == 4) // we have the 4 final corners
    {
        return(corners);
    }
}

return(vector<Point>());

}

The return values are wrong, how can I change this piece of code to return those contours?

Amine
  • 2,241
  • 2
  • 19
  • 41
  • You cannot simply return a C++ object to Java like this. Which data type do you expect these to be in Java? – zenzelezz Mar 01 '19 at 11:57
  • I expect a list of MatOfPoint. – Amine Mar 01 '19 at 13:01
  • The `jobjectArray` you want to return must be allocated with the JNI `NewObjectArray`API. See https://stackoverflow.com/questions/1610045/how-to-return-an-array-from-jni-to-java as a starting point. – manuell Mar 11 '19 at 17:04

0 Answers0