找到你要的答案

Q:VL_SLIC MATLAB Output for VL_FEAT

Q:vl_slic Matlab输出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?

我用MATLAB中的vl_slic功能和我下面的函数教程在这里:http://www.vlfeat.org/overview/slic.html

这是我迄今为止写的代码:

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

我只是一个黑色的图像,我不能够看到的超像素分割图像。有没有办法,我可以查看结果显示在网页上?

answer1: 回答1:

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:

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:

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!

原因是因为段实际上是一个地图,告诉你哪个地区你的图像处理。如果在这个地图的像素属于ID K,这意味着该像素属于超像素K,地图是UInt32类型所以当你试着做imshow(段);真的没有表现出任何有意义的。对于网站上看到的图像,有1023段给出您选择的参数。因此,地图跨度从0到1023。如果想看看这段的样子,你可以做imshow(段,[ ]);。这将是什么做的,ID为1023的地区将被映射到白色,而像素不属于任何超像素区域(编号0),被映射到黑色。你会得到这样的东西:

不是很有意义!现在,为了得到你在网页上看到的,你将不得不做更多的工作。据我所知,VLFeat没有内置的功能,显示你的结果就像是在自己的网页上看到的。因此,你必须自己编写代码。你可以按照以下步骤进行:

  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.

让我们现在通过代码。下面是你已经建立的设置:

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

现在让我们通过我刚才提到的算法:

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);

因此我们得到:

请记住,这是不完全一样,你在网站上得到的。我只是去了网站,并保存了图像,然后继续我刚才告诉你的代码。这可能是因为slic_image.jpg图像是不确切的,在他们最初给出的例子。似乎在那里有一些不好的量化效应区处理。另外,我用的版本比较老的版本0.9.16 vlfeat。从那时起,可能有改进的算法,所以我可能不使用最新的版本。在任何情况下,这是你的东西,你可以开始。

希望这有助于!

answer2: 回答2:

I found these lines in vl_demo_slic.m may be useful.

segments = vl_slic(im, regionSize, regularizer, 'verbose') ;

% overaly segmentation
[sx,sy]=vl_grad(double(segments), 'type', 'forward') ;
s = find(sx | sy) ;
imp = im ;
imp([s s+numel(im(:,:,1)) s+2*numel(im(:,:,1))]) = 0 ;

It generates edges from the gradient of the superpixel map (segments).

我在vl_demo_slic发现这些线。我可能是有用的。

segments = vl_slic(im, regionSize, regularizer, 'verbose') ;

% overaly segmentation
[sx,sy]=vl_grad(double(segments), 'type', 'forward') ;
s = find(sx | sy) ;
imp = im ;
imp([s s+numel(im(:,:,1)) s+2*numel(im(:,:,1))]) = 0 ;

它的超像素图的梯度产生的边缘(段)。

matlab  image-processing  image-segmentation  vlfeat