opencv最小二乘法拟合平面

  1. //Ax+by+cz=D  
  2. void cvFitPlane(const CvMat* points, float* plane){  
  3.     // Estimate geometric centroid.  
  4.     int nrows = points->rows;  
  5.     int ncols = points->cols;  
  6.     int type = points->type;  
  7.     CvMat* centroid = cvCreateMat(1, ncols, type);  
  8.     cvSet(centroid, cvScalar(0));  
  9.     for (int c = 0; c<ncols; c++){  
  10.         for (int r = 0; r < nrows; r++)  
  11.         {  
  12.             centroid->data.fl[c] += points->data.fl[ncols*r + c];  
  13.         }  
  14.         centroid->data.fl[c] /= nrows;  
  15.     }  
  16.     // Subtract geometric centroid from each point.  
  17.     CvMat* points2 = cvCreateMat(nrows, ncols, type);  
  18.     for (int r = 0; r<nrows; r++)  
  19.         for (int c = 0; c<ncols; c++)  
  20.             points2->data.fl[ncols*r + c] = points->data.fl[ncols*r + c] - centroid->data.fl[c];  
  21.     // Evaluate SVD of covariance matrix.  
  22.     CvMat* A = cvCreateMat(ncols, ncols, type);  
  23.     CvMat* W = cvCreateMat(ncols, ncols, type);  
  24.     CvMat* V = cvCreateMat(ncols, ncols, type);  
  25.     cvGEMM(points2, points, 1, NULL, 0, A, CV_GEMM_A_T);  
  26.     cvSVD(A, W, NULL, V, CV_SVD_V_T);  
  27.     // Assign plane coefficients by singular vector corresponding to smallest singular value.  
  28.     plane[ncols] = 0;  
  29.     for (int c = 0; c<ncols; c++){  
  30.         plane[c] = V->data.fl[ncols*(ncols - 1) + c];  
  31.         plane[ncols] += plane[c] * centroid->data.fl[c];  
  32.     }  
  33.     // Release allocated resources.  
  34.     cvReleaseMat(¢roid);  
  35.     cvReleaseMat(&points2);  
  36.     cvReleaseMat(&A);  
  37.     cvReleaseMat(&W);  
  38.     cvReleaseMat(&V);  
  39. }  

调用的方式:

[cpp]  view plain  copy
 print ?
  1. CvMat*points_mat = cvCreateMat(X_vector.size(), 3, CV_32FC1);//定义用来存储需要拟合点的矩阵   
  2.         for (int i=0;i < X_vector.size(); ++i)  
  3.         {  
  4.             points_mat->data.fl[i*3+0] = X_vector[i];//矩阵的值进行初始化   X的坐标值  
  5.             points_mat->data.fl[i * 3 + 1] = Y_vector[i];//  Y的坐标值  
  6.             points_mat->data.fl[i * 3 + 2] = Z_vector[i];<span style="font-family: Arial, Helvetica, sans-serif;">//  Z的坐标值</span>  
  7.   
  8.         }  
  9.         float plane12[4] = { 0 };//定义用来储存平面参数的数组   
  10.         cvFitPlane(points_mat, plane12);//调用方程   
我们拟合出来的方程:Ax+By+Cz=D

其中 A=plane12[0],    B=plane12[1],   C=plane12[2],   D=plane12[3],

这是要注意的方程的表示

OpenCV中提供了函数fitLine()来实现最小二乘法拟合直线。 使用方法如下: 1. 导入必要的库和模块: ```python import cv2 import numpy as np ``` 2. 准备数据: 我们需要提供一些点的坐标,用于拟合直线。这些点可以是图像中的像素点,也可以是其他任意坐标系中的点。 ```python # 生成一些随机点 points = np.random.randint(0, 200, (10, 2)) ``` 3. 拟合直线: 使用fitLine()函数来拟合直线,该函数需要传入一些参数: - points:需要拟合的点的坐标。 - distType:点到直线的距离类型,可以选择CV_DIST_L1, CV_DIST_L2或CV_DIST_L12。 - param:累加器分辨率与rho的比值,一般取值为1。 - reps:在一条直线上的最小点数,一般取值为0。 - aeps:角度的最小精度,一般取值为0.01度。 ```python # 使用fitLine()函数拟合直线 vx, vy, x, y = cv2.fitLine(points, cv2.DIST_L2, 0, 0.01, 0.01) ``` 4. 绘制直线: 得到直线的参数后,我们可以使用这些参数来绘制直线。可以使用cv2.line()函数来实现。 ```python # 绘制直线 lefty = int((-x*vy/vx) + y) righty = int(((200-x)*vy/vx)+y) cv2.line(img, (199,righty), (0,lefty), (0,255,0), 2) ``` 完整代码如下: ```python import cv2 import numpy as np # 生成一些随机点 points = np.random.randint(0, 200, (10, 2)) # 使用fitLine()函数拟合直线 vx, vy, x, y = cv2.fitLine(points, cv2.DIST_L2, 0, 0.01, 0.01) # 绘制直线 img = np.zeros((200, 200, 3), dtype=np.uint8) lefty = int((-x*vy/vx) + y) righty = int(((200-x)*vy/vx)+y) cv2.line(img, (199,righty), (0,lefty), (0,255,0), 2) cv2.imshow("Fit Line", img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 运行结果如下图所示: ![拟合直线结果](https://img-blog.csdnimg.cn/20210622152054570.png)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值