def test_apply_filters_functionality(self): cm = plt.get_cmap('gray') kw = {'cmap': cm, 'interpolation': 'none', 'origin': 'upper'} im = cv2.imread('ms_25_short.png', 0) angles = np.arange(0, 155, 25) scales = [16.8, 22.5] orient, _, response = MultiSkewExtractor.filter_document( im, scales, angles) print(response[99:119, 99:119]) print("\norient:{}\n\n".format(orient[99:119, 99:119])) response = np.double(response) m, s = _mean_std(response, int(math.ceil(22.5) * 2 + 1)) print("\nmean:{}\n\n".format(m[99:119, 99:119])) print("\nstd:{}\n\n".format(s[99:119, 99:119])) high = 22 low = 8 thresh_niblack2 = np.divide((response - m), s) * 20 # plt.subplot(1, 3, 3) # plt.imshow(thresh_niblack2, **kw) # plt.title('thresh_niblack2') lines = apply_hysteresis_threshold(thresh_niblack2, low, high) print("\nlines:{}\n\n".format(lines[99:119, 99:119])) # plt.subplot(1, 3, 1) # plt.imshow(response, **kw) # plt.title('response') plt.subplot(1, 1, 1) plt.imshow(lines, **kw) plt.title('lines') plt.show() print("done")
def threshold_phansalkar(image, window_size=15, k=0.25, r=None, p=3.0, q=10.0): """Applies Phansalkar local threshold to an array. Phansalkar is a modification of Sauvola technique to deal with low contrast images. This method is using the following formula:: T = m(x,y) * (1 + p * exp( -q * m(x,y) ) + k * ((s(x,y) / R) - 1)) where m(x,y) and s(x,y) are the mean and standard deviation of pixel (x,y) neighborhood defined by a rectangular window with size w times w centered around the pixel. k,p and q are configurable parameters. R is the maximum standard deviation of a greyscale image. Parameters ---------- image : ndarray Input image. window_size : int, or iterable of int, optional Window size specified as a single odd integer (3, 5, 7, …), or an iterable of length ``image.ndim`` containing only odd integers (e.g. ``(1, 5, 5)``). k : float, optional Value of the positive parameter k. r : float, optional Value of R, the dynamic range of standard deviation. If None, set to the half of the image dtype range. p : float, optional Value of the parameter p. q : float, optional Value of the parameter q. Returns ------- threshold : (N, M) ndarray Threshold mask. All pixels with an intensity higher than this value are assumed to be foreground. Notes ----- This algorithm is originally designed for detection of cell nuclei in low contrast images. Therefore the historgram has to be equalized beforehand using skimage.exposure.equalize_adapthist(). References ---------- .. [1] Phansalskar N. et al. "Adaptive local thresholding for detection of nuclei in diversity stained cytology images.", International Conference on Communications and Signal Processing (ICCSP), pp. 218-220, 2011 :DOI:`10.1109/ICCSP.2011.5739305` Examples -------- >>> from skimage import data >>> from skimage.exposure import equalize_adapthist >>> image = data.page() >>> image_eq = equalize_adapthist(image) >>> t_phansalkar = threshold_phansalkar(image_eq, window_size=15, k=0.25, p=2.0, q=10.0) >>> binary_image = image_eq > t_phansalkar """ if r is None: # set r as image.ptp() # since the image is processed using equalize_adapthist(), r will become always 1 r = 1.0 m, s = _mean_std(image, window_size) return m * (1 + np.power(p, (-q * m)) + k * ((s / r) - 1))
def test_mean_std_3d(window_size, mean_kernel): image = np.random.rand(40, 40, 40) m, s = _mean_std(image, w=window_size) expected_m = ndi.convolve(image, mean_kernel, mode='mirror') np.testing.assert_allclose(m, expected_m) expected_s = ndi.generic_filter(image, np.std, size=window_size, mode='mirror') np.testing.assert_allclose(s, expected_s)
def test_mean_std_2d(): image = np.random.rand(256, 256) window_size = 11 m, s = _mean_std(image, w=window_size) mean_kernel = np.ones((window_size,) * 2) / window_size**2 expected_m = ndi.convolve(image, mean_kernel, mode='mirror') np.testing.assert_allclose(m, expected_m) expected_s = ndi.generic_filter(image, np.std, size=window_size, mode='mirror') np.testing.assert_allclose(s, expected_s)
def __niblack_pre_process(self, max_response, n): im = np.double(max_response) m, s = _mean_std(im, 47) high = 22 low = 8 thresh_niblack2 = np.divide((im - m), s) * 20 lines = apply_hysteresis_threshold(thresh_niblack2, low, high) lines = reconstruction(np.logical_and(self.bin_image, lines), lines, method='dilation') return thresh_niblack2, lines
def niblack_pre_process(max_response, n, bin): im = np.double(max_response) # int(16.8) * 2 + 1 m, s = _mean_std(im, 47) high = 22 low = 8 thresh_niblack2 = np.divide((im - m), s) * 20 lines = apply_hysteresis_threshold(thresh_niblack2, low, high) lines = reconstruction(np.logical_and(bin, lines), lines, method='dilation') return thresh_niblack2, lines