Ask Your Question
1

Draw Rectangle from Webcam with 2 largest object

asked 2016-01-01 23:34:12 -0600

tenkeason gravatar image

updated 2016-01-02 03:33:27 -0600

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 flag offensive close merge delete

Comments

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...

FooBar gravatar imageFooBar ( 2016-01-02 03:34:56 -0600 )edit

yeah , i only want draw rectangles around 2 largest contour only. Please help me -w-

tenkeason gravatar imagetenkeason ( 2016-01-02 04:14:04 -0600 )edit

Then just have a look at the link I posted.

FooBar gravatar imageFooBar ( 2016-01-02 05:11:12 -0600 )edit

1 answer

Sort by » oldest newest most voted
3

answered 2016-01-02 06:42:48 -0600

updated 2016-01-02 10:01:13 -0600

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[1] );
    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[0][0] == Point( 1, 1 ) & contours[0][1] == Point( 1, gray.rows -2 )
                & contours[0][2] == Point( gray.cols - 2, gray.rows -2 ) & contours[0][3] == Point( gray.cols - 2, 1 ) )
        {
            contours[0] = contours[1];
            contours[1] = contours[2];
        }

        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

image description

image description

with the test image below findContours finds image boundaries as a contour.

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

image description

image description

edit flag offensive delete link more

Comments

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

tenkeason gravatar imagetenkeason ( 2016-01-02 09:40:16 -0600 )edit

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

sturkmen gravatar imagesturkmen ( 2016-01-02 09:59:15 -0600 )edit

also check your post on SO

sturkmen gravatar imagesturkmen ( 2016-01-02 10:47:23 -0600 )edit

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?

tenkeason gravatar imagetenkeason ( 2016-01-02 21:17:59 -0600 )edit

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

tenkeason gravatar imagetenkeason ( 2016-01-03 03:58:07 -0600 )edit

you can paste your code here and give the link

sturkmen gravatar imagesturkmen ( 2016-01-03 04:20:37 -0600 )edit

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 ) );
    }
sturkmen gravatar imagesturkmen ( 2016-01-03 05:18:18 -0600 )edit

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

tenkeason gravatar imagetenkeason ( 2016-01-03 05:28:01 -0600 )edit

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

tenkeason gravatar imagetenkeason ( 2016-01-04 03:27:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-01 23:33:26 -0600

Seen: 1,059 times

Last updated: Jan 02 '16