找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2514|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

ROS中激光雷達(dá)數(shù)據(jù)處理之特征提取 源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
   ROS中激光雷達(dá)的數(shù)據(jù)就是一串距離值,每隔1度一個(gè)距離值(具體情況得看激光雷達(dá)的參數(shù)),通過實(shí)測(cè)激光雷達(dá)的數(shù)據(jù)提取關(guān)鍵特征,直線,圓弧

  1. // 進(jìn)行多邊形擬合: Points : 輪廓上的點(diǎn)      n -- 輪廓點(diǎn)數(shù)目  Eps -- 擬合精度
  2. // 返回值: 若該輪廓段需要分段,則返回分段點(diǎn)在該輪廓點(diǎn)列中的索引,否則,返回 0 表示不需要分段
  3. // 這里是整個(gè)算法計(jì)算復(fù)雜性最大的一個(gè)地方
  4. // 為了提高程序運(yùn)行效率,對(duì)點(diǎn)到直線的距離計(jì)算進(jìn)行改進(jìn):
  5. // 多邊形擬合中的直線是由點(diǎn)列中的點(diǎn)決定的
  6. // 為了計(jì)算點(diǎn)到直線的距離,
  7. // 采用坐標(biāo)系旋轉(zhuǎn),將直線旋轉(zhuǎn)到x軸方向,這樣點(diǎn)到直線的距離即為各個(gè)點(diǎn)
  8. // 在坐標(biāo)旋轉(zhuǎn)后的y值的絕對(duì)值
  9. // 同時(shí),坐標(biāo)旋轉(zhuǎn)矩陣在該次運(yùn)算中為定值,只需一次計(jì)算,不需要多次的開方或三角計(jì)算
  10. int OpenRadar::PolyContourFit( int* X, int* Y, int n , float Eps ) // 根據(jù)輪廓點(diǎn),用多邊形擬合該輪廓點(diǎn)   
  11. {
  12.     double dis = sqrt((double)(((X[0] - X[n - 1])*(X[0] - X[n - 1])) +  
  13.                      ((Y[0] - Y[n - 1])* (Y[0] - Y[n - 1]))));
  14.     double cosTheta = (X[n- 1] - X[0]) / dis;
  15.     double sinTheta = - ( Y[n- 1] - Y[0] )/dis;
  16.     double MaxDis = 0;
  17.     int i ;
  18.     int MaxDisInd = -1;
  19.     double dbDis;
  20.     for(i = 1 ; i < n - 1 ; i++)
  21.     {
  22.         // 進(jìn)行坐標(biāo)旋轉(zhuǎn),求旋轉(zhuǎn)后的點(diǎn)到x軸的距離
  23.         dbDis = abs( (Y[i] - Y[0]) * cosTheta + (X[i] - X[0])* sinTheta);
  24.         if( dbDis > MaxDis)
  25.         {
  26.             MaxDis = dbDis;
  27.             MaxDisInd = i;
  28.         }
  29.     }
  30.     if(MaxDis > Eps)
  31.     {
  32.         return MaxDisInd;
  33.         //        cout << "Line 1 : " << endl;
  34.         //        cout << "Start :" << Points[0].x << "  " << Points[0].y  << " --- " << Points[MaxDisInd].x << "  " << Points[MaxDisInd].y << endl;
  35.         //        cout << "角度: "<<180 * atan2(Points[0].y - Points[MaxDisInd].y , Points[0].x - Points[MaxDisInd].x ) / 3.1415926;
  36.         //        cout << "Line 2 :" << endl;
  37.         //        cout << "Start :" << Points[MaxDisInd].x << "  " << Points[MaxDisInd].y  << " --- " << Points[n - 1].x << "  " << Points[n - 1].y << endl;
  38.         //        cout << "角度: "<< 180 * atan2(Points[n - 1].y - Points[MaxDisInd].y , Points[n - 1].x - Points[MaxDisInd].x ) / 3.1415926;
  39.     }
  40.     //    else{
  41.     //        cout << "Line 1 : " << endl;
  42.     //        cout << "Start :" << Points[0].x << "  " << Points[0].y  << " --- " << Points[n - 1].x << "  " << Points[n - 1].y << endl;
  43.     //        cout << "角度: "<<180 * atan2(Points[n - 1].y - Points[0].y , Points[n - 1].x - Points[0].x ) / 3.1415926;

  44.     //    }
  45.     return 0;
  46. }

  47. //將折線拆成兩段
  48. int OpenRadar::BreakPolyLine(vector<int>& BreakedRadarRho,
  49.                              vector<double>& BreakedRadarTheta,
  50.                              vector<int>& SepRadarRho ,   
  51.                              vector<double>&SepRadarTheta)
  52. {
  53.     int rho = 0;
  54.     double theta = 0.0;
  55.     int X[1200] = {0};
  56.     int Y[1200] = {0};
  57.     int rhoCopy[1200] = {0};
  58.     double thetaCopy[1200] = {0};
  59.     int pointCnt = 0;
  60.     int lineCnt = 0;
  61.     int N = 0;
  62.     SepRadarRho.clear();
  63.     SepRadarTheta.clear();
  64.     Corners.clear();

  65.     //進(jìn)行多次迭代,將所有的折線都拆分成直線段
  66.    
  67.    vector<int>CornerIndex;
  68.    int CornerCnt = 0;
  69.    int tempIndex = 0;
  70.     for (int i = 0; i < static_cast<int>(BreakedRadarRho.size());i++)
  71.     {
  72.         rho   = BreakedRadarRho.at(i);
  73.         theta = BreakedRadarTheta.at(i);

  74.         if (rho < 0)
  75.         {
  76.             if (pointCnt > 200)//數(shù)目比較少的點(diǎn)直接拋棄
  77.             {
  78.                 CornerIndex.clear();
  79.                 CornerCnt = FindCorners(CornerIndex,X,Y,0,pointCnt,200);

  80.                 if (CornerIndex.size() == 0)
  81.                 {
  82.                     for (int k = 0 ; k < pointCnt;k++)
  83.                     {
  84.                         SepRadarRho.push_back(rhoCopy[k]);
  85.                         SepRadarTheta.push_back(thetaCopy[k]);
  86.                     }
  87.                     SepRadarRho.push_back(-1);
  88.                     SepRadarTheta.push_back(1000.0);
  89.                     lineCnt++;
  90.                 }else
  91.                 {
  92.                     tempIndex = 0;
  93.                     for (int k = 0 ; k < pointCnt;k++)
  94.                     {
  95.                         SepRadarRho.push_back(rhoCopy[k]);
  96.                         SepRadarTheta.push_back(thetaCopy[k]);
  97.                         if (k == CornerIndex.at(tempIndex))
  98.                         {
  99.                             SepRadarRho.push_back(-1);
  100.                             SepRadarTheta.push_back(1000.0);
  101.                             lineCnt++;
  102.                             if (tempIndex < static_cast<int>(CornerIndex.size()) -1)
  103.                             {
  104.                                 tempIndex++;
  105.                             }  
  106.                         }
  107.                     }
  108.                     SepRadarRho.push_back(-1);
  109.                     SepRadarTheta.push_back(1000.0);
  110.                     lineCnt++;
  111.                 }
  112.             }
  113.             pointCnt = 0;
  114.             continue;
  115.         }
  116.         X[pointCnt] = static_cast<int>(rho*cos(theta));
  117.         Y[pointCnt] = static_cast<int>(rho*sin(theta));
  118.         rhoCopy[pointCnt]   = rho;
  119.         thetaCopy[pointCnt] = theta;
  120.         pointCnt++;
  121.     }
  122.    
  123.     //cout<<"lineCnt: "<<lineCnt<<endl;
  124.     return lineCnt;
  125. }

  126. //進(jìn)行直線擬合
  127. void OpenRadar::FitLine(vector<LinePara>& FittedLine,vector<int>& RadarRho,vector<double>& RadarTheta){

  128.     int rho = 0;
  129.     double theta = 0.0;
  130.     int X[1200] = {0};
  131.     int Y[1200] = {0};
  132.     int pointCnt = 0;
  133.     LinePara tmpLinePara;
  134.     FittedLine.clear();
  135.     for (int i = 0 ; i < static_cast<int>(RadarRho.size());i++)
  136.     {
  137.         rho = RadarRho.at(i);
  138.         theta = RadarTheta.at(i);

  139.         if (rho < 0)
  140.         {
  141.             if (pointCnt > 100 )//點(diǎn)數(shù)目足夠多的點(diǎn)列才進(jìn)行直線擬合
  142.             {
  143.                 WeightedFit(X ,Y ,pointCnt,&tmpLinePara);
  144.                 FittedLine.push_back(tmpLinePara);

  145.                 //存儲(chǔ)擬合的數(shù)據(jù)和擬合結(jié)果
  146.        /*         FILE* pF = fopen("line.txt","w");
  147.                 fprintf(pF,"[a]\n");
  148.                 fprintf(pF,"%f\n",tmpLinePara.a);
  149.                 fprintf(pF,"[b]\n");
  150.                 fprintf(pF,"%f\n",tmpLinePara.b);
  151.                 fprintf(pF,"[x]\n");
  152.                 for (int j = 0; j < pointCnt ;j++)
  153.                 {
  154.                     fprintf(pF,"%d,",X[j]);
  155.                 }
  156.                 fprintf(pF,"\n[y]\n");
  157.                 for (int j = 0; j < pointCnt ;j++)
  158.                 {
  159.                     fprintf(pF,"%d,",Y[j]);
  160.                 }
  161.                 fclose(pF);*/

  162.                 pointCnt = 0;
  163.                 continue;
  164.             }else {
  165.                 pointCnt = 0;
  166.                 continue;
  167.             }   
  168.         }

  169.         X[pointCnt] = static_cast<int>(rho*cos(theta));
  170.         Y[pointCnt] = static_cast<int>(rho*sin(theta));
  171.         pointCnt++;
  172.     }
  173.    /* for (int i = 0; i < FittedLine.size();i++)
  174.     {
  175.         cout<<"a: "<<FittedLine.at(i).a<<"  b: "<<FittedLine.at(i).b<<" ";
  176.         cout<<"x1: "<<FittedLine.at(i).startPoint.x<<" "
  177.             <<"y1: "<<FittedLine.at(i).startPoint.y<<" "
  178.             <<"x1: "<<FittedLine.at(i).endPoint.x<<" "
  179.             <<"y1: "<<FittedLine.at(i).endPoint.y<<endl;
  180.     }*/

  181. }
復(fù)制代碼


以上代碼: OpenRadar7.0.zip (25.96 KB, 下載次數(shù): 13)
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表