- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一堆二维点。您可以在左图中看到它们。它们形成了某种带有几只兔子耳朵的环。我的目标是找到大的内循环/椭圆,您可以在右侧看到它。
什么样的算法对这种情况有用。
我尝试了 RANSAC 算法的变体(取 5 个随机点,形成一个椭圆,确定一个分数并重复)。我以某种方式设计了评分函数,椭圆内的点得到很多负分,而椭圆外但非常接近的点得到很多正分。但结果一点也不乐观。该算法找到了环,但我得到了环的一些随机椭圆,而不是我想要的大内椭圆。
有什么好的策略吗?
最佳答案
这里有一些 best fit circle 的 Java 代码。
https://www.spaceroots.org/documents/circle/CircleFitter.java
// Copyright (c) 2005-2007, Luc Maisonobe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with
// or without modification, are permitted provided that
// the following conditions are met:
//
// Redistributions of source code must retain the
// above copyright notice, this list of conditions and
// the following disclaimer.
// Redistributions in binary form must reproduce the
// above copyright notice, this list of conditions and
// the following disclaimer in the documentation
// and/or other materials provided with the
// distribution.
// Neither the names of spaceroots.org, spaceroots.com
// nor the names of their contributors may be used to
// endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package org.spaceroots;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.awt.geom.Point2D;
/** Class fitting a circle to a set of points.
* <p>This class implements the fitting algorithms described in the
* paper <a
* href="http://www.spaceroots.org/documents/circle/circle-fitting.pdf">
* Finding the circle that best fits a set of points</a></p>
* @author Luc Maisonobe
*/
public class CircleFitter {
/** Test program entry point.
* @param args command line arguments
*/
public static void main(String[] args) {
try {
BufferedReader br = null;
switch (args.length) {
case 0 :
br = new BufferedReader((new InputStreamReader(System.in)));
break;
case 1:
br = new BufferedReader(new FileReader(args[0]));
break;
default :
System.err.println("usage: java CircleFitter [file]");
System.exit(1);
}
// read the points, ignoring blank lines and comment lines
ArrayList list = new ArrayList();
int l = 0;
for (String line = br.readLine(); line != null; line = br.readLine()) {
++l;
line = line.trim();
if ((line.length() > 0) && (! line.startsWith("#"))) {
// this is a data line, we expect two numerical fields
String[] fields = line.split("\\s+");
if (fields.length != 2) {
throw new LocalException("syntax error at line " + l + ": " + line
+ "(expected two fields, found"
+ fields.length + ")");
}
// parse the fields and add the point to the list
list.add(new Point2D.Double(Double.parseDouble(fields[0]),
Double.parseDouble(fields[1])));
}
}
Point2D.Double[] points =
(Point2D.Double[]) list.toArray(new Point2D.Double[list.size()]);
DecimalFormat format =
new DecimalFormat("000.00000000",
new DecimalFormatSymbols(Locale.US));
// fit a circle to the test points
CircleFitter fitter = new CircleFitter();
fitter.initialize(points);
System.out.println("initial circle: "
+ format.format(fitter.getCenter().x)
+ " " + format.format(fitter.getCenter().y)
+ " " + format.format(fitter.getRadius()));
// minimize the residuals
int iter = fitter.minimize(100, 0.1, 1.0e-12);
System.out.println("converged after " + iter + " iterations");
System.out.println("final circle: "
+ format.format(fitter.getCenter().x)
+ " " + format.format(fitter.getCenter().y)
+ " " + format.format(fitter.getRadius()));
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
System.exit(1);
} catch (LocalException le) {
System.err.println(le.getMessage());
System.exit(1);
}
}
/** Build a new instance with a default current circle.
*/
public CircleFitter() {
center = new Point2D.Double(0.0, 0.0);
rHat = 1.0;
points = null;
}
/** Initialize an approximate circle based on all triplets.
* @param points circular ring sample points
* @exception LocalException if all points are aligned
*/
public void initialize(Point2D.Double[] points)
throws LocalException {
// store the points array
this.points = points;
// analyze all possible points triplets
center.x = 0.0;
center.y = 0.0;
int n = 0;
for (int i = 0; i < (points.length - 2); ++i) {
Point2D.Double p1 = (Point2D.Double) points[i];
for (int j = i + 1; j < (points.length - 1); ++j) {
Point2D.Double p2 = (Point2D.Double) points[j];
for (int k = j + 1; k < points.length; ++k) {
Point2D.Double p3 = (Point2D.Double) points[k];
// compute the triangle circumcenter
Point2D.Double cc = circumcenter(p1, p2, p3);
if (cc != null) {
// the points are not aligned, we have a circumcenter
++n;
center.x += cc.x;
center.y += cc.y;
}
}
}
}
if (n == 0) {
throw new LocalException("all points are aligned");
}
// initialize using the circumcenters average
center.x /= n;
center.y /= n;
updateRadius();
}
/** Update the circle radius.
*/
private void updateRadius() {
rHat = 0;
for (int i = 0; i < points.length; ++i) {
double dx = points[i].x - center.x;
double dy = points[i].y - center.y;
rHat += Math.sqrt(dx * dx + dy * dy);
}
rHat /= points.length;
}
/** Compute the circumcenter of three points.
* @param pI first point
* @param pJ second point
* @param pK third point
* @return circumcenter of pI, pJ and pK or null if the points are aligned
*/
private Point2D.Double circumcenter(Point2D.Double pI,
Point2D.Double pJ,
Point2D.Double pK) {
// some temporary variables
Point2D.Double dIJ = new Point2D.Double(pJ.x - pI.x, pJ.y - pI.y);
Point2D.Double dJK = new Point2D.Double(pK.x - pJ.x, pK.y - pJ.y);
Point2D.Double dKI = new Point2D.Double(pI.x - pK.x, pI.y - pK.y);
double sqI = pI.x * pI.x + pI.y * pI.y;
double sqJ = pJ.x * pJ.x + pJ.y * pJ.y;
double sqK = pK.x * pK.x + pK.y * pK.y;
// determinant of the linear system: 0 for aligned points
double det = dJK.x * dIJ.y - dIJ.x * dJK.y;
if (Math.abs(det) < 1.0e-10) {
// points are almost aligned, we cannot compute the circumcenter
return null;
}
// beware, there is a minus sign on Y coordinate!
return new Point2D.Double(
(sqI * dJK.y + sqJ * dKI.y + sqK * dIJ.y) / (2 * det),
-(sqI * dJK.x + sqJ * dKI.x + sqK * dIJ.x) / (2 * det));
}
/** Minimize the distance residuals between the points and the circle.
* <p>We use a non-linear conjugate gradient method with the Polak and
* Ribiere coefficient for the computation of the search direction. The
* inner minimization along the search direction is performed using a
* few Newton steps. It is worthless to spend too much time on this inner
* minimization, so the convergence threshold can be rather large.</p>
* @param maxIter maximal iterations number on the inner loop (cumulated
* across outer loop iterations)
* @param innerThreshold inner loop threshold, as a relative difference on
* the cost function value between the two last iterations
* @param outerThreshold outer loop threshold, as a relative difference on
* the cost function value between the two last iterations
* @return number of inner loop iterations performed (cumulated
* across outer loop iterations)
* @exception LocalException if we come accross a singularity or if
* we exceed the maximal number of iterations
*/
public int minimize(int iterMax,
double innerThreshold, double outerThreshold)
throws LocalException {
computeCost();
if ((J < 1.0e-10) || (Math.sqrt(dJdx * dJdx + dJdy * dJdy) < 1.0e-10)) {
// we consider we are already at a local minimum
return 0;
}
double previousJ = J;
double previousU = 0.0, previousV = 0.0;
double previousDJdx = 0.0, previousDJdy = 0.0;
for (int iterations = 0; iterations < iterMax;) {
// search direction
double u = -dJdx;
double v = -dJdy;
if (iterations != 0) {
// Polak-Ribiere coefficient
double beta =
(dJdx * (dJdx - previousDJdx) + dJdy * (dJdy - previousDJdy))
/ (previousDJdx * previousDJdx + previousDJdy * previousDJdy);
u += beta * previousU;
v += beta * previousV;
}
previousDJdx = dJdx;
previousDJdy = dJdy;
previousU = u;
previousV = v;
// rough minimization along the search direction
double innerJ;
do {
innerJ = J;
double lambda = newtonStep(u, v);
center.x += lambda * u;
center.y += lambda * v;
updateRadius();
computeCost();
} while ((++iterations < iterMax)
&& ((Math.abs(J - innerJ) / J) > innerThreshold));
// global convergence test
if ((Math.abs(J - previousJ) / J) < outerThreshold) {
return iterations;
}
previousJ = J;
}
throw new LocalException("unable to converge after "
+ iterMax + " iterations");
}
/** Compute the cost function and its gradient.
* <p>The results are stored as instance attributes.</p>
*/
private void computeCost() throws LocalException {
J = 0;
dJdx = 0;
dJdy = 0;
for (int i = 0; i < points.length; ++i) {
double dx = points[i].x - center.x;
double dy = points[i].y - center.y;
double di = Math.sqrt(dx * dx + dy * dy);
if (di < 1.0e-10) {
throw new LocalException("cost singularity:"
+ " point at the circle center");
}
double dr = di - rHat;
double ratio = dr / di;
J += dr * (di + rHat);
dJdx += dx * ratio;
dJdy += dy * ratio;
}
dJdx *= 2.0;
dJdy *= 2.0;
}
/** Compute the length of the Newton step in the search direction.
* @param u abscissa of the search direction
* @param v ordinate of the search direction
* @return value of the step along the search direction
*/
private double newtonStep(double u, double v) {
// compute the first and second derivatives of the cost
// along the specified search direction
double sum1 = 0, sum2 = 0, sumFac = 0, sumFac2R = 0;
for (int i = 0; i < points.length; ++i) {
double dx = center.x - points[i].x;
double dy = center.y - points[i].y;
double di = Math.sqrt(dx * dx + dy * dy);
double coeff1 = (dx * u + dy * v) / di;
double coeff2 = di - rHat;
sum1 += coeff1 * coeff2;
sum2 += coeff2 / di;
sumFac += coeff1;
sumFac2R += coeff1 * coeff1 / di;
}
// step length attempting to nullify the first derivative
return -sum1 / ((u * u + v * v) * sum2
- sumFac * sumFac / points.length
+ rHat * sumFac2R);
}
/** Get the circle center.
* @return circle center
*/
public Point2D.Double getCenter() {
return center;
}
/** Get the circle radius.
* @return circle radius
*/
public double getRadius() {
return rHat;
}
/** Local exception class for algorithm errors. */
public static class LocalException extends Exception {
/** Build a new instance with the supplied message.
* @param message error message
*/
public LocalException(String message) {
super(message);
}
}
/** Current circle center. */
private Point2D.Double center;
/** Current circle radius. */
private double rHat;
/** Circular ring sample points. */
private Point2D.Double[] points;
/** Current cost function value. */
private double J;
/** Current cost function gradient. */
private double dJdx;
private double dJdy;
}
编辑 一旦你有了一个最佳拟合圆,你就可以缩小它直到满足某种内点与外点的比例。或者,您可以删除圆外的所有点并再次运行最佳拟合圆算法,重复此过程,直到您得到满意的答案。
关于algorithm - 寻找二维点云的内圆/椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31646720/
我有 n 个圆圈,它们必须完美地围绕着一个椭圆,如下图所示: 在这张图片中,我需要找出椭圆周围每个圆的位置,并且还能够计算出完全适合这些周围圆的椭圆。 我知道的信息是每个圆的半径(都一样),以及圆的个
如何在three.js 中创建一个椭圆? 我看过这个: Drawing an ellipse in THREE.js 但是如果有人可以提供一个工作示例会很酷。 我试过这个: ellipse = new
我将使用哪种 SKPhysicsBody 主体类型来创建椭圆物理主体? 我知道我可以用直线做一条曲线,只是让它不是一个真正的椭圆,但似乎必须有某种方法来挤压一个圆或创建一个圆? 最佳答案 Create
我有一堆二维点。您可以在左图中看到它们。它们形成了某种带有几只兔子耳朵的环。我的目标是找到大的内循环/椭圆,您可以在右侧看到它。 什么样的算法对这种情况有用。 我尝试了 RANSAC 算法的变体(取
这个问题在这里已经有了答案: Ellipsis in the middle of a text (Mac style) (16 个答案) 关闭 7 年前。
我是 XAML 新手。我遇到了这个问题,我需要将符号图标或字体图标放入椭圆形状(因此它显示为圆圈内的图标)。但是,似乎 Ellipse 的 Fill 属性只需要 ImageBrush 和 ColorB
我是 OpenCV 的新手,正在学习进行一些图像处理。作为我项目的一部分,我遇到了将椭圆形式的图像补丁变形为目标椭圆的问题。据我所知,我需要计算两个补丁之间的仿射变换,然后将此变换扭曲到目标补丁中。浏
我希望能够使用 Graphics2D 实例在 BufferedImage 上绘图,并在形状的外部 填充颜色。如果这是矩形之类的形状,那会很容易,但我需要使用的形状是圆形。 用颜色填充一个圆很容易,只需
我正在尝试在 box2D (Cocos2D) 中创建一个椭圆对象。到目前为止,我已经为此使用了 b2CircleShape,但我意识到它不会再削减它了,我必须拥有椭圆形的 body 。可能吗?我试过使
我只是想知道是否有一种方法可以像我在绘画中制作的这个例子那样用拇指制作椭圆 slider : 现在我正在使用 style 但只适用于水平的silders。这是示例代码:
本文讲述了java实现画线、矩形、椭圆、字符串功能的实例代码。分享给大家供大家参考,具体如下: ?
我一直在阅读有关将圆拟合到数据的一些方法(如 this )。我想看看这些方法如何处理真实数据并考虑使用 R 来实现这一点。我尝试在 rseek 中搜索可以帮助解决此问题的软件包,但没有找到任何有用的信
当椭圆不使用此公式1旋转时。如果value = 1-指向椭圆,如果value> 1-外部,如果value #include #include struct Pt {int x, y;
已尝试搜索,但找不到任何内容。 我正在尝试使用数组和 for 循环绘制多个 2D 椭圆,我每秒都会重新绘制框架。问题是,我每次重新绘制时只得到一个椭圆,有人可以告诉我我的代码有什么问题吗? impor
是否可以在 Altair 图表内按照图表 x 和 y 变量的测量单位绘制线条和几何形状?图表可能是多面的,形状和线条取决于每个特定面中的数据。 可重现的示例: import pandas as pd
我有两个系列及其交点。我想在图表上有一个椭圆形(椭圆形),中心位于交叉点。应根据轴单位设置椭圆半径,以显示每个轴的感兴趣区域。 Highcharts.chart('container', {
有没有办法调整 Highcharts 中轴标签的行高?有时,对于断行标签,可能会出现重叠/间距问题,如果可以降低行高,这些问题会得到缓解。 正如您在下图中较长的红色标签中所见,自定义行高会很有帮助。有
我想在 android 中画一个椭圆/圆,但我无法让它显示出来。我可以使用低效的循环和 glVertex 在 C 的 OpenGL 中(而不是在 android 中)做得很好,但是 OpenGL ES
本文实例讲述了Android编程开发之在Canvas中利用Path绘制基本图形的方法。分享给大家供大家参考,具体如下: 在Android中绘制基本的集合图形,本程序就是自定义一个View组件,程序
<ellipse> 元素可以画一个椭圆 SVG 椭圆 - <ellipse> <ellipse> 元素可以画一个椭圆 椭圆与圆很相似。不同之处在于椭圆有不同的
我是一名优秀的程序员,十分优秀!