# 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 );}


edit retag close merge delete