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

OpenCV draw rectangle from webcam with 2 largest objects

我需要用网络摄像头中的 2 个最大对象绘制矩形。我已经用网络摄像头中的 2 个最大对象绘制轮廓,但现在我对如何绘制 2 个最大的矩形感到困惑。 有人可以告诉我代码吗~

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

看看下面的代码

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

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

输入图片

结果图像根据排序类型