Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute MATLAB/Octave Online

%Moving k-means algorithm
close all;
clear all;
clc;
q11=imread('micro2.png');
q1=rgb2gray(q11);
figure,imshow(q1);
kk1=0;
kk2=0;
akk1=0;
akk2=0;
%x=ceil(255*rand);
%y=ceil(255*rand);
figure,imhist(q1);
x=220;
y=11;
x1=0;
y1=0;
      
for i=1:272
    for j=1:256
        k1=q1(i,j)-x;
        if(k1<0)
            k1=k1*(-1);
        end
        k1=k1^2;
        k2=q1(i,j)-y;
        if(k2<0)
            k2=k2*(-1);
        end
        k2=k2^2;
        if(k1<k2)
            q2(i,j)=0;
        else
            q2(i,j)=255;
        end
    end
end
 
    k1=1;
    k2=1;
    n1=0;
    n2=0;
    clear kk1;
    clear kk2;
    kk1=0;
    kk2=0;
      
     for i=1:272
         for j=1:256
             if(q2(i,j)==255)
                kk1(k1)=q1(i,j);
                k1=k1+1;
                n1=n1+1;
              else
                kk2(k2)=q1(i,j);
                k2=k2+1;
                n2=n2+1;
              end 
         end
     end
akk1=kk1(:);
sumkk1=sum(akk1);
akk2=kk2(:);
sumkk2=sum(akk2);
x1=sumkk1/n1;
y1=sumkk2/n2;
fc1=0;
fc2=0;
nc=n1+n2;
for aq1=1:20
    for i=1:272
        for j=1:256
            if(q2(i,j)==255)
                d1=q1(i,j)-x1;
                d1=d1^2;
                fc1=fc1+d1;
            else
                d2=q1(i,j)-y1;
                d2=d2^2;
                fc2=fc2+d2;
            end
        end
    end
    if(fc1>fc2)
        n=0;
        fcl=fc1;
        fcs=fc2;
        cl=x1;
        cs=y1;
        ncl=n1;
        ncs=n2;
    else
        n=1;
        fcl=fc2;
        fcs=fc1;
        cl=y1;
        cs=x1;
        ncl=n2;
        ncs=n1;     
    end
    n11=fcs/fcl;
    if(n11>0&&n11<(1/3))
        break;
    else
        for i=1:272
            for j=1:256
                if(n==0)
                    if(q2(i,j)==255 &&q1(i,j)>fcl)
                        q2(i,j)=0; 
                    end 
                else
                    if(q2(i,j)== 0 &&q1(i,j)>fcl)
                        q2(i,j)=255; 
                    end
                end
            end
        end
        scs=0;
        scl=0;
        for i=1:272
            for j=1:256
                if(n==0)
                    if(q2(i,j)==0)
                        ncs=ncs+1;
                        scs=scs+q1(i,j);
                    else
                        ncl=ncl+1;
                        scl=scl+q1(i,j);
                    end
                else
                    if(q2(i,j)==255)
                        ncs=ncs+1;
                        scs=scs+q1(i,j);
                    else
                        ncl=ncl+1;
                        scl=scl+q1(i,j);
                    end
                    cs=(1/ncs)*(scs);
                    cl=(1/ncl)*(scl);
                    n11=n11-(n11/nc); 
                    if(n==0)
                        x1=cl;
                        y1=cs;
                    else
                        x1=cs;
                        y1=cl;
                    end
                end
            end
        end
    end
end
figure,imshow(uint8(q2));

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.