I was trying to make a program for image compression using k means clustering
Can someone tell me what’s wrong with this code?
from scipy import misc
import numpy as np
from scipy.misc import toimage
img=misc.imread('bird_small.png')
img=img.reshape((16384,3))
def findc(X,incd) :
c=[]
for j in range(0,16384):
k1 = []
for i in range(0,16):
k=X[j]-incd[i]
k1.append(k.dot(k.transpose()))
print(j)
c.append(np.argmax(k1))
return c
def findu(X,u):
u=np.zeros((16,3))
a=np.zeros(16)
for j in range(0,16384):
for i in range(0,16):
if(c[j]==i):
u[i]=u[i]+X[j]
a[i]=a[i]+1
newc=[]
for i in range(0,16):
newc.append(u[i]/a[i])
return newc
incd = np.random.randint(np.size(img,axis=0), size=16)
print(np.size(img,axis=0))
incd = img[incd, :]
incd = incd.reshape((16, 3))
print(incd)
for _ in range(0,10):
c=findc(img,incd)
prevcd=incd
incd=findu(img,c)
for j in range(0,16384):
for i in range(0, 16):
if (c[j] == i):
img[j]=incd[i]
img.reshape((128,128,3))
toimage(img).show()