Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

the following code should make the trick:

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Mat bin;
    threshold(src, bin, 100, 255, CV_THRESH_BINARY);
    findContours( bin, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    // merge all contours into one vector
    std::vector<cv::Point> merged_contour_points;
    for (size_t i = 0; i < contours.size(); i++) {
      for (size_t j = 0; j < contours[i].size(); j++) {
        merged_contour_points.push_back(contours[i][j]);
      }
    }

    // get rotated bounding box
    std::vector<cv::Point> hull;
    cv::convexHull(cv::Mat(merged_contour_points),hull);
    cv::Mat hull_points(hull);
    cv::RotatedRect rotated_bounding_rect = minAreaRect(hull_points);
    // get ellipse
    cv::RotatedRect minEllipse = fitEllipse(hull_points);


    Mat drawing = Mat::zeros( src.size(), CV_8UC3 );

    for( size_t i = 0; i < contours.size(); i++ )
    {
        drawContours( drawing, contours, i, Scalar(255, 255, 255), 1, 8, vector<Vec4i>(), 0, Point() );
    }

    ellipse( drawing, minEllipse, Scalar(0, 255, 0), 1, 8 );
    Point2f rect_points[4]; rotated_bounding_rect.points( rect_points );
    for( int j = 0; j < 4; j++ )
      line( drawing, rect_points[j], rect_points[(j+1)%4], Scalar(0, 0, 255), 1, 8 );

image description image description

image description image description

enjoy :-)

the following code should make the trick:

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Mat bin;
    threshold(src, bin, 100, 255, CV_THRESH_BINARY);
    findContours( bin, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    // merge all contours into one vector
    std::vector<cv::Point> merged_contour_points;
    for (size_t i = 0; i < contours.size(); i++) {
      for (size_t j = 0; j < contours[i].size(); j++) {
        merged_contour_points.push_back(contours[i][j]);
      }
    }

    // get rotated bounding box
    std::vector<cv::Point> hull;
    cv::convexHull(cv::Mat(merged_contour_points),hull);
    cv::Mat hull_points(hull);
    cv::RotatedRect rotated_bounding_rect = minAreaRect(hull_points);
    // get ellipse
    cv::RotatedRect minEllipse = fitEllipse(hull_points);


    Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
     // draw contours
    for( size_t i = 0; i < contours.size(); i++ )
    {
        drawContours( drawing, contours, i, Scalar(255, 255, 255), 1, 8, vector<Vec4i>(), 0, Point() );
    }

    // draw ellipse
    ellipse( drawing, minEllipse, Scalar(0, 255, 0), 1, 8 );

    // draw rotated bounding box
    Point2f rect_points[4]; rotated_bounding_rect.points( rect_points );
    for( int j = 0; j < 4; j++ )
      line( drawing, rect_points[j], rect_points[(j+1)%4], Scalar(0, 0, 255), 1, 8 );

image description image description

image description image description

enjoy :-)

the following code should make do the trick:

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Mat bin;
    threshold(src, bin, 100, 255, CV_THRESH_BINARY);
    findContours( bin, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    // merge all contours into one vector
    std::vector<cv::Point> merged_contour_points;
    for (size_t i = 0; i < contours.size(); i++) {
      for (size_t j = 0; j < contours[i].size(); j++) {
        merged_contour_points.push_back(contours[i][j]);
      }
    }

    // get rotated bounding box
    std::vector<cv::Point> hull;
    cv::convexHull(cv::Mat(merged_contour_points),hull);
    cv::Mat hull_points(hull);
    cv::RotatedRect rotated_bounding_rect = minAreaRect(hull_points);
    // get ellipse
    cv::RotatedRect minEllipse = fitEllipse(hull_points);


    Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
    // draw contours
    for( size_t i = 0; i < contours.size(); i++ )
    {
        drawContours( drawing, contours, i, Scalar(255, 255, 255), 1, 8, vector<Vec4i>(), 0, Point() );
    }

    // draw ellipse
    ellipse( drawing, minEllipse, Scalar(0, 255, 0), 1, 8 );

    // draw rotated bounding box
    Point2f rect_points[4]; rotated_bounding_rect.points( rect_points );
    for( int j = 0; j < 4; j++ )
      line( drawing, rect_points[j], rect_points[(j+1)%4], Scalar(0, 0, 255), 1, 8 );

image description image description

image description image description

enjoy :-)