SLIC Superpixels 算法代码学习笔记

本文记录了SLIC(Simple Linear Iterative Clustering)超像素算法的学习过程,包括超像素生成函数的入口,参数设置如种子个数、紧凑度等,并详细介绍了SLIC算法的关键步骤,如超像素大小计算、RGB到LAB转换、种子点布局、超像素聚类以及连接性约束的强制执行。
摘要由CSDN通过智能技术生成
1.主程序入口
  • 下面的程序就是超像素生成的函数入口
slic.DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(img, width, height, labels, numlabels, m_spcount, m_compactness);
这里有几个特别的参数需要说明:
  1. slic:SLIC slic; 是一个SLIC类
  2. labels:int* labels = new int[sz];一张标签图,和图像大小一致,用于标记每个像素的标签值;sz=width*height,即一张图像的像素总数。
  3. numlabels:int numlabels(0);是图像最终分成的类数,即最终生成的超像素个数,在这里被初始化为0。
  4. m_spcount: 客户从界面输入的值,即初始化的种子个数,但是SLIC算法中不一定每个种子最终都能得一个超像素,由于某些因素可能被其他超像素合并。若种子数不符合规定,则通过(总像素值SZ)/(每个超像素默认大小200)获得种子数:if (m_spcount < 20 || m_spcount > sz/4) m_spcount = sz/200;
  5. m_compactness:if(m_compactness < 1.0 || m_compactness > 80.0) m_compactness = 20.0;这个值也是有用户设定的,是颜色特征和XY坐标特征之间的紧密度比例,20这个值效果往往不错。
  • 该函数的定义
void SLIC::DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(
    unsigned int *                                   ubuff,//img
   const int                                         width,
    const int                                        height,
    int*&                                           klabels,//labels
    int&                                            numlabels, //
    const int &                                      K, //初始化的种子 m_spcount
    const double &                                   compactness) // m_compactness空间参数转换的权重值
{
    const int superpixelsize = 0.5+double(width*height)/ double(K);
    DoSuperpixelSegmentation_ForGivenSuperpixelSize(ubuff,width,height,klabels,numlabels,superpixelsize,compactness);
}
  1. superpixelsize:超像素的大小,即每个超像素中包含的像素值
  2. DoSuperpixelSegmentation_ForGivenSuperpixelSize函数中完成了超像素生成的功能
  3. const int STEP = sqrt(double(superpixelsize))+0.5;这个变量很关键,是种子点的跨度。
2.子程序流程
DoSuperpixelSegmentation_ForGivenSuperpixelSize函数中主要包含以下函数:
  • DoRGBtoLABConversion(ubuff, m_lvec, m_avec, m_bvec); 
     将RGB图像转换为LAB图像。
  • GetLABXYSeeds_ForGivenStepSize(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, STEP, perturbseeds, edgemag);
           均匀分布种子点,将种子点的5维特征值LABXY作为分类的中心点特征值存入 kseeds向量中。
  • PerformSuperpixelSLIC(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, klabels, STEP, edgemag,compactness);
          对整张图像进行局部的K-Means聚类,生成超像素。这是超像素生成的关键步骤,也耗时最多。
以下是原始slic算法的Matlab代码: function [L,NumLabels] = slic(I,k,compactness) % I: input image % k: desired number of superpixels % compactness: controls the shape of the superpixels % Output: % L: label matrix % NumLabels: number of superpixels % Convert input image to Lab color space I = double(I); [L,a,b] = RGB2Lab(I(:,:,1),I(:,:,2),I(:,:,3)); % Initialize variables [h,w,~] = size(I); S = round(sqrt(h*w/k)); % superpixel size m = round(S/2); % grid spacing N = h*w; L = zeros(h,w); dist = inf(h,w); x = 1:w; y = 1:h; [X,Y] = meshgrid(x,y); X = X(:); Y = Y(:); % Initialize cluster centers C = [m:m:w-m m:m:h-m]'; [X,Y] = meshgrid(C,C); C = [X(:) Y(:)]; NumLabels = size(C,1); % Perform k-means clustering for i = 1:10 dist_old = dist; dist = inf(h,w); for j = 1:NumLabels x_min = max(C(j,1)-S,1); x_max = min(C(j,1)+S,w); y_min = max(C(j,2)-S,1); y_max = min(C(j,2)+S,h); idx = find(X>=x_min & X<=x_max & Y>=y_min & Y<=y_max); D = (L(idx)-L(C(j,2),C(j,1))).^2 + ... ((a(idx)-a(C(j,2),C(j,1))).^2 + ... (b(idx)-b(C(j,2),C(j,1))).^2)*(compactness^2); idx2 = find(D<dist(idx)); dist(idx(idx2)) = D(idx2); L(idx(idx2)) = j; end if dist_old==dist break; end for j = 1:NumLabels idx = find(L==j); C(j,:) = round(mean([X(idx) Y(idx)])); end end % Display superpixels figure; imshow(I); hold on; for i = 1:NumLabels idx = find(L==i); plot(X(idx),Y(idx),'o','MarkerSize',2,'MarkerFaceColor',rand(1,3)); end hold off; end 注意:这个代码是原始的slic算法,可能存在一些缺陷和不足,建议在使用时进行改进和优化。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值