0

バックグラウンド:

gSLICrを使用して画像の SLIC スーパーピクセルを計算しました。これは、画像スーパーピクセルの「ピクセルごとのマップ」をインデックス (0 からスーパーピクセルの数 - 1) として提供します。

const int*このマップは、インデックスを含む整数 const 配列 ( ) へのポインターです。

OpenCV を使用して、各スーパーピクセルの重心を計算したいと思います。

Matlabのバックグラウンドから来て、次を使用してこれを行いますregionprops

segments = vl_slic(myImage, regionSize, regularizer);
stats = regionprops(segments, 'Centroid');
centroids = cat(1, stats.Centroid);

OpenCV を使用してこれを行う方法がわかりません。

質問:

const int*(i)配列をに変換するにはどうすればよいcv::Matですか?

(ii) (i) の行列からスーパーピクセル重心を計算するにはどうすればよいですか?

4

1 に答える 1

0

最初の質問は答えられているようですので、2 番目の質問に焦点を当てます。次のコードを使用して、各スーパーピクセルの平均座標 (空間重心) を計算しました。

/** \brief Compute the mean coordinates of each superpixel (i.e. spatial centroids).
 * \param[in] labels a matrix of type CV_32SC1 holding the labels for each pixel
 * \param[out] means the spatial centroids (or means in y and x axes) of the superpixels
 */
void getMeans(const cv::Mat &labels, std::vector<cv::Vec2f> &means) {

    // Count superpixels or get highest superpixel index:
    int superpixels = 0;
    for (int i = 0; i < labels.rows; ++i) {
        for (int j = 0; j < labels.cols; ++j) {
            if (labels.at<int>(i, j) > superpixels) {
                superpixels = labels.at<int>(i, j);
            }
        }
    }

    superpixels++;

    // Setup means as zero vectors.
    means.clear();
    means.resize(superpixels);
    for (int k = 0; k < superpixels; k++)
    {
        means[k] = cv::Vec2f(0, 0);
    }

    std::vector<int> counts(superpixels, 0);

    // Sum y and x coordinates for each superpixel:
    for (int i = 0; i < labels.rows; ++i) {
        for (int j = 0; j < labels.cols; ++j) {
            means[labels.at<int>(i, j)][0] += i; // for computing mean i (i.e. row or y axis)
            means[labels.at<int>(i, j)][1] += j; // for computing the mean j (i.e. column or x axis)

            counts[labels.at<int>(i, j)]++;
        }
    }

    // Obtain averages by dividing by the size (=number of pixels) of the superpixels.
    for (int k = 0; k < superpixels; ++k) {
        means[k] /= counts[k];
    }
}

// Do something with the means ...

平均色も必要な場合、メソッドは引数として画像を必要としますが、残りのコードは平均色を計算するために簡単に適用できます。

于 2016-09-02T14:20:28.423 に答える