If (extension.lower() = ".jpg" or extension.lower() = ".jpeg") and numColors > 1 and numColors <= MAX_CLUSTERS and iterations > 0 and iterations <= MAX_ITERATIONS :
NameFile,extension = os.path.splitext(inputFile) Print "#Colors: " + str(numColors) + " #Iterations: " + str(iterations) Print "Error: #Colors or #Iteration are not correct" Print "Time: " + str(T1-T0) + " seconds." #print "Printing OLD CENTROIDS: " + str(CENTROIDS) #print "Printing NEW CENTROIDS: " + str(NEW_CENTROIDS) #print "Printing CLUSTER NO " + str(CLUSTER_NO) # n_cluster is the number of cluster where the pixel belongs toĬlusterTuple = x # x position of the pixelĬlusterTuple = y # y position of the pixelĬlusterTuple = findCluster(loadedImage) # cluster no. # ClusterTuple has for each pixel in the image OutputImage.save(name+"_cl"+str(N_CLUSTERS)+"_it"+str(N_ITERATIONS)+EXTENSION,"JPEG",quality=100) OutputImage = Image.new("RGB",IMAGE_SIZE)ĭraw.point((element,element), fill=c) python kmeans.py my_Picture.jpg no_Of_Clusters no_Of_Iterations The libraries needed for this example code are PIL and Numpy. In order to get familiarity with k-means, a first prototype of the algorithm is implemented in python. K, as already mentioned, is defined by the user. The K centroids are the RGB colours of the image that will be computed iteration by iteration and, at the end, it will define the final colors of the entire image. In our domain the dataset is represented by the entire image containing its pixels. Kmeans can be used to reduce the number of color of an RGB image for many purposes and for this reason the scheme described above is expressed in a different domain. Once every element is assigned to some cluster, for each cluster the new centroid is computed.Each element of the dataset is assigned to the closest cluster.K centroids, one of each cluster, are chosen with an heuristic method.For a better insight of this algorithm I suggest to read this.Ī brief explanation of how it works is shown below. K-Means is commonly used for cluster analysis and data mining. !! The code is available on my gitHub toskyRocker account!!
0 Comments
Leave a Reply. |