Path: blob/master/modules/imgproc/test/test_fitellipse.cpp
16339 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2016, Itseez, Inc, all rights reserved.56#include "test_precomp.hpp"78namespace opencv_test { namespace {910// return true if point lies inside ellipse11static bool check_pt_in_ellipse(const Point2f& pt, const RotatedRect& el) {12Point2f to_pt = pt - el.center;13double pt_angle = atan2(to_pt.y, to_pt.x);14double el_angle = el.angle * CV_PI / 180;15double x_dist = 0.5 * el.size.width * cos(pt_angle + el_angle);16double y_dist = 0.5 * el.size.height * sin(pt_angle + el_angle);17double el_dist = sqrt(x_dist * x_dist + y_dist * y_dist);18return cv::norm(to_pt) < el_dist;19}2021// Return true if mass center of fitted points lies inside ellipse22static bool fit_and_check_ellipse(const vector<Point2f>& pts) {23RotatedRect ellipse = fitEllipseDirect(pts); // fitEllipseAMS() also works fine2425Point2f mass_center;26for (size_t i = 0; i < pts.size(); i++) {27mass_center += pts[i];28}29mass_center /= (float)pts.size();3031return check_pt_in_ellipse(mass_center, ellipse);32}3334TEST(Imgproc_FitEllipse_Issue_4515, accuracy) {35vector<Point2f> pts;36pts.push_back(Point2f(327, 317));37pts.push_back(Point2f(328, 316));38pts.push_back(Point2f(329, 315));39pts.push_back(Point2f(330, 314));40pts.push_back(Point2f(331, 314));41pts.push_back(Point2f(332, 314));42pts.push_back(Point2f(333, 315));43pts.push_back(Point2f(333, 316));44pts.push_back(Point2f(333, 317));45pts.push_back(Point2f(333, 318));46pts.push_back(Point2f(333, 319));47pts.push_back(Point2f(333, 320));4849EXPECT_TRUE(fit_and_check_ellipse(pts));50}5152TEST(Imgproc_FitEllipse_Issue_6544, accuracy) {53vector<Point2f> pts;54pts.push_back(Point2f(924.784f, 764.160f));55pts.push_back(Point2f(928.388f, 615.903f));56pts.push_back(Point2f(847.4f, 888.014f));57pts.push_back(Point2f(929.406f, 741.675f));58pts.push_back(Point2f(904.564f, 825.605f));59pts.push_back(Point2f(926.742f, 760.746f));60pts.push_back(Point2f(863.479f, 873.406f));61pts.push_back(Point2f(910.987f, 808.863f));62pts.push_back(Point2f(929.145f, 744.976f));63pts.push_back(Point2f(917.474f, 791.823f));6465EXPECT_TRUE(fit_and_check_ellipse(pts));66}6768}} // namespace697071