Possible if only draw a Rectangle at the front object?

asked 2016-01-20 05:16:56 -0500

tenkeason gravatar image

May i know is that possible if we draw a rectangle at the front object show as below picture?

This is the Real Situation

image description

This is what the robot will get from webcam when tracking the color

image description

This what was i get now (draw a rectangles at biggest object)

image description

This is what i want to do

image description

int largestIndex = 0;
int largestContour = 0;
int secondLargestIndex = 0;
int secondLargestContour = 0;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

//find contours
findContours(thresh, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

/// Find the convex hull object for each contour
vector<vector<Point> >hull(contours.size());
vector<vector<int> >inthull(contours.size());
vector<vector<Vec4i> >defects(contours.size());

for (int i = 0; i < contours.size(); i++)
{
    convexHull(Mat(contours[i]), hull[i], false);
    convexHull(Mat(contours[i]),inthull[i], false);
    if (inthull[i].size()>3)
    convexityDefects(contours[i], inthull[i], defects[i]);
}

//find 2 largest contour
for( int i = 0; i< contours.size(); i++ )
{
    if(contours[i].size() > largestContour)
    {
        secondLargestContour = largestContour;
        secondLargestIndex = largestIndex;
        largestContour = contours[i].size();
        largestIndex = i;
    }
    else if(contours[i].size() > secondLargestContour)
    {
        secondLargestContour = contours[i].size();
        secondLargestIndex = i;
    }
}
    //show contours of 2 biggest and hull as well
if(contours.size()>0)
    {
    //check for contouraea function if error occur
    //draw the 2 largest contour using previously stored index.
    //drawContours(frame, contours, largestIndex, CV_RGB(0,255,0), 2, 8, hierarchy);
    //drawContours(frame, contours, secondLargestIndex, CV_RGB(0,255,0), 2, 8, hierarchy);

    //draw rectangle at specified obstacle
    Rect minRect = boundingRect( Mat(contours[ largestIndex ]) );
    rectangle( frame, minRect, Scalar( 0, 0, 255 ),2 );
    minRect = boundingRect( Mat(contours[ secondLargestIndex ]) );
    rectangle( frame, minRect, Scalar( 0, 0, 255 ),2 );}

This code link : http://paste.ofcode.org/33beiUcpvTuPd...

edit retag flag offensive close merge delete

Comments

1

Can't equip the robot with a depth sensor or a stereoscopy set? I can't think of a way to solve this issue in a way that it will work for all possible scenarios and perspectives.. a simple webcam only sees 2 dimensions :/

Pedro Batista gravatar imagePedro Batista ( 2016-01-20 06:53:45 -0500 )edit

OK thanks.

tenkeason gravatar imagetenkeason ( 2016-01-20 07:19:55 -0500 )edit

I agree to Pedro, without extra sensors, this is a no go, unless you know in advance the actual size of the object and want to do some sort of viewpoint/perspective analytics ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-20 07:56:23 -0500 )edit
1

If you know the shape of the objects and can guarantee the top or bottom will not line up exactly, then you can possibly make it work. A few other assumptions are necessary.

Do you know the shape of the objects?

Tetragramm gravatar imageTetragramm ( 2016-01-20 21:26:22 -0500 )edit