拟合圆:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
from numpy import *
from scipy import optimize
import functools
def countcalls(fn):
@functools.wraps(fn)
def wrapped(*args):
wrapped.ncalls +=1
return fn(*args)
wrapped.ncalls = 0
return wrapped
def calc_R(xc, yc):
return sqrt((xx - xc) ** 2 + (yy - yc) ** 2)
@countcalls
def f_2(c):
Ri = calc_R(*c)
return Ri - Ri.mean()
def nihe(xx,yy):
# 质心坐标
x_m = mean(xx)
y_m = mean(yy)
#圆心估计
center_estimate = x_m, y_m
center_2, _ = optimize.leastsq(f_2, center_estimate)
xc_2, yc_2 = center_2
Ri_2 = calc_R(xc_2, yc_2)
#拟合圆的半径
R_2 = Ri_2.mean()
residu_2 = sum((Ri_2 - R_2)**2)
residu2_2 = sum((Ri_2**2-R_2**2)**2)
ncalls_2= f_2.ncalls
#输出列表
# fmt = '%-22s %10.5f %10.5f %10.5f %10d %10.6f %10.6f %10.
|