このメソッド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;
}