• 大小: 2KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-28
  • 语言: Matlab
  • 标签: matlab  SLIC  代码  

资源简介

SLIC的matlab代码,可以更改自己的图片位置,生成结果,可以直接运行使用

资源截图

代码片段和文件信息

clc
clear
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%读取图像,预处理%%%%%%%%%%%%%%%
he = imread(‘C:\Users\lpflying\Desktop\my doc\程序代码\BSR\BSDS500\data\images\train\train5.jpg‘);%读取图像
cform = makecform(‘srgb2lab‘);%图像由RGB转为lab
lab_he = applycform(hecform);%
lab_he=double(lab_he);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%设置初值%%%%%%%%%%%%%%%%%%
color=[25000];
thre=0.02;%最终生成分割图像梯度阈值
m=40;%权值
k=1;%划分为300个簇
die=20;%kmeans迭代die次
x=size(he);s=(x(1)*x(2)/k)^0.5;s=ceil(s);%初始分割网格间距s
r=ceil(x(1)/s);%网格行数r
w=ceil(x(2)/s);%网格列数w
ct=r*w;
belong=ones(x(1)x(2));
center=zeros(ct5);
%初始每个像素点的距离
dist=9999*ones(x(1)x(2));
%初始中心节点center
for i=1:r
    for j=1:w
        if (i            x1=(i-1)*s+fix(s/2);
        else
            x1=(i-1)*s+fix(rem(x(1)s)/2);
        end
        if (j            y1=(j-1)*s+fix(s/2);
        else
            y1=(j-1)*s+fix(rem(x(2)s)/2);
        end
        z=lab_he(x1y1:);
        center((i-1)*w+j:)=[z(::1) z(::2) z(::3) x1 y1];%初始中心节点center
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%迭代聚类处理%%%%%%%%%%%%%%%
t1=clock;
move=99999;
for c=1:die    %进行迭代die次
    if move<10
        break;
    end
    move=0;
    c1=zeros(ct);
    ct_x=zeros(ct);
    ct_y=zeros(ct);
    ct_l=zeros(ct);
    ct_a=zeros(ct);
    ct_b=zeros(ct);
    for i=1:ct
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        for u=center(i4)-s:center(i4)+s
            if(u>=1)&&(u<=x(1))
                for v=center(i5)-s:center(i5)+s
                    if(v>=1)&&(v<=x(2))
                        dc=((lab_he(uv1)-center(i1))^2+(lab_he(uv2)-center(i2))^2+(lab_he(uv3)-center(i3))^2)^0.5;
                        ds=((u-center(i4))^2+(v-center(i5))^2)^0.5;
                        d=((dc)^2+(ds*m/s)^2)^0.5;%计算距离
                        if d                            dist(uv)=d;
                            belong(uv)=i; 
                            move=move+1;
                        end
                    end
                end
            end
        end
    end 
    for k=1:x(1)
        for g=1:x(2)
            i=belong(kg);
            c1(i)=c1(i)+1;
            ct_x(i)=ct_x(i)+k;
            ct_y(i)=ct_y(i)+g;
            ct_l(i)=ct_l(i)+lab_he(kg1);
            ct_a(i)=ct_a(i)+lab_he(kg2);
            ct_b(i)=ct_b(i)+lab_he(kg3);
            
        end
    end
    for i=1:ct
        center(i4)=fix(ct_x(i)/c1(i));
        center(i5)=fix(ct_y(i)/c1(i));
        center(i1)=fix(ct_l(i)/c1(i));
        center(i2)=fix(ct_a(i)/c1(i));
        center(i3)=fix(ct_b(i)/c1(i));
    end
end
t2=clock;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%去坏点%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:ct
    bw=zeros(x(1)x(2));
    for k=1:x(1)
        for g=1:x(2)
            if belong(kg)==i
                bw(kg)=1;
         

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        6373  2013-10-24 12:34  slic\slic.m
     文件          68  2019-05-05 17:51  slic\说明.txt
     目录           0  2019-05-05 17:51  slic\

评论

共有 条评论