matlabimage-processingimage-segmentationsuperpixels

Specifying value of each pixel in a superpixel


I could specify superpixels for an image an their properties.

  L = superpixels(A, 200);
    K=regionprops(L, 'PixelIdxList');

I know that mean intensity value of each superpixel could be specified as follows:

K=regionprops(L, 'MeanIntensity')

The question is how it is possible to specify values of all pixels within a superpixel?


Solution

  • The syntax for getting a list of all pixel values within each label is K = regionprops(L, A, 'PixelValues'). But this only works for grey-value A.

    The simplest solution is to iterate over the channels, and call the above function for each channel:

    A = imread('~/tmp/boat.tiff'); % whatever RGB image
    L = superpixels(A, 200);
    
    n = size(A,3); % number of channels, typically 3
    K = cell(max(L(:)),n);
    for ii=1:n
       tmp = regionprops(L, A(:,:,ii), 'PixelValues');
       K(:,ii) = {tmp.PixelValues};
    end
    

    We now have a cell array K that contains the values for each labeled pixel: K{lab,1} is the set of values for the pixels labeled lab, for the first channel.

    The following code collates the components of each pixel into a single array:

    K2 = cell(size(K,1),1);
    for ii=1:numel(K2)
       K2{ii} = [K{ii,:}];
    end
    

    Now K2 contains RGB arrays of data: K{lab} is a Nx3 matrix with RGB values for each of the N pixels labeled lab.