Oppgave 3 - Terskling i praksis
def riddler_calvard(img, G): T = G / 2 Tprev = -1 while abs(Tprev - T) > 1e-5: mu_background = (img[img <= T]).mean() mu_foreground = (img[img > T]).mean() Tprev = T T = (mu_background+mu_foreground)/2 return T def otsu(img, G): p = my_hist(img, G) / img.size c = np.cumsum(p) # P_1 i notatene mu_k = np.cumsum(np.arange(G) * p) mu = mu_k[-1] # == np.mean(img) var_B = (mu_k - mu*c )**2 / (c *(1-c)) t = np.nanargmax(var_B) score = np.max(var_B) / np.var(img) return t, score