How To Access The Segments Of The Image

2019-09-11 08:21发布

I would like to extract color, shape and texture features of superpixel segments of an image . Then, I would like to visualize those features in order to select the important features.

I am using the code at this link: https://github.com/np-csu/SLIC-superpixel

I would like to access each cluster of the segmented image like this study: http://www.pyimagesearch.com/2014/12/29/accessing-individual-superpixel-segmentations-python/

However, I couldn' t find the related part of the code.

1条回答
Bombasti
2楼-- · 2019-09-11 08:23

The method int* SLIC::GetLabel() returns the label for each pixel. You can create a Mat header for the int* for easy access:

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

Then you can create a mask for each superpixel (label):

Mat1b superpixel_mask = labelImg == label;

and retrieve the superpixel in the original image:

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

Then you can compute whatever statistic you need.

Here the full code for reference:

#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;
}
查看更多
登录 后发表回答