# Revision history [back]

### Problem when calculating the fitted ellipse

I need to extract contour of shapes in images, and get the fitted ellipse. However, when I use the following code, the result is not very satisfying.

void MinConvexOval(Mat gray, vector<vector<point>> &contours, vector<mat> oval_result) { Mat oval(gray.rows3,gray.cols3,CV_8UC3);

    vector<vector<Point>>::const_iterator itc=contours.begin();
while(itc != contours.end())
{
double perimeter=arcLength(*itc,true);
double area= contourArea(*itc,false);
Rect rect=boundingRect(*itc);
CvBox2D box=minAreaRect(*itc);

Point pt1, pt2;
pt1.x=rect.x;
pt1.y=rect.y;
pt2.x=rect.x+rect.width;
pt2.y=rect.y+rect.height;

RotatedRect box2=fitEllipse(*itc);

ellipse(oval, box2, Scalar(255,0,255), 1, 8);
Point2f vertices[4];
box2.points(vertices);
for (int i = 0; i < 4; i++)
line(oval, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));
++itc;
}
drawContours(oval,contours,-1, Scalar(0,255,255), 1);

imshow("oval",oval);
waitKey(0);


}

Testing image: Result image: The fitting result of the right-down shape is wrong, could anyone help me, please? Thanks a lot!

 2 No.2 Revision thdrksdfthmn 2110 ●4 ●18 ●44

### Problem when calculating the fitted ellipse

I need to extract contour of shapes in images, and get the fitted ellipse. However, when I use the following code, the result is not very satisfying.

void MinConvexOval(Mat gray, vector<vector<point>> vector<vector<Point>> &contours, vector<mat> vector<Mat> oval_result)
{
Mat oval(gray.rows3,gray.cols3,CV_8UC3); oval(gray.rows*3,gray.cols*3,CV_8UC3);

vector<vector<Point>>::const_iterator itc=contours.begin();
while(itc != contours.end())
{
double perimeter=arcLength(*itc,true);
double area= contourArea(*itc,false);
Rect rect=boundingRect(*itc);
CvBox2D box=minAreaRect(*itc);

Point pt1, pt2;
pt1.x=rect.x;
pt1.y=rect.y;
pt2.x=rect.x+rect.width;
pt2.y=rect.y+rect.height;

RotatedRect box2=fitEllipse(*itc);

ellipse(oval, box2, Scalar(255,0,255), 1, 8);
Point2f vertices[4];
box2.points(vertices);
for (int i = 0; i < 4; i++)
line(oval, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));
++itc;
}
drawContours(oval,contours,-1, Scalar(0,255,255), 1);

imshow("oval",oval);
waitKey(0);
}
} Testing image:

Result image:

