Mat hierarchy in Java vs vector<cv::Vec4i> hierarchy in C++

asked 2015-12-05 06:22:55 -0600

JustMyName gravatar image

I have a function in C++ as below.

void trackFilteredObject(int &x, int &y, cv::Mat threshold, cv::Mat cameraFeed){

    cv::Mat temp;
        threshold.copyTo(temp);
        vector< vector<cv::Point> > contours;
        vector<cv::Vec4i> hierarchy;
        findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
        double refArea = 0;
        bool objectFound = false;

        if (hierarchy.size() > 0) {
            vector< vector<cv::Point> > largestContourVec;
            largestContourVec.push_back(contours.at(contours.size() - 1));
            Eco_Rect = boundingRect(largestContourVec.at(0));
            int numObjects = hierarchy.size();
            if (numObjects<100){
                for (int index = 0; index >= 0; index = hierarchy[index][0]){
                    cv::Moments moment = moments((cv::Mat)contours[index]);
                    double area = moment.m00;
                    if (area>0 && area>refArea){
                        x = moment.m10 / area;
                        y = moment.m01 / area;
                        objectFound = true;
                        refArea = area;
                    }
                }

                if (objectFound == true){
                    cv::circle(cameraFeed, cv::Point(x, y), 10, cv::Scalar(0, 255, 0), 2);
                    line(cameraFeed, cv::Point(roi_rect.width / 2, 0), cv::Point(roi_rect.width / 2, roi_rect.height), 2, 2);
                    line(cameraFeed, cv::Point(0, roi_rect.height / 2), cv::Point(roi_rect.width,roi_rect.height / 2), 2, 2);
                }
            }
            else cv::putText(cameraFeed, "TOO MUCH NOISE! ADJUST FILTER", cv::Point(0, 50), 1, 2, cv::Scalar(0, 0, 255), 2);
        }

}

Then I want to convert it to Java to use in android.

void trackFilteredObject(int x, int y, Mat threshold, Mat cameraFeed){

    Mat temp = new Mat();
    threshold.copyTo(temp);
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    findContours(temp, contours, hierarchy, 2, 2);
    double refArea = 0;
    boolean objectFound = false;

    if (hierarchy.size() > 0) {
        List<MatOfPoint> largestContourVec = new ArrayList<MatOfPoint>();
        largestContourVec.add(contours.get(contours.size() - 1));
        Eco_Rect = boundingRect(largestContourVec.get(0));
        int numObjects = hierarchy.size();
        if (numObjects<100){
            for (int index = 0; index >= 0; index = hierarchy[index][0]){
                Moments moment = moments((Mat)contours.get(index));
                double area = moment.get_m00();
                if (area>0 && area>refArea){
                    x = (int) (moment.get_m10() / area);
                    y = (int) (moment.get_m01() / area);
                    objectFound = true;
                    refArea = area;
                }
            }

            if (objectFound == true){
                circle(cameraFeed, new Point(x, y), 10, new Scalar(0, 255, 0), 2);
                line(cameraFeed, new Point(roi_rect.width / 2, 0), new Point(roi_rect.width / 2, roi_rect.height),new Scalar(0,0,255), 2, 2);
                line(cameraFeed, new Point(0, roi_rect.height / 2), new Point(roi_rect.width,roi_rect.height / 2),new Scalar(0,0,255), 2, 2);
            }
        }
        else putText(cameraFeed, "TOO MUCH NOISE! ADJUST FILTER", new Point(0, 50), 1, 2, new Scalar(0, 0, 255), 2);
    }

}

However, i do not how to convert 3 lines coding that i commented due to different type of hierarchy in C++ and Java.

hierarchy.size() > 0 Error: cant compare org.opencv.core.Size with int. This one maybe can replace by hierarchy != null.

int numObjects = hierarchy.size(); Error: Basically same as above but i have no idea how to change it. I need to know the size.

for (int index = 0; index >= 0; index = hierarchy[index][0]) Error: In c++ hierarchy is vector type but in Java is Mat type.

Anyone can help me?

edit retag flag offensive close merge delete

Comments

Hi. Just have the same problem. Maybe you can reference to the book "OpenCV 3.0 Computer Vision with Java".

yogurtsea gravatar imageyogurtsea ( 2016-08-14 05:19:29 -0600 )edit

Someone please help on this :( I'm stuck and unable to find any documentation whatsoever.

Ash gravatar imageAsh ( 2018-03-23 01:02:10 -0600 )edit