vl_quickseg.m
Upload User: yangacai01
Upload Date: 2020-01-24
Package Size: 7747k
Code Size: 2k
Category:

Special Effects

Development Platform:

Matlab

  1. function [Iseg labels map gaps E] = vl_quickseg(I, ratio, kernelsize, maxdist)
  2. % VL_QUICKSEG Produce a quickshift segmentation of a grayscale or color image
  3. %   [ISEG LABELS MAPS GAPS E] = VL_QUICKSEG(I, RATIO, KERNELSIZE, MAXDIST)
  4. % Produces a Quickshift segmentation of an image. See VL_QUICKSHIFT for more
  5. % details.
  6. %
  7. % Inputs:
  8. %   I           Input image, may be RGB or Grayscale. RGB images are first
  9. %               converted to LAB.
  10. %   RATIO       Tradeoff between spatial consistency and color consistency.
  11. %               Small ratio gives more importance to the spatial component.
  12. %               Note that distance calculations happen in unnormalized image
  13. %               coordinates, so RATIO should be adjusted to compensate for
  14. %               larger images.  
  15. %   KERNELSIZE  The standard deviation of the parzen window density estimator.
  16. %   MAXDIST     The maximum distance between nodes in the quickshift tree. Used
  17. %               to cut links in the tree to form the segmentation.
  18. %
  19. % Outputs:
  20. %   ISEG   A color image where each pixel is labeled by the mean color in its
  21. %          region.
  22. %   LABELS A labeled image where the number corresponds to the cluster identity
  23. %   MAP    MAP as returned by VL_QUICKSHIFT: For each pixel, the pointer to the
  24. %          nearest pixel which increases the estimate of the density
  25. %   GAPS   GAPS as returned by VL_QUICKSHIFT: For each pixel, the distance to
  26. %          the nearest pixel which increases the estimate of the density
  27. %   E      E as returned by VL_QUICKSHIFT: The estimate of the density 
  28. I = im2double(I);
  29. % Add less than one pixel noise to break ties caused by constant regions in an
  30. % arbitrary fashon
  31. I = I + rand(size(I))/2550;
  32. if size(I,3) == 1
  33.   Ix = ratio * I;
  34. else
  35.   Ix = ratio * vl_xyz2lab(vl_rgb2xyz(I));
  36.   %Ix = Ix(:,:,2:3); % Throw away L
  37. end
  38. % Perform quickshift to obtain the segmentation tree, which is already cut by
  39. % maxdist. If a pixel has no nearest neighbor which increases the density, its
  40. % parent in the tree is itself, and gaps is inf.
  41. [map,gaps,E] = vl_quickshift(Ix, kernelsize, maxdist) ;
  42. % Follow the parents of the tree until we have reached the root nodes
  43. % mapped: a labeled segmentation where the labels are the indicies of the modes
  44. % in the original image.
  45. % labels: mapped after having been renumbered 1:nclusters and reshaped into a
  46. % vector
  47. [mapped labels] = vl_flatmap(map) ;
  48. labels = reshape(labels, size(map));
  49. % imseg builds an average description of the region by color
  50. Iseg = vl_imseg(I, labels);