delete core8p

This commit is contained in:
Olivier Debeir
2012-10-15 15:55:21 +02:00
parent ef768b22a8
commit 7862b971be
5 changed files with 20 additions and 306 deletions
-17
View File
@@ -1,17 +0,0 @@
cimport numpy as np
# generic cdef functions
cdef inline np.uint8_t uint8_max(np.uint8_t a, np.uint8_t b)
cdef inline np.uint8_t uint8_min(np.uint8_t a, np.uint8_t b)
#---------------------------------------------------------------------------
# 8 bit core kernel receives extra information about data inferior and superior percentiles
#---------------------------------------------------------------------------
cdef inline _core8p(np.uint8_t kernel(int*, float, np.uint8_t, float, float),
np.ndarray[np.uint8_t, ndim=2] image,
np.ndarray[np.uint8_t, ndim=2] selem,
np.ndarray[np.uint8_t, ndim=2] mask,
np.ndarray[np.uint8_t, ndim=2] out,
char shift_x, char shift_y, float p0, float p1)
-266
View File
@@ -1,266 +0,0 @@
#cython: cdivision=True
#cython: boundscheck=False
#cython: nonecheck=False
#cython: wraparound=False
import numpy as np
cimport numpy as np
from libc.stdlib cimport malloc, free
# generic cdef functions
cdef inline np.uint8_t uint8_max(np.uint8_t a, np.uint8_t b): return a if a >= b else b
cdef inline np.uint8_t uint8_min(np.uint8_t a, np.uint8_t b): return a if a <= b else b
#---------------------------------------------------------------------------
# 8 bit core kernel receives extra information about data inferior and superior percentiles
#---------------------------------------------------------------------------
cdef inline _core8p(np.uint8_t kernel(int*, float, np.uint8_t, float, float),
np.ndarray[np.uint8_t, ndim=2] image,
np.ndarray[np.uint8_t, ndim=2] selem,
np.ndarray[np.uint8_t, ndim=2] mask,
np.ndarray[np.uint8_t, ndim=2] out,
char shift_x, char shift_y, float p0, float p1):
""" Main loop, this function computes the histogram for each image point
- data is uint8
- result is uint8 casted
"""
cdef int rows = image.shape[0]
cdef int cols = image.shape[1]
cdef int srows = selem.shape[0]
cdef int scols = selem.shape[1]
cdef int centre_r = int(selem.shape[0] / 2) + shift_y
cdef int centre_c = int(selem.shape[1] / 2) + shift_x
# check that structuring element center is inside the element bounding box
assert centre_r >= 0
assert centre_c >= 0
assert centre_r < srows
assert centre_c < scols
image = np.ascontiguousarray(image)
if mask is None:
mask = np.ones((rows, cols), dtype=np.uint8)
else:
mask = np.ascontiguousarray(mask)
if out is None:
out = np.zeros((rows, cols), dtype=np.uint8)
else:
out = np.ascontiguousarray(out)
# create extended image and mask
cdef int erows = rows+srows-1
cdef int 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 = <np.uint8_t*>eimage.data
cdef np.uint8_t* emask_data = <np.uint8_t*>emask.data
cdef np.uint8_t* out_data = <np.uint8_t*>out.data
cdef np.uint8_t* image_data = <np.uint8_t*>image.data
cdef np.uint8_t* mask_data = <np.uint8_t*>mask.data
# define local variable types
cdef int r, c, rr, cc, s, value, local_max, i, even_row
cdef float pop # number of pixels actually inside the neighborhood (float)
# allocate memory with malloc
cdef int max_se = srows*scols
# number of element in each attack border
cdef int num_se_n, num_se_s, num_se_e, num_se_w
# the current local histogram distribution
cdef int* histo = <int*>malloc(256 * sizeof(int))
# these lists contain the relative pixel row and column for each of the 4 attack borders
# east, west, north and south
# e.g. se_e_r lists the rows of the east structuring element border
cdef int* se_e_r = <int*>malloc(max_se * sizeof(int))
cdef int* se_e_c = <int*>malloc(max_se * sizeof(int))
cdef int* se_w_r = <int*>malloc(max_se * sizeof(int))
cdef int* se_w_c = <int*>malloc(max_se * sizeof(int))
cdef int* se_n_r = <int*>malloc(max_se * sizeof(int))
cdef int* se_n_c = <int*>malloc(max_se * sizeof(int))
cdef int* se_s_r = <int*>malloc(max_se * sizeof(int))
cdef int* se_s_c = <int*>malloc(max_se * sizeof(int))
# build attack and release borders
# by using difference along axis
t = np.hstack((selem,np.zeros((selem.shape[0],1))))
t_e = np.diff(t,axis=1)==-1
t = np.hstack((np.zeros((selem.shape[0],1)),selem))
t_w = np.diff(t,axis=1)==1
t = np.vstack((selem,np.zeros((1,selem.shape[1]))))
t_s = np.diff(t,axis=0)==-1
t = np.vstack((np.zeros((1,selem.shape[1])),selem))
t_n = np.diff(t,axis=0)==1
num_se_n = num_se_s = num_se_e = num_se_w = 0
for r in range(srows):
for c in range(scols):
if t_e[r,c]:
se_e_r[num_se_e] = r - centre_r
se_e_c[num_se_e] = c - centre_c
num_se_e += 1
if t_w[r,c]:
se_w_r[num_se_w] = r - centre_r
se_w_c[num_se_w] = c - centre_c
num_se_w += 1
if t_n[r,c]:
se_n_r[num_se_n] = r - centre_r
se_n_c[num_se_n] = c - centre_c
num_se_n += 1
if t_s[r,c]:
se_s_r[num_se_s] = r - centre_r
se_s_c[num_se_s] = c - centre_c
num_se_s += 1
# initial population and histogram
for i in range(256):
histo[i] = 0
pop = 0
for r in range(srows):
for c in range(scols):
rr = r
cc = c
if selem[r, c]:
if emask_data[rr * ecols + cc]:
value = eimage_data[rr * ecols + 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],p0,p1)
# kernel -------------------------------------------
# main loop
r = 0
for even_row in range(0,rows,2):
# ---> 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]
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]
histo[value] -= 1
pop -= 1.
# kernel -------------------------------------------
out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c],p0,p1)
# kernel -------------------------------------------
r += 1 # pass to the next row
if r>=rows:
break
# ---> 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]
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]
histo[value] -= 1
pop -= 1.
# kernel -------------------------------------------
out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c],p0,p1)
# 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]
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]
histo[value] -= 1
pop -= 1.
# kernel -------------------------------------------
out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c],p0,p1)
# kernel -------------------------------------------
r += 1 # pass to the next row
if r>=rows:
break
# ---> 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]
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]
histo[value] -= 1
pop -= 1.
# kernel -------------------------------------------
out_data[r * cols + c] = kernel(histo,pop,eimage_data[(r+centre_r) * ecols + c + centre_c],p0,p1)
# kernel -------------------------------------------
# release memory allocated by malloc
free(se_e_r)
free(se_e_c)
free(se_w_r)
free(se_w_c)
free(se_n_r)
free(se_n_c)
free(se_s_r)
free(se_s_c)
free(histo)
return out
+17 -17
View File
@@ -7,13 +7,13 @@ import numpy as np
cimport numpy as np
# import main loop
from _core8p cimport _core8p,uint8_max,uint8_min
from _core8 cimport _core8,uint8_max,uint8_min
# -----------------------------------------------------------------
# kernels uint8 (SOFT version using percentiles)
# -----------------------------------------------------------------
cdef inline np.uint8_t kernel_autolevel(int* histo, float pop, np.uint8_t g, float p0, float p1):
cdef inline np.uint8_t kernel_autolevel(Py_ssize_t* histo, float pop, np.uint8_t g, float p0, float p1,Py_ssize_t s0, Py_ssize_t s1):
cdef int i,imin,imax,sum,delta
if pop:
@@ -42,7 +42,7 @@ cdef inline np.uint8_t kernel_autolevel(int* histo, float pop, np.uint8_t g, flo
return <np.uint8_t>(128)
cdef inline np.uint8_t kernel_gradient(int* histo, float pop, np.uint8_t g, float p0, float p1):
cdef inline np.uint8_t kernel_gradient(Py_ssize_t* histo, float pop, np.uint8_t g, float p0, float p1,Py_ssize_t s0, Py_ssize_t s1):
cdef int i,imin,imax,sum,delta
if pop:
@@ -65,7 +65,7 @@ cdef inline np.uint8_t kernel_gradient(int* histo, float pop, np.uint8_t g, floa
return <np.uint8_t>(0)
cdef inline np.uint8_t kernel_mean(int* histo, float pop, np.uint8_t g, float p0, float p1):
cdef inline np.uint8_t kernel_mean(Py_ssize_t* histo, float pop, np.uint8_t g, float p0, float p1,Py_ssize_t s0, Py_ssize_t s1):
cdef int i,sum,mean,n
if pop:
@@ -84,7 +84,7 @@ cdef inline np.uint8_t kernel_mean(int* histo, float pop, np.uint8_t g, float p0
else:
return <np.uint8_t>(0)
cdef inline np.uint8_t kernel_mean_substraction(int* histo, float pop, np.uint8_t g, float p0, float p1):
cdef inline np.uint8_t kernel_mean_substraction(Py_ssize_t* histo, float pop, np.uint8_t g, float p0, float p1,Py_ssize_t s0, Py_ssize_t s1):
cdef int i,sum,mean,n
if pop:
@@ -103,7 +103,7 @@ cdef inline np.uint8_t kernel_mean_substraction(int* histo, float pop, np.uint8_
else:
return <np.uint8_t>(0)
cdef inline np.uint8_t kernel_morph_contr_enh(int* histo, float pop, np.uint8_t g, float p0, float p1):
cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t* histo, float pop, np.uint8_t g, float p0, float p1,Py_ssize_t s0, Py_ssize_t s1):
cdef int i,imin,imax,sum,delta
if pop:
@@ -131,7 +131,7 @@ cdef inline np.uint8_t kernel_morph_contr_enh(int* histo, float pop, np.uint8_t
else:
return <np.uint8_t>(0)
cdef inline np.uint8_t kernel_percentile(int* histo, float pop, np.uint8_t g, float p0, float p1):
cdef inline np.uint8_t kernel_percentile(Py_ssize_t* histo, float pop, np.uint8_t g, float p0, float p1,Py_ssize_t s0, Py_ssize_t s1):
cdef int i
cdef float sum = 0.
@@ -145,7 +145,7 @@ cdef inline np.uint8_t kernel_percentile(int* histo, float pop, np.uint8_t g, fl
else:
return <np.uint8_t>(0)
cdef inline np.uint8_t kernel_pop(int* histo, float pop, np.uint8_t g, float p0, float p1):
cdef inline np.uint8_t kernel_pop(Py_ssize_t* histo, float pop, np.uint8_t g, float p0, float p1,Py_ssize_t s0, Py_ssize_t s1):
cdef int i,sum,n
if pop:
@@ -159,7 +159,7 @@ cdef inline np.uint8_t kernel_pop(int* histo, float pop, np.uint8_t g, float p0,
else:
return <np.uint8_t>(0)
cdef inline np.uint8_t kernel_threshold(int* histo, float pop, np.uint8_t g, float p0, float p1):
cdef inline np.uint8_t kernel_threshold(Py_ssize_t* histo, float pop, np.uint8_t g, float p0, float p1,Py_ssize_t s0, Py_ssize_t s1):
cdef int i
cdef float sum = 0.
@@ -183,7 +183,7 @@ def autolevel(np.ndarray[np.uint8_t, ndim=2] image,
char shift_x=0, char shift_y=0, float p0=0., float p1=0.):
"""autolevel
"""
return _core8p(kernel_autolevel,image,selem,mask,out,shift_x,shift_y,p0,p1)
return _core8(kernel_autolevel,image,selem,mask,out,shift_x,shift_y,p0,p1,<Py_ssize_t>0,<Py_ssize_t>0)
def gradient(np.ndarray[np.uint8_t, ndim=2] image,
@@ -193,7 +193,7 @@ def gradient(np.ndarray[np.uint8_t, ndim=2] image,
char shift_x=0, char shift_y=0, float p0=0., float p1=0.):
"""return p0,p1 percentile gradient
"""
return _core8p(kernel_gradient,image,selem,mask,out,shift_x,shift_y,p0,p1)
return _core8(kernel_gradient,image,selem,mask,out,shift_x,shift_y,p0,p1,<Py_ssize_t>0,<Py_ssize_t>0)
def mean(np.ndarray[np.uint8_t, ndim=2] image,
np.ndarray[np.uint8_t, ndim=2] selem,
@@ -202,7 +202,7 @@ def mean(np.ndarray[np.uint8_t, ndim=2] image,
char shift_x=0, char shift_y=0, float p0=0., float p1=0.):
"""return mean between [p0 and p1] percentiles
"""
return _core8p(kernel_mean,image,selem,mask,out,shift_x,shift_y,p0,p1)
return _core8(kernel_mean,image,selem,mask,out,shift_x,shift_y,p0,p1,<Py_ssize_t>0,<Py_ssize_t>0)
def mean_substraction(np.ndarray[np.uint8_t, ndim=2] image,
np.ndarray[np.uint8_t, ndim=2] selem,
@@ -211,7 +211,7 @@ def mean_substraction(np.ndarray[np.uint8_t, ndim=2] image,
char shift_x=0, char shift_y=0, float p0=0., float p1=0.):
"""return original - mean between [p0 and p1] percentiles *.5 +127
"""
return _core8p(kernel_mean_substraction,image,selem,mask,out,shift_x,shift_y,p0,p1)
return _core8(kernel_mean_substraction,image,selem,mask,out,shift_x,shift_y,p0,p1,<Py_ssize_t>0,<Py_ssize_t>0)
def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image,
np.ndarray[np.uint8_t, ndim=2] selem,
@@ -220,7 +220,7 @@ def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image,
char shift_x=0, char shift_y=0, float p0=0., float p1=0.):
"""reforce contrast using percentiles
"""
return _core8p(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y,p0,p1)
return _core8(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y,p0,p1,<Py_ssize_t>0,<Py_ssize_t>0)
def percentile(np.ndarray[np.uint8_t, ndim=2] image,
@@ -230,7 +230,7 @@ def percentile(np.ndarray[np.uint8_t, ndim=2] image,
char shift_x=0, char shift_y=0, float p0=0., float p1=0.):
"""return p0 percentile
"""
return _core8p(kernel_percentile,image,selem,mask,out,shift_x,shift_y,p0,p1)
return _core8(kernel_percentile,image,selem,mask,out,shift_x,shift_y,p0,p1,<Py_ssize_t>0,<Py_ssize_t>0)
def pop(np.ndarray[np.uint8_t, ndim=2] image,
@@ -240,7 +240,7 @@ def pop(np.ndarray[np.uint8_t, ndim=2] image,
char shift_x=0, char shift_y=0, float p0=0., float p1=0.):
"""return nb of pixels between [p0 and p1]
"""
return _core8p(kernel_pop,image,selem,mask,out,shift_x,shift_y,p0,p1)
return _core8(kernel_pop,image,selem,mask,out,shift_x,shift_y,p0,p1,<Py_ssize_t>0,<Py_ssize_t>0)
def threshold(np.ndarray[np.uint8_t, ndim=2] image,
np.ndarray[np.uint8_t, ndim=2] selem,
@@ -249,4 +249,4 @@ def threshold(np.ndarray[np.uint8_t, ndim=2] image,
char shift_x=0, char shift_y=0, float p0=0., float p1=0.):
"""return 255 if g > percentile p0
"""
return _core8p(kernel_threshold,image,selem,mask,out,shift_x,shift_y,p0,p1)
return _core8(kernel_threshold,image,selem,mask,out,shift_x,shift_y,p0,p1,<Py_ssize_t>0,<Py_ssize_t>0)
-3
View File
@@ -13,7 +13,6 @@ def configuration(parent_package='', top_path=None):
cython(['_core8.pyx'], working_path=base_path)
cython(['_core8p.pyx'], working_path=base_path)
cython(['_core16.pyx'], working_path=base_path)
cython(['_core16p.pyx'], working_path=base_path)
cython(['_core16b.pyx'], working_path=base_path)
@@ -25,8 +24,6 @@ def configuration(parent_package='', top_path=None):
config.add_extension('_core8', sources=['_core8.c'],
include_dirs=[get_numpy_include_dirs()])
config.add_extension('_core8p', sources=['_core8p.c'],
include_dirs=[get_numpy_include_dirs()])
config.add_extension('_core16', sources=['_core16.c'],
include_dirs=[get_numpy_include_dirs()])
config.add_extension('_core16p', sources=['_core16p.c'],
+3 -3
View File
@@ -12,8 +12,8 @@ if __name__ == '__main__':
a16 = data.camera().astype(np.uint16)
selem = disk(10)
f8= rank.mean(a8,selem)
f16= rank.mean(a16,selem)
f8= rank.percentile_autolevel(a8,selem,p0=.0,p1=1.)
f16= rank.autolevel(a16,selem)
print f8==f16
@@ -21,7 +21,7 @@ if __name__ == '__main__':
plt.subplot(1,2,1)
plt.imshow(f16)
plt.subplot(1,2,2)
plt.imshow(f8-f16)
plt.imshow(f8)
plt.show()