python opencv 圆形roi

x33g5p2x  于2021-12-28 转载在 Python  
字(0.6k)|赞(0)|评价(0)|浏览(483)

拟合圆:

  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import cv2
  4. from numpy import *
  5. from scipy import optimize
  6. import functools
  7. def countcalls(fn):
  8. @functools.wraps(fn)
  9. def wrapped(*args):
  10. wrapped.ncalls +=1
  11. return fn(*args)
  12. wrapped.ncalls = 0
  13. return wrapped
  14. def calc_R(xc, yc):
  15. return sqrt((xx - xc) ** 2 + (yy - yc) ** 2)
  16. @countcalls
  17. def f_2(c):
  18. Ri = calc_R(*c)
  19. return Ri - Ri.mean()
  20. def nihe(xx,yy):
  21. # 质心坐标
  22. x_m = mean(xx)
  23. y_m = mean(yy)
  24. #圆心估计
  25. center_estimate = x_m, y_m
  26. center_2, _ = optimize.leastsq(f_2, center_estimate)
  27. xc_2, yc_2 = center_2
  28. Ri_2 = calc_R(xc_2, yc_2)
  29. #拟合圆的半径
  30. R_2 = Ri_2.mean()
  31. residu_2 = sum((Ri_2 - R_2)**2)
  32. residu2_2 = sum((Ri_2**2-R_2**2)**2)
  33. ncalls_2= f_2.ncalls
  34. #输出列表
  35. # fmt = '%-22s %10.5f %10.5f %10.5f %10d %10.6f %10.6f %10.

相关文章