matlabimage-processingimage-segmentationvlfeat

VL_SLIC MATLAB Output for VL_FEAT


I am using the VL_SLIC function in MATLAB and I am following the tutorial for the function here: http://www.vlfeat.org/overview/slic.html

This is the code I have written so far:

im = imread('slic_image.jpg');
regionSize = 10 ;
regularizer = 10;
vl_setup;
segments = vl_slic(single(im), regionSize, regularizer);
imshow(segments);

I just get a black image and I am not able to see the segmented image with the superpixels. Is there a way that I can view the result as shown in the webpage?


Solution

  • The reason why is because segments is actually a map that tells you which regions of your image are superpixels. If a pixel in this map belongs to ID k, this means that this pixel belongs to superpixel k. Also, the map is of type uint32 and so when you try doing imshow(segments); it really doesn't show anything meaningful. For that image that is seen on the website, there are 1023 segments given your selected parameters. As such, the map spans from 0 to 1023. If want to see what the segments look like, you could do imshow(segments,[]);. What this will do is that the region with the ID of 1023 will get mapped to white, while the pixels that don't belong to any superpixel region (ID of 0), gets mapped to black. You would actually get something like this:

    enter image description here

    Not very meaningful! Now, to get what you see on the webpage, you're going to have to do a bit more work. From what I know, VLFeat doesn't have built-in functionality that shows you the results like what is seen on their webpage. As such, you will have to write code to do it yourself. You can do this by following these steps:

    1. Create a map that is true that is the same size as the image
    2. For each superpixel region k:
      • Create another map that marks true for any pixel belonging to the region k, and false otherwise.
      • Find the perimeter of this region.
      • Set these perimeter pixels to false in the map created in Step #1
    3. Repeat Step #2 until we have finished going through all of the regions.
    4. Use this map to mask out all of the pixels in the original image to get what you see in the website.

    Let's go through that code now. Below is the setup that you have established:

    vl_setup;
    im = imread('slic_image.jpg');
    regionSize = 10 ;
    regularizer = 10  ;
    segments = vl_slic(single(im), regionSize, regularizer);
    

    Now let's go through that algorithm that I just mentioned:

    perim = true(size(im,1), size(im,2));
    for k = 1 : max(segments(:))
        regionK = segments == k;
        perimK = bwperim(regionK, 8);
        perim(perimK) = false;
    end
    
    perim = uint8(cat(3,perim,perim,perim));
    
    finalImage = im .* perim;
    imshow(finalImage);
    

    We thus get:

    enter image description here

    Bear in mind that this is not exactly the same as what you get on the website. I simply went to the website and saved that image, then proceeded with the code I just showed you. This is probably because the slic_image.jpg image is not the exact original that was given in their example. There seems to be superpixels in areas where there are some bad quantization artifacts. Also, I'm using a relatively old version of VLFeat - Version 0.9.16. There may have been improvements to the algorithm since then, so I may not be using the most up to date version. In any case, this is something for you that you can start with.

    Hope this helps!