User talk:2A01:E0A:475:A4D0:7878:77BE:F159:D194

Better algorithm
Hello, as a new user, I don't know how to properly edit the page. Here is the modification I want to add, to the algorithm section.

As the algorithm requires only the histogram, not the complete image, we can optimize sucessive calls to compute_otsu_criteria by computing the histogram only once with img_hist and using the compute_otsu_hist function instead. On large images, this greatly improves performance

def img_hist(img): r = []; for i in range(0,len(img) ): for j in range(0,len(img[i]) ): pixel = img[i,j]; while(len(r) <= pixel ):r.append(0); r[pixel]=r[pixel]+1 return r

def compute_otsu_hist(hist,th): #hist is the histogram : an array with index as pixel value, and data as pixel count if(th>len(hist)):return np.inf

n0=0 n1=0 sum0 = 0 sum1 = 0 #compute mean values for i in range(0, min(th, len(hist)) ): n0 = n0+hist[i]; sum0 = sum0 + hist[i]*i if n0==0: return np.inf for i in range(th, len(hist)): n1 = n1+hist[i]; sum1 = sum1 + hist[i]*i if n1==0: return np.inf mean0 = sum0/n0 mean1 = sum1/n1 #compute variances var0=0 var1=0 for i in range(0, min(th, len(hist)) ): var0=var0 + hist[i]*pow(i-mean0,2) for i in range(th, len(hist)): var1=var1 + hist[i]*pow(i-mean1,2) var0=var0/n0 var1=var1/n1 weight0 = n0 / (n0+n1) weight1 = 1-weight0 return weight0 * var0 + weight1 * var1