pixel_numb = img2.shape[0] * img2.shape[1] prom_pond = 1/pixel_numb bins = 100 hist = img3 final_thresh = -1 final_value = -1 intensity = np.arange(100) for x in range (1,100): pcb = np.sum(hist[:x]) pcf = np.sum(hist[x:]) wb = pcb * prom_pond wf = pcf * prom_pond mub = np.sum(intensity[:x] * hist[:x]) / (pcb) muf = np.sum(intensity[x:] * hist[x:]) / (pcf) value = wb * wf * (mub-muf) **2 if value > final_value: final_thresh = x/100 final_value = value print(final_thresh) img8 = img2.copy() img8[img2 > final_thresh] = 1 img8[img2 < final_thresh] = 0 plt.figure(6) plt.title('Binarizacion por el método de Otsu') plt.imshow(img8,'gray')