Você está na página 1de 3

if get(handles.radiobutton3,'value') %jml=str2num(get(handles.edit1,'string')); %ambil jumlah seg trus konversi ke number ima=I; ima1=double(ima);%convert into double.

ima11=ima1(:);%convert the image matrix into vector. %copy=ima1;%copy the image [R C]=size(ima1);%to get the dimention of the Matrix %make the Cenroid matrix mu1=[60 165 211 165 211];%innitialization of the Matrix pil=str2num(get(handles.edit3,'string')); mu=mu(1:pil); %Recalculation of the centroid for l=1:length(mu) cmd=['ass' num2str(l) '=zeros(R,C)']; eval(cmd); end while(true) oldmu=mu;%chek the centroid for i=1:R%row for j=1:C%coulomb r=ima1(i,j);%pixel value of the (i,j)co ordinate ab=abs((ima1(i,j))-mu);%to find out that the pixel will belongs to which cluster mn=find(ab==min(ab)); % if mn(1)==1 % ass1(i,j)=r;%assingning to the 1st cluster % end % if mn(1)==2 % ass2(i,j)=r;%assingning to the 2nd cluster % end % if mn(1)==3 % ass3(i,j)=r;%assingning to the 3rd cluster % end cmd=['ass' num2str(mu) '=r']; eval(cmd); end end % co1=ass1(:);%trasfer into vector % su1=sum(co1);%sum of the vector % fi1=find(co1);%to find non zero element % len1=length(fi1);%to find the length of the non zeor element. % mm1=su1/len1; % mm11=round(mm1);%new center element. %now to calculate the 2nd element of the centroid for k=1:mu cmd=['co' num2str(k) '=ass' num2str(k) '(:)']; eval(cmd); cmd=['su' num2str(k) '=sum(co' num2str (k) ')']; eval(cmd); cmd=['fi' num2str(k) '=find(co' num2str (k) ')'];

eval(cmd); cmd=['len' num2str(k) '=length(fi' num2str (k) ')']; eval(cmd); cmd=['mm' num2str(k) '=su' num2str (k) '/len' num2str (k)]; eval(cmd); cmd=['mm' num2str(k) num2str(k) '=round(mm' num2str (k)]; eval(cmd); end % % % % % % % % % % % % co2=ass2(:); su2=sum(co2); fi2=find(co2); len2=length(fi2); mm2=su2/len2; mm22=round(mm2); %now to calculate the 3rd element of the centroid. co3=ass3(:); su3=sum(co3); fi3=find(co3); len3=length(fi3); mm3=su3/len3; mm33=round(mm3); %new centroid cmd=['mu=[']; for k=1:mu cmd=[cmd 'mm' num2str(k) num2str(k)]; end cmd=[cmd ']']; eval(cmd) %mu=[mm11 mm22 mm33]; if(mu==oldmu) break;end;

end %labelling of the clusters for i=1:R for j=1:C % if ass1(i,j)>0;ass1(i,j)=1; end % if ass2(i,j)>0 % ass2(i,j)=2; % end % if ass3(i,j)>0 % ass3(i,j)=3; % end for k=1:mu cmd=['if ass' num2str(k) '(i,j)>0;ass' num2str(k) '(i,j)=i; end']; end end end %representing the culustered image

for k=1:mu cmd=['finlcluste=' 'ass' num2str(k) '+ass' num2str(k) '+ass' num2str(k)]; eval(cmd); cmd=['finlcluste1=label2rgb(finlcluste)']; eval(cmd); cmd=['fff=rgb2gray(finlcluste1)']; eval(cmd) end % finlcluste=(ass1+ass2+ass3);%sum up the three labelled cluster % finlcluste1=label2rgb(finlcluste);%final segmented image % fff=rgb2gray(finlcluste1); %finlcluste1=lab2uint8(finlcluste); axes(handles.axes3); imshow(fff); rectangle('Position', [100, 100, 10, 10]); % imwrite(fff, 'mri103.jpg'); end if get(handles.radiobutton4,'value') end

Você também pode gostar