【发布时间】:2020-12-21 06:58:12
【问题描述】:
亲爱的:我没有使用 MATLAB 的经验,但有一些使用 Python 的经验。我正在尝试将 MATLAB CircleFitByKasa 函数转换为 Python。
CircleFitByKasa函数代码如下:
function Par = CircleFitByKasa(XY)
%--------------------------------------------------------------------------
%
% Simple algebraic circle fit (Kasa method)
% I. Kasa, "A curve fitting procedure and its error analysis",
% IEEE Trans. Inst. Meas., Vol. 25, pages 8-14, (1976)
%
% Input: XY(n,2) is the array of coordinates of n points x(i)=XY(i,1), y(i)=XY(i,2)
%
% Output: Par = [a b R] is the fitting circle:
% center (a,b) and radius R
%
%--------------------------------------------------------------------------
P = [XY ones(size(XY,1),1)] \ [XY(:,1).^2 + XY(:,2).^2];
Par = [P(1)/2 , P(2)/2 , sqrt((P(1)^2+P(2)^2)/4+P(3))];
end % CircleFitByKasa
我已经为同一个MATLAB函数翻译了两个python代码,python代码如下所示。
import numpy as np
import os
import sys
import open3d as o3d
import math
pcd = o3d.io.read_point_cloud('C:\\Users\\wilso\\python\\datasets\\PCD\\rail_pcd_points.pcd')
XY=np.asarray(pcd.points)[:,:2]
def circiebykasa1(XY):
P=np.linalg.solve(np.concatenate((XY, np.ones((len(XY),1))), axis=1), (XY[:,0]**2 + XY[:,1]**2))
Par=(P[0]/2 , P[1]/2 , np.sqrt((np.power(P[0],2)+np.power(P[1],2))/4+P[2]))
return Par
def circiebykasa2(XY):
P=np.linalg.lstsq(np.concatenate((XY, np.ones((len(XY),1))), axis=1),(np.power(XY[:,0],2) + np.power(XY[:,1],2)))
Par=(P[0]/2 , P[1]/2 , np.sqrt((np.power(P[0],2)+np.power(P[1],2))/4+P[2]))
return Par
翻译后的 Python 代码可以在没有任何回溯的情况下运行,但结果对我来说似乎有点连贯。根据MATLAB代码中的注释,MATLAB函数应该返回拟合圆的中心(a,b)和半径r;因此,我假设a、b 和r 应该是单个数字(因为输入数组(XY) 是二维的)。但是,我翻译的 Python 函数都显示了以下结果。
(array([ -49.44680817, -65.29780001, -974.3765832 ]),
array([1.47859859e+09]),
array([1.47859859e+09, 1.47859859e+09, 1.47859859e+09]))
根据结果,我的参数a和r是3D坐标,没有太大意义。任何人都可以看看,让我知道我哪里做错了吗?
【问题讨论】:
标签: python arrays matlab numpy