Possible if only draw a Rectangle at the front object?
May i know is that possible if we draw a rectangle at the front object show as below picture?
This is the Real Situation
This is what the robot will get from webcam when tracking the color
This what was i get now (draw a rectangles at biggest object)
This is what i want to do
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...
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 :/
OK thanks.
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 ...
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?