0

画像のスーパーピクセル セグメントの色、形状、テクスチャの特徴を抽出したいと考えています。次に、重要な機能を選択するために、それらの機能を視覚化したいと思います。

このリンクのコードを使用しています: https://github.com/np-csu/SLIC-superpixel

この研究のように、分割された画像の各クラスターにアクセスしたいと思います: http://www.pyimagesearch.com/2014/12/29/accessing-individual-superpixel-segmentations-python/

しかし、コードの関連部分が見つかりませんでした。

4

1 に答える 1

2

このメソッドint* SLIC::GetLabel()は、各ピクセルのラベルを返します。簡単にアクセスできるように、 のMatヘッダーを作成できます。int*

Mat1i labelImg(img.rows, img.cols, slic.GetLabel());

次に、スーパーピクセル (ラベル) ごとにマスクを作成できます。

Mat1b superpixel_mask = labelImg == label;

元の画像のスーパーピクセルを取得します。

Mat3b superpixel_in_img;
img.copyTo(superpixel_in_img, superpixel_mask);

その後、必要な統計を計算できます。

参照用の完全なコードは次のとおりです。

#include <opencv2/opencv.hpp>
#include "slic.h"

int main()
{
    // Load an image
    Mat3b img = imread("path_to_image");

    // Set the maximum number of superpixels
    UINT n_of_superpixels = 200;
    SLIC slic;

    // Compute the superpixels
    slic.GenerateSuperpixels(img, n_of_superpixels);

    // Visualize superpixels
    //Mat3b res = slic.GetImgWithContours(Scalar(0,0,255));

    // Get the labels
    Mat1i labelImg(img.rows, img.cols, slic.GetLabel());

    // Get the actual number of labels
    // may be less that n_of_superpixels
    double max_dlabel;
    minMaxLoc(labelImg, NULL, &max_dlabel);
    int max_label = int(max_dlabel);

    // Iterate over each label
    for (int label = 0; label <= max_label; ++label)
    {
        // Mask for each label
        Mat1b superpixel_mask = labelImg == label;

        // Superpixel in original image
        Mat3b superpixel_in_img;
        img.copyTo(superpixel_in_img, superpixel_mask);

        // Now you have the binary mask of each superpixel: superpixel_mask
        // and the superpixel in the original image: superpixel_in_img
    }

    return 0;
}
于 2015-09-29T12:58:45.717 に答える