// Pad left and top of Integral image with zeros
I = np.vstack((np.zeros((1, I.shape[1]), I.dtype), I))
I = np.hstack((np.zeros((I.shape[0], 1), I.dtype), I))
kern = np.zeros((w + 1, w + 1))
kern[0, 0], kern[-1, -1] = 1, 1
kern[[0, -1], [-1, 0]] = -1
After Change
g = image.astype(np.float)
sum_g2 = ndi.convolve(g ** 2., np.ones((w, w)), mode="constant")
sum_m2 = w2 * m ** 2.
s = np.sqrt((sum_g2 - sum_m2) / w2)
return m, s
def threshold_niblack(image, window_size=15, k=0.2):