2016-07-01 6 views
1

私はcv :: ximgproc :: SuperpixelSLIC opencv C++を使ってイメージのセグメントを生成しています。私は各セグメントのラベルを一意にしたい。ここに私のコードです。ラベル0を2つのセグメントに与えられていることを私が観察しlabel.txtファイルでSuperpixelSLICでセグメントの一意のラベルを見つける方法

Mat segmentImage() { 
    int num_iterations = 4; 
    int prior = 2; 
    bool double_step = false; 
    int num_levels = 10; 
    int num_histogram_bins = 5; 

    int width, height; 

    width = h1.size().width; 
    height = h1.size().height; 

    seeds = createSuperpixelSLIC(h1); 

    Mat mask; 

    seeds->iterate(num_iterations); 

    Mat labels; 
    seeds->getLabels(labels); 
    for (int i = 0; i < labels.rows; i++) { 
     for (int j = 0; j < labels.cols; j++) { 
      if (labels.at<int>(i, j) == 0) 
       cout << i << " " << j << " " << labels.at<int>(i, j) << endl; 

     } 
    } 
    ofstream myfile; 
    myfile.open("label.txt"); 
    myfile << labels; 
    myfile.close(); 

    seeds->getLabelContourMask(mask, false); 
    h1.setTo(Scalar(0, 0, 255), mask); 

    imshow("result", h1); 
    imwrite("result.png", h1); 
    return labels; 
} 

(すなわちセグメントは、ピクセル(0,0)と画素(692442)が含まれる。これらの二つのセグメントはかなり遠く離れている。

です

答えて

1

あなたが本質的に必要とするのは、接続されたコンポーネントのアルゴリズムです。あなたが使っている正確なSLIC実装を知らなければ、SLICは通常、接続されていないスーパーピクセルを生成する傾向があります。つまり、同じラベルを持つセグメントが切断されています。私が使用した簡単な解決策は、connecここにはhttps://github.com/davidstutz/matlab-multi-label-connected-components(元々はここから:http://xenia.media.mit.edu/~rahimi/connected/)のアルゴリズム形式があります。このリポジトリにはMatLabラッパーが含まれています。 relabelConnectedSuperpixelsを実行して得られたラベル、オン

#include "connected_components.h" 
// ... 

void relabelSuperpixels(cv::Mat &labels) { 

    int max_label = 0; 
    for (int i = 0; i < labels.rows; i++) { 
     for (int j = 0; j < labels.cols; j++) { 
      if (labels.at<int>(i, j) > max_label) { 
       max_label = labels.at<int>(i, j); 
      } 
     } 
    } 

    int current_label = 0; 
    std::vector<int> label_correspondence(max_label + 1, -1); 

    for (int i = 0; i < labels.rows; i++) { 
     for (int j = 0; j < labels.cols; j++) { 
      int label = labels.at<int>(i, j); 

      if (label_correspondence[label] < 0) { 
       label_correspondence[label] = current_label++; 
      } 

      labels.at<int>(i, j) = label_correspondence[label]; 
     } 
    } 
} 

int relabelConnectedSuperpixels(cv::Mat &labels) { 

    relabelSuperpixels(labels); 

    int max = 0; 
    for (int i = 0; i < labels.rows; ++i) { 
     for (int j = 0; j < labels.cols; ++j) { 
      if (labels.at<int>(i, j) > max) { 
       max = labels.at<int>(i, j); 
      } 
     } 
    } 

    ConnectedComponents cc(2*max); 

    cv::Mat components(labels.rows, labels.cols, CV_32SC1, cv::Scalar(0)); 
    int component_count = cc.connected<int, int, std::equal_to<int>, bool>((int*) labels.data, (int*) components.data, labels.cols, 
      labels.rows, std::equal_to<int>(), false); 

    for (int i = 0; i < labels.rows; i++) { 
     for (int j = 0; j < labels.cols; j++) { 
      labels.at<int>(i, j) = components.at<int>(i, j); 
     } 
    } 

    // component_count would be the NEXT label index, max is the current highest! 
    return component_count - max - 1; 
} 

:あなたのケースではあなただけの次のコードと一緒にconnected_components.hを必要としています。

+0

ありがとう 'David。 Btw私はすでにSLICアルゴリズムのこのタイプを削除するためにDFSを使用して私自身の接続コンポーネントアルゴリズムを書き留めている – rajatV

関連する問題