OpenCV从网络摄像头绘制矩形,有2个最大的对象

时间:2016-01-02 05:39:50

标签: opencv contour rectangles

我需要从网络摄像头绘制带有2个最大对象的矩形。我已经用网络摄像头中的2个最大的对象绘制轮廓但现在我混淆了如何绘制2个最大的矩形。 有人可以给我看一下代码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);
    }
}

1 个答案:

答案 0 :(得分:1)

看看下面的代码

基于按边界框或区域排序轮廓。

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

using namespace cv;
using namespace std;

struct contour_sorter_dsc // sorts contours by their bounding boxes 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 ) );
    }
};

struct contour_sorter_dsc_area // sorts contours by their areas descending
{
    bool operator ()( const vector<Point>& a, const vector<Point> & b )
    {
        double area_a = contourArea( a );
        double area_b = contourArea( b );
        return ( area_b < area_a );
    }
};

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

    Mat canvas1 = src.clone();
    Mat canvas2 = src.clone();

    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( canvas1, contours, i, Scalar( 255,255,0 ) );
            Rect minRect = boundingRect( Mat(contours[i]) );
            rectangle( canvas1, minRect, Scalar( 0, 0, 255 ) );
        }
    }

    imshow( "result of sorting contours by bounding boxes ", canvas1 );

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

    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( canvas2, contours, i, Scalar( 255,255,0 ) );
            Rect minRect = boundingRect( Mat(contours[i]) );
            rectangle( canvas2, minRect, Scalar( 0, 0, 255 ) );
        }
    }

    imshow( "result of sorting contours by areas ", canvas2 );
    waitKey();
    return 0;
}

输入图片

enter image description here

根据排序类型

的结果图像

enter image description here

enter image description here