Ask Your Question

Revision history [back]

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

        drawContours( src, contours, i, Scalar(0,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

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(0,255,0) );
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