From ae73da922f7293a24cad3c974bb83d05fb7960c8 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Mon, 15 Oct 2012 14:45:25 +0200 Subject: [PATCH] remplace emask with is_in_mask function --- skimage/rank/_core8.pyx | 103 +++++++++++++++--------------- skimage/rank/tests/demo_single.py | 29 +++++++++ 2 files changed, 79 insertions(+), 53 deletions(-) create mode 100644 skimage/rank/tests/demo_single.py diff --git a/skimage/rank/_core8.pyx b/skimage/rank/_core8.pyx index 6dc59059..baedd67a 100644 --- a/skimage/rank/_core8.pyx +++ b/skimage/rank/_core8.pyx @@ -18,6 +18,15 @@ from libc.stdlib cimport malloc, free # 8 bit core kernel #--------------------------------------------------------------------------- +cdef inline Py_ssize_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols,Py_ssize_t r, Py_ssize_t c,np.uint8_t* mask): + if r < 0 or r > rows - 1 or c < 0 or c > cols - 1: + return 0 + else: + if mask[r*cols+c]: + return 1 + else: + return 0 + cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t), np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] selem, @@ -55,21 +64,9 @@ char shift_x, char shift_y): else: out = np.ascontiguousarray(out) - # create extended image and mask - cdef Py_ssize_t erows = rows+srows-1 - cdef Py_ssize_t ecols = cols+scols-1 - - cdef np.ndarray emask = np.zeros((erows, ecols), dtype=np.uint8) - cdef np.ndarray eimage = np.zeros((erows, ecols), dtype=np.uint8) - - eimage[centre_r:rows+centre_r,centre_c:cols+centre_c] = image - emask[centre_r:rows+centre_r,centre_c:cols+centre_c] = mask - mask = np.ascontiguousarray(mask) # define pointers to the data - cdef np.uint8_t* eimage_data = eimage.data - cdef np.uint8_t* emask_data = emask.data cdef np.uint8_t* out_data = out.data cdef np.uint8_t* image_data = image.data @@ -145,18 +142,18 @@ char shift_x, char shift_y): for r in range(srows): for c in range(scols): - rr = r - cc = c + rr = r - centre_r + cc = c - centre_c if selem[r, c]: - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] += 1 pop += 1. r = 0 c = 0 # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c]) + out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c]) # kernel ------------------------------------------- # main loop @@ -165,22 +162,22 @@ char shift_x, char shift_y): # ---> west to east for c in range(1,cols): for s in range(num_se_e): - rr = r + se_e_r[s] + centre_r - cc = c + se_e_c[s] + centre_c - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + rr = r + se_e_r[s] + cc = c + se_e_c[s] + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] += 1 pop += 1. for s in range(num_se_w): - rr = r + se_w_r[s] + centre_r - cc = c + se_w_c[s] + centre_c - 1 - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + rr = r + se_w_r[s] + cc = c + se_w_c[s] - 1 + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] -= 1 pop -= 1. # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c]) + out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c]) # kernel ------------------------------------------- r += 1 # pass to the next row @@ -189,43 +186,43 @@ char shift_x, char shift_y): # ---> north to south for s in range(num_se_s): - rr = r + se_s_r[s] + centre_r - cc = c + se_s_c[s] + centre_c - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + rr = r + se_s_r[s] + cc = c + se_s_c[s] + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] += 1 pop += 1. for s in range(num_se_n): - rr = r + se_n_r[s] + centre_r - 1 - cc = c + se_n_c[s] + centre_c - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + rr = r + se_n_r[s] - 1 + cc = c + se_n_c[s] + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] -= 1 pop -= 1. # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c]) + out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c]) # kernel ------------------------------------------- # ---> east to west for c in range(cols-2,-1,-1): for s in range(num_se_w): - rr = r + se_w_r[s] + centre_r - cc = c + se_w_c[s] + centre_c - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + rr = r + se_w_r[s] + cc = c + se_w_c[s] + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] += 1 pop += 1. for s in range(num_se_e): - rr = r + se_e_r[s] + centre_r - cc = c + se_e_c[s] + centre_c + 1 - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + rr = r + se_e_r[s] + cc = c + se_e_c[s] + 1 + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] -= 1 pop -= 1. # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c]) + out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c]) # kernel ------------------------------------------- r += 1 # pass to the next row @@ -234,22 +231,22 @@ char shift_x, char shift_y): # ---> north to south for s in range(num_se_s): - rr = r + se_s_r[s] + centre_r - cc = c + se_s_c[s] + centre_c - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + rr = r + se_s_r[s] + cc = c + se_s_c[s] + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] += 1 pop += 1. for s in range(num_se_n): - rr = r + se_n_r[s] + centre_r - 1 - cc = c + se_n_c[s] + centre_c - if emask_data[rr * ecols + cc]: - value = eimage_data[rr * ecols + cc] + rr = r + se_n_r[s] - 1 + cc = c + se_n_c[s] + if is_in_mask(rows,cols,rr,cc,mask_data): + value = image_data[rr * cols + cc] histo[value] -= 1 pop -= 1. # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c]) + out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c]) # kernel ------------------------------------------- # release memory allocated by malloc diff --git a/skimage/rank/tests/demo_single.py b/skimage/rank/tests/demo_single.py new file mode 100644 index 00000000..028f27b2 --- /dev/null +++ b/skimage/rank/tests/demo_single.py @@ -0,0 +1,29 @@ +import numpy as np +import matplotlib.pyplot as plt +from pprint import pprint + +from skimage import data +from skimage.morphology.selem import disk +import skimage.rank as rank + + +if __name__ == '__main__': + a8 = data.camera() + a16 = data.camera().astype(np.uint16) + selem = disk(10) + + f8= rank.mean(a8,selem) + f16= rank.mean(a16,selem) + + print f8==f16 + + plt.figure() + plt.subplot(1,2,1) + plt.imshow(a8) + plt.subplot(1,2,2) + plt.imshow(f8-f16) + plt.show() + + + +