2016-04-18 125 views
0

我試圖在n維數據上運行kmeans算法。opencv多維kmeans

我有N點,每個點都有{ x, y, z, ... , n }的功能。

我的代碼如下:

cv::Mat points(N, n, CV_32F); 

// fill the data points 

cv::Mat labels; cv::Mat centers; 

cv::kmeans(points, k, labels, cv::TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 1000, 0.001), 10, cv::KMEANS_PP_CENTERS, centers); 

的問題是,k均值算法運行到分割故障。

任何幫助表示讚賞

更新

美姬和Micka如何說,上面的代碼是正確的!

我曾犯了一個錯誤「填充數據點」,讓我破壞了記憶

+0

代碼似乎沒問題。請提供[mcve] – Miki

回答

1

代碼看起來OK。您必須選擇數據作爲每列一維。

你可以嘗試運行這個例子嗎?

// k-means 
int main(int argc, char* argv[]) 
{ 

    cv::Mat projectedPointsImage = cv::Mat(512, 512, CV_8UC3, cv::Scalar::all(255)); 

    int nReferenceCluster = 10; 
    int nSamplesPerCluster = 100; 

    int N = nReferenceCluster*nSamplesPerCluster; // number of samples 
    int n = 10; // dimensionality of data 

    // fill the data points 
    // create n artificial clusters and randomly seed 100 points around them 

    cv::Mat referenceCenters(nReferenceCluster, n, CV_32FC1); 

    //std::cout << referenceCenters << std::endl; 
    cv::randu(referenceCenters, cv::Scalar::all(0), cv::Scalar::all(512)); 
    //std::cout << "FILLED:" << "\n" << referenceCenters << std::endl; 

    cv::Mat points = cv::Mat::zeros(N, n, CV_32FC1); 
    cv::randu(points, cv::Scalar::all(-20), cv::Scalar::all(20)); // seed points around the center 

    for (int j = 0; j < nReferenceCluster; ++j) 
    { 
     cv::Scalar clusterColor = cv::Scalar(rand() % 255, rand() % 255, rand() % 255); 
     //cv::Mat & clusterCenter = referenceCenters.row(j); 
     for (int i = 0; i < nSamplesPerCluster; ++i) 
     { 
      // creating a sample randomly around the artificial cluster: 
      int index = j*nSamplesPerCluster + i; 
      //samplesRow += clusterCenter; 
      for (int k = 0; k < points.cols; ++k) 
      { 
       points.at<float>(index, k) += referenceCenters.at<float>(j, k); 
      } 

      // projecting the 10 dimensional clusters to 2 dimensions: 
      cv::circle(projectedPointsImage, cv::Point(points.at<float>(index, 0), points.at<float>(index, 1)), 2, clusterColor, -1); 

     } 
    } 


    cv::Mat labels; cv::Mat centers; 
    int k = 10; // searched clusters in k-means 

    cv::kmeans(points, k, labels, cv::TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 1000, 0.001), 10, cv::KMEANS_PP_CENTERS, centers); 

    for (int j = 0; j < centers.rows; ++j) 
    { 
     std::cout << centers.row(j) << std::endl; 
     cv::circle(projectedPointsImage, cv::Point(centers.at<float>(j, 0), centers.at<float>(j, 1)), 30, cv::Scalar::all(0), 2); 
    } 

    cv::imshow("projected points", projectedPointsImage); 
    cv::imwrite("C:/StackOverflow/Output/KMeans.png", projectedPointsImage); 
    cv::waitKey(0); 
    return 0; 
} 

我在那裏創建人造聚類中心周圍的10維數據。爲了顯示我把它們投影到2D,得到這個結果:

enter image description here

+0

感謝Micka的回答。我的代碼中有這些編譯錯誤。 kmean.cpp:43:55:錯誤:類型'cv :: Mat'的非常量引用無效初始化類型'cv :: Mat'的右值 cv :: Mat&clusterCenter = referenceCenters.row(j) ; ^ kmean.cpp:50:46:錯誤:類型'cv :: Mat'的類型爲'cv :: Mat'的非常量引用無效初始化 cv :: Mat&samplesRow = points.row (指數); – thewoz

+0

嘗試更新代碼請 – Micka

+0

它完美的作品! – thewoz