# Draw Rectangle from Webcam with 2 largest object

I need to draw rectangle with 2 largest object from webcam. I already got to draw contours with 2 largest object from webcam but now i confuse in how to draw 2 largest Rectangle. Someone can show me the code Please~

//find and draw contours
void showconvex(Mat &thresh,Mat &frame)
{

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

edit retag close merge delete

Do you want to draw a rectangle around your largest contours or do you want to recognize if your contours are (approximate?) rectangles? To estimate and draw rectangles: http://docs.opencv.org/2.4/doc/tutori...

Sort by » oldest newest most voted take a look at the code below

based on sorting contours according to their bounding boxes.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

struct contour_sorter_dsc // sorts bounding rectangles of contours descending
{
bool operator ()( const vector<Point>& a, const vector<Point> & b )
{
Rect ra( boundingRect(a) );
Rect rb( boundingRect(b) );
return ( ( rb.width * rb.height ) < ( ra.width * ra.height ) );
}
};

int main( int argc, char** argv )
{
Mat src = imread( argv );
if( src.empty() )
{
return -1;
}

Mat gray;
cvtColor( src, gray, COLOR_BGR2GRAY );
gray = gray > 127; // binarize image

vector<vector<Point> > contours;
findContours( gray, contours, RETR_LIST, CHAIN_APPROX_SIMPLE );

sort(contours.begin(), contours.end(), contour_sorter_dsc());

for( size_t i = 0; i< 2; i++ )
{  // checks if the first contour is image boundary
if( contours == Point( 1, 1 ) & contours == Point( 1, gray.rows -2 )
& contours == Point( gray.cols - 2, gray.rows -2 ) & contours == Point( gray.cols - 2, 1 ) )
{
contours = contours;
contours = contours;
}

if( i < contours.size())
{
drawContours( src, contours, i, Scalar( 255,255,0 ) );
Rect minRect = boundingRect( Mat(contours[i]) );
rectangle( src, minRect, Scalar( 0, 0, 255 ) );
}
}

imshow( "result", src );
waitKey();

return 0;
}


TEST IMAGES AND RESULTS  with the test image below findContours finds image boundaries as a contour.

on the code you can see how we can eliminate it.  more

is that same concept if i using webcam? or something difference?

how will you binarize input image ? if you share your full code i can help more.

Sorry, i'm newbie in opencv, may i know how to send you the full code? The codes was too long, or can i have your email?

May i know if i want use call function when need this, what should i change/do in this code? –

you can do some additions like

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

Rect minRect = boundingRect( Mat(contours[ largestIndex ]) );
rectangle( src, minRect, Scalar( 0, 0, 255 ) );
minRect = boundingRect( Mat(contours[ secondLargestIndex ]) );
rectangle( src, minRect, Scalar( 0, 0, 255 ) );
}


yeah! it work! but why it will draw the rectangle at the other small object also? or my code have something wrong there?

Would you help me to solve this problem? What should i use to get the corner coordinates http://answers.opencv.org/question/82...

Official site

GitHub

Wiki

Documentation