The fitting result of the right-down shape is wrong, could anyone help me, please? Thanks a lot! 


 3 No.3 Revision updated 2015-04-30 21:15:55 -0500 Problem when calculating the fitted ellipse I need to extract contour of shapes in images, and get the fitted ellipse. However, when I use the following code, the result is not very satisfying. #include "stdafx.h" include <iostream> include <opencv2\core\core.hpp> include <opencv2\highgui\highgui.hpp> include <opencv2\imgproc\imgproc.hpp> include <fstream> using namespace std; using namespace cv; extern void Threshold(Mat gray, Mat& binary); extern void Not(Mat& im); // Calculate the contour of connected region // erease the image that is too large or too small void getSizeContours(vector<vector<point>> &contours) { int cmin = 10; // min contour length int cmax = 10000; // max contour length vector<vector<point>>::const_iterator itc = contours.begin(); while(itc != contours.end()) { if((itc->size()) < cmin || (itc->size()) > cmax) { itc = contours.erase(itc); } else ++ itc; } } // Calculate min convex oval void MinConvexOval(Mat gray, vector<vector<Point>> vector<vector<point>> &contours, vector<Mat> vector<mat> oval_result) { Mat oval(gray.rows*3,gray.cols*3,CV_8UC3); vector<vector<Point>>::const_iterator oval(gray.rows3,gray.cols3,CV_8UC3); vector<vector<point>>::const_iterator itc=contours.begin(); while(itc != contours.end()) { double perimeter=arcLength(*itc,true); perimeter=arcLength(itc,true); double area= contourArea(*itc,false); contourArea(itc,false); Rect rect=boundingRect(*itc); rect=boundingRect(itc); CvBox2D box=minAreaRect(*itc); box=minAreaRect(itc); Point pt1, pt2; pt1.x=rect.x; pt1.y=rect.y; pt2.x=rect.x+rect.width; pt2.y=rect.y+rect.height; RotatedRect box2=fitEllipse(*itc); ellipse(oval, box2, Scalar(255,0,255), 1, 8); Point2f vertices[4]; box2.points(vertices); for (int i = 0; i < 4; i++) line(oval, vertices[i], vertices[(i+1)%4], Scalar(0,255,0)); ++itc; } drawContours(oval,contours,-1, Scalar(0,255,255), 1); imshow("oval",oval); waitKey(0); } } void cvText(Mat img, const char* text, int x, int y) { CvFont font; double hscale = 1.0; double vscale = 1.0; int linewidth = 2; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX |CV_FONT_ITALIC,hscale,vscale,0,linewidth); Scalar textColor =Scalar(0,255,255); Point textPos =Point(x, y); putText(img, text, textPos, CV_FONT_HERSHEY_COMPLEX, 1,textColor); } void GetContour(Mat grayim) { Mat binaryim; Threshold(grayim, binaryim); // binaryzation vector<vector<point>> contours; //CV_CHAIN_APPROX_NONE Get all of the pixels of the contours findContours(binaryim, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0)); getSizeContours(contours); vector<Mat> oval_result; MinConvexOval(grayim,contours,oval_result); } Testing image: Result image: The fitting result of the right-down shape is wrong, could anyone help me, please? Thanks a lot! 4 No.4 Revision updated 2015-04-30 21:50:48 -0500 Problem when calculating the fitted ellipse I need to extract contour of shapes in images, and get the fitted ellipse. However, when I use the following code, the result is not very satisfying. #include "stdafx.h" include <iostream> include <opencv2\core\core.hpp> include <opencv2\highgui\highgui.hpp> include <opencv2\imgproc\imgproc.hpp> include #include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include <fstream> using namespace std; using namespace cv; extern void Threshold(Mat gray, Mat& binary); extern void Not(Mat& im); // Calculate the contour of connected region // erease the image that is too large or too small void getSizeContours(vector<vector<point>> &contours) { int cmin = 10; // min contour length int cmax = 10000; // max contour length vector<vector<point>>::const_iterator itc = contours.begin(); while(itc != contours.end()) { if((itc->size()) < cmin || (itc->size()) > cmax) { itc = contours.erase(itc); } else ++ itc; } } // Calculate min convex oval void MinConvexOval(Mat gray, vector<vector<point>> &contours, vector<mat> oval_result) { Mat oval(gray.rows3,gray.cols3,CV_8UC3); vector<vector<point>>::const_iterator itc=contours.begin(); while(itc != contours.end()) { double perimeter=arcLength(itc,true); double area= contourArea(itc,false); Rect rect=boundingRect(itc); CvBox2D box=minAreaRect(itc); Point pt1, pt2; pt1.x=rect.x; pt1.y=rect.y; pt2.x=rect.x+rect.width; pt2.y=rect.y+rect.height; RotatedRect box2=fitEllipse(*itc); ellipse(oval, box2, Scalar(255,0,255), 1, 8); Point2f vertices[4]; box2.points(vertices); for (int i = 0; i < 4; i++) line(oval, vertices[i], vertices[(i+1)%4], Scalar(0,255,0)); ++itc; } drawContours(oval,contours,-1, Scalar(0,255,255), 1); imshow("oval",oval); waitKey(0); } void cvText(Mat img, const char* text, int x, int y) { CvFont font; double hscale = 1.0; double vscale = 1.0; int linewidth = 2; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX |CV_FONT_ITALIC,hscale,vscale,0,linewidth); Scalar textColor =Scalar(0,255,255); Point textPos =Point(x, y); putText(img, text, textPos, CV_FONT_HERSHEY_COMPLEX, 1,textColor); } void GetContour(Mat grayim) { Mat binaryim; Threshold(grayim, binaryim); // binaryzation vector<vector<point>> binaryzation vector<vector<Point>> contours; //CV_CHAIN_APPROX_NONE Get all of the pixels of the contours findContours(binaryim, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0)); getSizeContours(contours); vector<Mat> oval_result; MinConvexOval(grayim,contours,oval_result); } Testing image: Result image: The fitting result of the right-down shape is wrong, could anyone help me, please? Thanks a lot! 5 No.5 Revision updated 2015-05-01 07:40:07 -0500 Eduardo 3414 ●12 ●39 Problem when calculating the fitted ellipse I need to extract contour of shapes in images, and get the fitted ellipse. However, when I use the following code, the result is not very satisfying. #include "stdafx.h" #include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include <fstream> <fstream> using namespace std; using namespace cv; extern void Threshold(Mat gray, Mat& binary); extern void Not(Mat& im); im); // Calculate the contour of connected region // erease the image that is too large or too small void getSizeContours(vector<vector<point>> getSizeContours(vector<vector<Point>> &contours) { int cmin = 10; // min contour length int cmax = 10000; // max contour length vector<vector<point>>::const_iterator vector<vector<Point>>::const_iterator itc = contours.begin(); while(itc != contours.end()) { if((itc->size()) < cmin || (itc->size()) > cmax) { itc = contours.erase(itc); } else ++ itc; } } // Calculate min convex oval void MinConvexOval(Mat gray, vector<vector<point>> vector<vector<Point>> &contours, vector<mat> vector<Mat> oval_result) { Mat oval(gray.rows3,gray.cols3,CV_8UC3); vector<vector<point>>::const_iterator oval(gray.rows*3,gray.cols*3,CV_8UC3); vector<vector<Point>>::const_iterator itc=contours.begin(); while(itc != contours.end()) { double perimeter=arcLength(itc,true); perimeter=arcLength(*itc,true); double area= contourArea(itc,false); contourArea(*itc,false); Rect rect=boundingRect(itc); rect=boundingRect(*itc); CvBox2D box=minAreaRect(itc); box=minAreaRect(*itc); Point pt1, pt2; pt1.x=rect.x; pt1.y=rect.y; pt2.x=rect.x+rect.width; pt2.y=rect.y+rect.height; RotatedRect box2=fitEllipse(*itc); ellipse(oval, box2, Scalar(255,0,255), 1, 8); Point2f vertices[4]; box2.points(vertices); for (int i = 0; i < 4; i++) line(oval, vertices[i], vertices[(i+1)%4], Scalar(0,255,0)); ++itc; } drawContours(oval,contours,-1, Scalar(0,255,255), 1); imshow("oval",oval); waitKey(0); } } void cvText(Mat img, const char* text, int x, int y) { CvFont font; font; double hscale = 1.0; double vscale = 1.0; int linewidth = 2; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX |CV_FONT_ITALIC,hscale,vscale,0,linewidth); Scalar textColor =Scalar(0,255,255); Point textPos =Point(x, y); putText(img, text, textPos, CV_FONT_HERSHEY_COMPLEX, 1,textColor); } } void GetContour(Mat grayim) { Mat binaryim; Threshold(grayim, binaryim); // binaryzation binaryzation vector<vector<Point>> contours; //CV_CHAIN_APPROX_NONE Get all of the pixels of the contours findContours(binaryim, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0)); getSizeContours(contours); vector<Mat> oval_result; MinConvexOval(grayim,contours,oval_result); } } Testing image: image: Result image: image: The fitting result of the right-down shape is wrong, could anyone help me, please? Thanks a lot! 


 Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license. about | faq | help | privacy policy | terms of service Powered by Askbot version 0.10.2 Please note: OpenCV answers requires javascript to work properly, please enable javascript in your browser, here is how //IE fix to hide the red margin var noscript = document.getElementsByTagName('noscript')[0]; noscript.style.padding = '0px'; noscript.style.backgroundColor = 'transparent'; askbot['urls']['mark_read_message'] = '/s/messages/markread/'; askbot['urls']['get_tags_by_wildcard'] = '/s/get-tags-by-wildcard/'; askbot['urls']['get_tag_list'] = '/s/get-tag-list/'; askbot['urls']['follow_user'] = '/followit/follow/user/{{userId}}/'; askbot['urls']['unfollow_user'] = '/followit/unfollow/user/{{userId}}/'; askbot['urls']['user_signin'] = '/account/signin/'; askbot['urls']['getEditor'] = '/s/get-editor/'; askbot['urls']['apiGetQuestions'] = '/s/api/get_questions/'; askbot['urls']['ask'] = '/questions/ask/'; askbot['urls']['questions'] = '/questions/'; askbot['settings']['groupsEnabled'] = false; askbot['settings']['static_url'] = '/m/'; askbot['settings']['minSearchWordLength'] = 4; askbot['settings']['mathjaxEnabled'] = false; askbot['settings']['sharingSuffixText'] = ''; askbot['settings']['errorPlacement'] = 'after-label'; askbot['data']['maxCommentLength'] = 800; askbot['settings']['editorType'] = 'markdown'; askbot['settings']['commentsEditorType'] = 'rich\u002Dtext'; askbot['messages']['askYourQuestion'] = 'Ask Your Question'; askbot['messages']['questionSingular'] = 'question'; askbot['messages']['answerSingular'] = 'answer'; askbot['messages']['acceptOwnAnswer'] = 'accept or unaccept your own answer'; askbot['messages']['followQuestions'] = 'follow questions'; askbot['settings']['allowedUploadFileTypes'] = [ "jpg", "jpeg", "gif", "bmp", "png", "tiff" ]; askbot['data']['haveFlashNotifications'] = true; askbot['data']['activeTab'] = 'questions'; askbot['settings']['csrfCookieName'] = 'csrftoken'; askbot['data']['searchUrl'] = ''; /*<![CDATA[*/ $('.mceStatusbar').remove();//a hack to remove the tinyMCE status bar$(document).ready(function(){ // focus input on the search bar endcomment var activeTab = askbot['data']['activeTab']; if (inArray(activeTab, ['users', 'questions', 'tags', 'badges'])) { var searchInput = $('#keywords'); } else if (activeTab === 'ask') { var searchInput =$('#id_title'); } else { var searchInput = undefined; animateHashes(); } if (searchInput) { searchInput.focus(); putCursorAtEnd(searchInput); } var haveFullTextSearchTab = inArray(activeTab, ['questions', 'badges', 'ask']); var haveUserProfilePage = $('body').hasClass('user-profile-page'); if ((haveUserProfilePage || haveFullTextSearchTab) && searchInput && searchInput.length) { var search = new FullTextSearch(); askbot['controllers'] = askbot['controllers'] || {}; askbot['controllers']['fullTextSearch'] = search; search.setSearchUrl(askbot['data']['searchUrl']); if (activeTab === 'ask') { search.setAskButtonEnabled(false); } search.decorate(searchInput); } else if (activeTab === 'tags') { var search = new TagSearch(); search.decorate(searchInput); } if (askbot['data']['userIsAdminOrMod']) {$('body').addClass('admin'); } if (askbot['settings']['groupsEnabled']) { askbot['urls']['add_group'] = "/s/add-group/"; var group_dropdown = new GroupDropdown(); $('.groups-dropdown').append(group_dropdown.getElement()); } var userRep =$('#userToolsNav .reputation'); if (userRep.length) { var showPermsTrigger = new ShowPermsTrigger(); showPermsTrigger.decorate(userRep); } }); if (askbot['data']['haveFlashNotifications']) { $('#validate_email_alert').click(function(){notify.close(true)}) notify.show(); } var langNav =$('.lang-nav'); if (langNav.length) { var nav = new LangNav(); nav.decorate(langNav); } /*]]>*/ //todo - take this out into .js file $(document).ready(function(){$('div.revision div[id^=rev-header-]').bind('click', function(){ var revId = this.id.substr(11); toggleRev(revId); }); lanai.highlightSyntax(); }); function toggleRev(id) { var arrow = $("#rev-arrow-" + id); var visible = arrow.attr("src").indexOf("hide") > -1; if (visible) { var image_path = '/m/default/media/images/expander-arrow-show.gif?v=6'; } else { var image_path = '/m/default/media/images/expander-arrow-hide.gif?v=6'; } image_path = image_path + "?v=6"; arrow.attr("src", image_path);$("#rev-body-" + id).slideToggle("fast"); } for (url_name in askbot['urls']){ askbot['urls'][url_name] = cleanUrl(askbot['urls'][url_name]); }