Ask Your Question

Revision history [back]

image description

with a bit correction i get this image

( the solution key is changing parameter of threshold from 25 to 125 )

#include "opencv2/opencv.hpp"

using namespace cv;



int test(Mat src)
{
    int largest_area=0;
    int largest_contour_index=0;
    cv::Rect bounding_rect;

    Mat thr;
    Mat dst;
    cvtColor(src,thr,COLOR_BGR2GRAY); //Convert to gray
    threshold(thr, thr,125, 255,THRESH_BINARY); //Threshold the gray

    vector<vector<cv::Point> > contours; // Vector for storing contour
    vector<Vec4i> hierarchy;

    findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image

    for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
    {
        double a=contourArea( contours[i],false);  //  Find the area of contour
        if(a>largest_area)
        {
            largest_area=a;
            largest_contour_index=i;                //Store the index of largest contour
            bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
        }

    }

    Scalar color( 0,0,255);
    drawContours( dst, contours,largest_contour_index, color, CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
    rectangle(src, bounding_rect,  Scalar(0,255,0),5, 8,0);
}

int main()
{

    namedWindow("test",0);
    Mat img;

    img=imread("YtmZcfe.jpg",1);
    test(img);
    imwrite("t.jpg",img);
    imshow("test", img);

    waitKey(0);
    return 0;
}

image description

with a bit correction i get this image

( the solution key is changing parameter of threshold from 25 to 125 ) sample code for finding and drawing biggest contour.

#include "opencv2/opencv.hpp"
<opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;
 using namespace std;

int test(Mat src)
main( int argc, char** argv )
{
    Mat src = imread( argv[1] );

    int largest_area=0;
    int largest_contour_index=0;
    cv::Rect Rect bounding_rect;

    Mat thr;
    Mat dst;
    cvtColor(src,thr,COLOR_BGR2GRAY); cvtColor( src, thr, COLOR_BGR2GRAY ); //Convert to gray
    threshold(thr, thr,125, 255,THRESH_BINARY); threshold( thr, thr, 125, 255, THRESH_BINARY ); //Threshold the gray

    vector<vector<cv::Point> vector<vector<Point> > contours; // Vector for storing contour
    vector<Vec4i> hierarchy;
contours

    findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE RETR_CCOMP, CHAIN_APPROX_SIMPLE ); // Find the contours in the image

    for( int size_t i = 0; i< contours.size(); i++ ) // iterate through each contour.
    {
        double a=contourArea( contours[i],false); area = contourArea( contours[i] );  //  Find the area of contour
        if(a>largest_area)

        if( area > largest_area )
        {
            largest_area=a;
            largest_contour_index=i;  largest_area = area;
            largest_contour_index = i;               //Store the index of largest contour
            bounding_rect=boundingRect(contours[i]); bounding_rect = boundingRect( contours[i] ); // Find the bounding rectangle for biggest contour
        }
     }

    Scalar color( 0,0,255);
    drawContours( dst, src, contours,largest_contour_index, color, CV_FILLED, 8, hierarchy Scalar( 0, 255, 0 ), 2 ); // Draw the largest contour using previously stored index.
    rectangle(src, bounding_rect,  Scalar(0,255,0),5, 8,0);
}

int main()
{

    namedWindow("test",0);
    Mat img;

    img=imread("YtmZcfe.jpg",1);
    test(img);
    imwrite("t.jpg",img);
    imshow("test", img);

    waitKey(0);

    imshow( "result", src );
    waitKey();
    return 0;
}