# ITK—空间点的直线拟合

typedef  itk::Image<short, 3>      ITKImageType;
typedef ITKImageType::PointType PointType;

void FittingLine(vector<PointType> phy_point)
{
int number = phy_point.size();
double x_sum = 0;
double y_sum = 0;
double xy_sum = 0;
double x2_sum = 0;
for (int i = 0; i < phy_point.size(); i++)
{
x_sum = x_sum + phy_point[i][0];
y_sum = y_sum + phy_point[i][1];
xy_sum = xy_sum + phy_point[i][0] * phy_point[i][1];
x2_sum = x2_sum + phy_point[i][0] * phy_point[i][0];
}
double temp = number*x2_sum - x_sum*x_sum;
//y =ax+b
double a = 0;
double b = 0;
if (temp != 0)// 判断分母不为0
{
a = (number*xy_sum - x_sum*y_sum) / temp;
b = (x2_sum*y_sum - x_sum*xy_sum) / temp;
}
else
{
a = 1;
b = 0;
}

}

typedef  itk::Image<short, 3>      ITKImageType;
typedef ITKImageType::PointType PointType;

void FittingLine(vector<PointType> phy_point)
{
cout << "开始拟合轴线:" << endl;
double x_sum = 0;
double y_sum = 0;
double z_sum = 0;
double xz_sum = 0;
double yz_sum = 0;
double z2_sum = 0;

for (int i = 0; i < phy_point.size(); i++)
{
x_sum = x_sum + phy_point[i][0];
y_sum = y_sum + phy_point[i][1];
z_sum = z_sum + phy_point[i][2];
xz_sum = xz_sum + phy_point[i][0] * phy_point[i][2];
yz_sum = yz_sum + phy_point[i][1] * phy_point[i][2];
z2_sum = z2_sum + phy_point[i][2] * phy_point[i][2];
}
//cout << x_sum <<" " << y_sum<<" "<< z_sum<<" "<< xz_sum<<" "<< yz_sum<<" "<< z2_sum<<endl;

typedef itk::Matrix <double, 2, 2> MatrixType;
MatrixType A;
MatrixType B;
A(0, 0) = xz_sum;
A(0, 1) = x_sum;
A(1, 0) = yz_sum;
A(1, 1) = y_sum;

B(0, 0) = z2_sum;
B(0, 1) = z_sum;
B(1, 0) = z_sum;
B(1, 1) = phy_point.size();

MatrixType P;
P = A*B.GetInverse();
//cout << P<< endl;
vector<double> line;
line.push_back(P(0, 0));
line.push_back(P(0, 1));
line.push_back(P(1, 0));
line.push_back(P(1, 1));
return line;

}


06-03 6567

03-12 4331
10-22 1615
04-05 747
08-20 4439
10-20 468
03-26 1万+