OpenCV Distance c++

asked 2018-12-19 03:35:25 -0500

Usubu gravatar image

Goodmorning everyone. I am making a small program that allows me to go and determine the area of the object and then to determine the distance of this object. To determine the distance of the object. I determined the area and measured the distance with the meter, repeating the same operation several times. At this point trying with any other object, I do not understand why the part on the distance is not printed on the screen.

The full code

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

    Mat temp;
    threshold.copyTo(temp);
    //these two vectors needed for output of findContours
    std::vector< std::vector<Point> > contours;
    std::vector<Vec4i> hierarchy;
    //find contours of filtered image using openCV findContours function
    findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );
    //use moments method to find our filtered object
    double refArea = 0;
    bool objectFound = false;
    if (hierarchy.size() > 0) {
        int numObjects = hierarchy.size();
        //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
        if(numObjects<MAX_NUM_OBJECTS){
            for (int index = 0; index >= 0; index = hierarchy[index][0]) {

                Moments moment = moments((cv::Mat)contours[index]);
                double area = moment.m00;

                //if the area is less than 20 px by 20px then it is probably just noise
                //if the area is the same as the 3/2 of the image size, probably just a bad filter
                //we only want the object with the largest area so we safe a reference area each
                //iteration and compare it to the area in the next iteration.
                if(area>MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea){
                    x = moment.m10/area;
                    y = moment.m01/area;
                    xCoord=x;
                    yCoord=y;
                    objectFound = true;
                    refArea = area;
                    std::cout<<area<<std::endl;

                    //DISTANCE HERE
                    double dist;
                    if (refArea==0) {
                        cout << "\nArea is egual to 0" << endl;
                    }
                    else if (refArea>0 && refArea<=295) {
                        dist = 50;
                        cout << "\nThe distance of the object is " << dist << " cm\n" <<endl;
                    }
                    else if (refArea>295 && refArea<=320) {
                        dist = 65;
                        cout << "\nThe distance of the object is " << dist << " cm\n" <<endl;
                    }
                    else if (refArea>320 && refArea<=335) {
                        dist = 95;
                        cout << "\nThe distance of the object is " << dist << " cm\n" <<endl;
                    }
                    else {
                        cout << "\nDistance no determinate" <<endl;
                    }
                    // end of distance
                } else objectFound = false;


            }
            //let user know you found an object
            if(objectFound ==true) {
                putText(cameraFeed,"Tracking Object",Point(0,50),2,1,Scalar(0,255,0),2);
                //draw object location on screen
                drawObject(x,y,cameraFeed);
            }

        }else putText(cameraFeed,"TOO MUCH NOISE! ADJUST FILTER",Point(0,50),1,2,Scalar(0,0,255),2);
    }
}

Can you help me to understand why nothing relative to distance is printed? Thank you

edit retag flag offensive close merge delete

Comments

go and debug it.

berak gravatar imageberak ( 2018-12-19 03:38:06 -0500 )edit

Ok now it works. But in your opinion can i use this code to measure distance?

Usubu gravatar imageUsubu ( 2018-12-19 04:13:01 -0500 )edit

if you meant "real world distance" : https://en.wikipedia.org/wiki/Pinhole...

berak gravatar imageberak ( 2018-12-19 04:17:20 -0500 )edit