import scipy.io import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as ptch import matplotlib.image as mpimg from PIL import Image import math from scipy.stats import norm from numpy.random import random from sklearn.cluster import KMeans from sklearn.metrics import pairwise_distances_argmin from sklearn.datasets import load_sample_image from sklearn.utils import shuffle import os def multinomial_resample(weights): """ This is the naive form of roulette sampling where we compute the cumulative sum of the weights and then use binary search to select the resampled point based on a uniformly distributed random number. Run time is O(n log n). You do not want to use this algorithm in practice; for some reason it is popular in blogs and online courses so I included it for reference. Parameters ---------- weights : list-like of float list of weights as floats Returns ------- indexes : ndarray of ints array of indexes into the weights defining the resample. i.e. the index of the zeroth resample is indexes[0], etc. """ weights=weights.T cumulative_sum = np.cumsum(weights) #cumulative_sum[-1] = 1. # avoid round-off errors: ensures sum is exactly one #print ( np.searchsorted(cumulative_sum, random(len(weights)))) return np.searchsorted(cumulative_sum, random(len(weights))) def lecture_image() : SEQUENCE = "./sequence2/" START = 1 #charge le nom des images de la séquence filenames = os.listdir(SEQUENCE) T = len(filenames) #print(T) #print(filenames) #charge la premiere image dans ’im’ tt = 0 # print(str(SEQUENCE)+str(filenames[tt])) #im = mpimg.imread(str(SEQUENCE)+str(filenames[tt])) im=Image.open((str(SEQUENCE)+str(filenames[tt]))) #affiche ’im’ #fig = figure; #set(fig,'Units','Normalized','Position',[0 0 1 1]); #set(gcf, 'DoubleBuffer', 'on') ; plt.imshow(im) #plt.show() return(im,filenames,T,SEQUENCE) def selectionner_zone() : #lecture_image() print('Cliquer 4 points dans l image pour definir la zone a suivre.') ; zone = np.zeros([2,4]) # print(zone) compteur=0 while(compteur != 4): res = plt.ginput(1) a=res[0] #print(type(a)) zone[0,compteur] = a[0] zone[1,compteur] = a[1] plt.plot(a[0],a[1],marker='X',color='red') compteur = compteur+1 #print(zone) newzone = np.zeros([2,4]) ; newzone[0, :] = np.sort(zone[0, :]) newzone[1, :] = np.sort(zone[1, :]) zoneAT = np.zeros([4]) zoneAT[0] = newzone[0,0] zoneAT[1] = newzone[1,0] zoneAT[2] = newzone[0,3]-newzone[0,0] zoneAT[3] = newzone[1,3]-newzone[1,0] #affichage du rectangle #print(zoneAT) xy=(zoneAT[0],zoneAT[1]) rect=ptch.Rectangle(xy,zoneAT[2],zoneAT[3],linewidth=3,edgecolor='red',facecolor='None') #plt.Rectangle(zoneAT[0:1],zoneAT[2],zoneAT[3]) currentAxis = plt.gca() currentAxis.add_patch(rect) plt.show(block=False) return(zoneAT) def rgb2ind(im,nb) : #nb = nombre de couleurs ou kmeans qui contient la carte de couleur de l'image de référence image=np.array(im,dtype=np.float64)/255 w,h,d=original_shape=tuple(image.shape) image_array=np.reshape(image,(w*h,d)) image_array_sample=shuffle(image_array,random_state=0)[:1000] # print(type(image_array)) if type(nb)==int : kmeans=KMeans(n_clusters=nb,random_state=0).fit(image_array_sample) else : kmeans=nb labels=kmeans.predict(image_array) #print(labels) image=recreate_image(kmeans.cluster_centers_,labels,w,h) #print(image) return(Image.fromarray(image.astype('uint8')),kmeans) def recreate_image(codebook,labels,w,h): d=codebook.shape[1] #image=np.zeros((w,h,d)) image=np.zeros((w,h)) label_idx=0 for i in range(w): for j in range(h): #image[i][j]=codebook[labels[label_idx]]*255 image[i][j]=labels[label_idx] #print(image[i][j]) label_idx+=1 return image def calcul_histogramme(im,zoneAT,Nb): # print(zoneAT) box=(zoneAT[0],zoneAT[1],zoneAT[0]+zoneAT[2],zoneAT[1]+zoneAT[3]) # print(box) littleim = im.crop(box) ## plt.imshow(littleim) ## plt.show() new_im,kmeans= rgb2ind(littleim,Nb) histogramme=np.asarray(new_im.histogram()) ## print(histogramme) histogramme=histogramme/np.sum(histogramme) # print(new_im) return (new_im,kmeans,histogramme)