mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-10 12:39:13 +08:00
group core8,8p and 8b
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
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
|
||||
# 8 bit core kernel receives extra information about data inferior and superior percentiles
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t),
|
||||
cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t, float, float, Py_ssize_t, Py_ssize_t),
|
||||
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)
|
||||
char shift_x, char shift_y, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1)
|
||||
|
||||
|
||||
+16
-9
@@ -14,6 +14,11 @@ 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
|
||||
#---------------------------------------------------------------------------
|
||||
@@ -31,12 +36,12 @@ cdef inline np.uint8_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols,Py_ssize_t r,
|
||||
else:
|
||||
return 0
|
||||
|
||||
cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t),
|
||||
cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t, float, float, Py_ssize_t, Py_ssize_t),
|
||||
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):
|
||||
char shift_x, char shift_y, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
""" Main loop, this function computes the histogram for each image point
|
||||
- data is uint8
|
||||
- result is uint8 casted
|
||||
@@ -78,7 +83,9 @@ char shift_x, char shift_y):
|
||||
|
||||
# define local variable types
|
||||
cdef Py_ssize_t r, c, rr, cc, s, value, local_max, i, even_row
|
||||
cdef float pop # number of pixels actually inside the neighborhood (float)
|
||||
|
||||
# number of pixels actually inside the neighborhood (float)
|
||||
cdef float pop
|
||||
|
||||
# allocate memory with malloc
|
||||
cdef Py_ssize_t max_se = srows*scols
|
||||
@@ -157,7 +164,7 @@ char shift_x, char shift_y):
|
||||
r = 0
|
||||
c = 0
|
||||
# kernel --------------------------------------------------------------------
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c])
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1)
|
||||
# kernel --------------------------------------------------------------------
|
||||
|
||||
# main loop
|
||||
@@ -181,14 +188,14 @@ char shift_x, char shift_y):
|
||||
pop -= 1.
|
||||
|
||||
# kernel --------------------------------------------------------------------
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c])
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1)
|
||||
# kernel --------------------------------------------------------------------
|
||||
|
||||
r += 1 # pass to the next row
|
||||
if r>=rows:
|
||||
break
|
||||
|
||||
# ---> north to south
|
||||
# ---> north to south
|
||||
for s in range(num_se_s):
|
||||
rr = r + se_s_r[s]
|
||||
cc = c + se_s_c[s]
|
||||
@@ -205,7 +212,7 @@ char shift_x, char shift_y):
|
||||
pop -= 1.
|
||||
|
||||
# kernel --------------------------------------------------------------------
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c])
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1)
|
||||
# kernel --------------------------------------------------------------------
|
||||
|
||||
# ---> east to west
|
||||
@@ -226,7 +233,7 @@ char shift_x, char shift_y):
|
||||
pop -= 1.
|
||||
|
||||
# kernel --------------------------------------------------------------------
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c])
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1)
|
||||
# kernel --------------------------------------------------------------------
|
||||
|
||||
r += 1 # pass to the next row
|
||||
@@ -250,7 +257,7 @@ char shift_x, char shift_y):
|
||||
pop -= 1.
|
||||
|
||||
# kernel --------------------------------------------------------------------
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c])
|
||||
out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1)
|
||||
# kernel --------------------------------------------------------------------
|
||||
|
||||
# release memory allocated by malloc
|
||||
|
||||
+28
-28
@@ -21,7 +21,7 @@ from _core8 cimport _core8
|
||||
# kernels uint8
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
cdef inline np.uint8_t kernel_autolevel(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
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 Py_ssize_t i,imin,imax,delta
|
||||
|
||||
if pop:
|
||||
@@ -41,7 +41,7 @@ cdef inline np.uint8_t kernel_autolevel(Py_ssize_t* histo, float pop, np.uint8_t
|
||||
else:
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_bottomhat(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
cdef inline np.uint8_t kernel_bottomhat(Py_ssize_t* histo, float pop, np.uint8_t g,float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
cdef Py_ssize_t i
|
||||
|
||||
for i in range(256):
|
||||
@@ -51,7 +51,7 @@ cdef inline np.uint8_t kernel_bottomhat(Py_ssize_t* histo, float pop, np.uint8_t
|
||||
return <np.uint8_t>(g-i)
|
||||
|
||||
|
||||
cdef inline np.uint8_t kernel_equalize(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
cdef inline np.uint8_t kernel_equalize(Py_ssize_t* histo, float pop, np.uint8_t g,float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
cdef Py_ssize_t i
|
||||
cdef float sum = 0.
|
||||
|
||||
@@ -65,7 +65,7 @@ cdef inline np.uint8_t kernel_equalize(Py_ssize_t* histo, float pop, np.uint8_t
|
||||
else:
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_gradient(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
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 Py_ssize_t i,imin,imax
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ cdef inline np.uint8_t kernel_gradient(Py_ssize_t* histo, float pop, np.uint8_t
|
||||
else:
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_maximum(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
cdef inline np.uint8_t kernel_maximum(Py_ssize_t* histo, float pop, np.uint8_t g,float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
cdef Py_ssize_t i
|
||||
|
||||
if pop:
|
||||
@@ -92,7 +92,7 @@ cdef inline np.uint8_t kernel_maximum(Py_ssize_t* histo, float pop, np.uint8_t g
|
||||
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_mean(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
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 Py_ssize_t i
|
||||
cdef float mean = 0.
|
||||
|
||||
@@ -103,7 +103,7 @@ cdef inline np.uint8_t kernel_mean(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
else:
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_meansubstraction(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
cdef inline np.uint8_t kernel_meansubstraction(Py_ssize_t* histo, float pop, np.uint8_t g,float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
cdef Py_ssize_t i
|
||||
cdef float mean = 0.
|
||||
|
||||
@@ -114,7 +114,7 @@ cdef inline np.uint8_t kernel_meansubstraction(Py_ssize_t* histo, float pop, np.
|
||||
else:
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_median(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
cdef inline np.uint8_t kernel_median(Py_ssize_t* histo, float pop, np.uint8_t g,float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
cdef Py_ssize_t i
|
||||
cdef float sum = pop/2.0
|
||||
|
||||
@@ -127,7 +127,7 @@ cdef inline np.uint8_t kernel_median(Py_ssize_t* histo, float pop, np.uint8_t g)
|
||||
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_minimum(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
cdef inline np.uint8_t kernel_minimum(Py_ssize_t* histo, float pop, np.uint8_t g,float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
cdef Py_ssize_t i
|
||||
|
||||
if pop:
|
||||
@@ -137,7 +137,7 @@ cdef inline np.uint8_t kernel_minimum(Py_ssize_t* histo, float pop, np.uint8_t g
|
||||
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_modal(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
cdef inline np.uint8_t kernel_modal(Py_ssize_t* histo, float pop, np.uint8_t g,float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
cdef Py_ssize_t hmax=0,imax=0
|
||||
|
||||
if pop:
|
||||
@@ -149,7 +149,7 @@ cdef inline np.uint8_t kernel_modal(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
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 Py_ssize_t i,imin,imax
|
||||
|
||||
if pop:
|
||||
@@ -168,10 +168,10 @@ cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t* histo, float pop, np.u
|
||||
else:
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_pop(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
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):
|
||||
return <np.uint8_t>(pop)
|
||||
|
||||
cdef inline np.uint8_t kernel_threshold(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
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 Py_ssize_t i
|
||||
cdef float mean = 0.
|
||||
|
||||
@@ -182,7 +182,7 @@ cdef inline np.uint8_t kernel_threshold(Py_ssize_t* histo, float pop, np.uint8_t
|
||||
else:
|
||||
return <np.uint8_t>(0)
|
||||
|
||||
cdef inline np.uint8_t kernel_tophat(Py_ssize_t* histo, float pop, np.uint8_t g):
|
||||
cdef inline np.uint8_t kernel_tophat(Py_ssize_t* histo, float pop, np.uint8_t g,float p0, float p1, Py_ssize_t s0, Py_ssize_t s1):
|
||||
cdef Py_ssize_t i
|
||||
|
||||
for i in range(255,-1,-1):
|
||||
@@ -201,7 +201,7 @@ def autolevel(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""bottom hat
|
||||
"""
|
||||
return _core8(kernel_autolevel,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_autolevel,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def bottomhat(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -210,7 +210,7 @@ def bottomhat(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""bottom hat
|
||||
"""
|
||||
return _core8(kernel_bottomhat,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_bottomhat,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def equalize(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -219,7 +219,7 @@ def equalize(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""local egalisation of the gray level
|
||||
"""
|
||||
return _core8(kernel_equalize,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_equalize,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def gradient(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -228,7 +228,7 @@ def gradient(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""local maximum - local minimum gray level
|
||||
"""
|
||||
return _core8(kernel_gradient,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_gradient,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def maximum(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -237,7 +237,7 @@ def maximum(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""local maximum gray level
|
||||
"""
|
||||
return _core8(kernel_maximum,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_maximum,image,selem,mask,out,shift_x,shift_y,.0,.0,<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,
|
||||
@@ -246,7 +246,7 @@ def mean(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""average gray level (clipped on uint8)
|
||||
"""
|
||||
return _core8(kernel_mean,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_mean,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def meansubstraction(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -255,7 +255,7 @@ def meansubstraction(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""(g - average gray level)/2+127 (clipped on uint8)
|
||||
"""
|
||||
return _core8(kernel_meansubstraction,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_meansubstraction,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def median(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -264,7 +264,7 @@ def median(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""local median
|
||||
"""
|
||||
return _core8(kernel_median,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_median,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def minimum(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -273,7 +273,7 @@ def minimum(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""local minimum gray level
|
||||
"""
|
||||
return _core8(kernel_minimum,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_minimum,image,selem,mask,out,shift_x,shift_y,.0,.0,<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,
|
||||
@@ -282,7 +282,7 @@ def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""morphological contrast enhancement
|
||||
"""
|
||||
return _core8(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def modal(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -291,7 +291,7 @@ def modal(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""local mode
|
||||
"""
|
||||
return _core8(kernel_modal,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_modal,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def pop(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -300,7 +300,7 @@ def pop(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""returns the number of actual pixels of the structuring element inside the mask
|
||||
"""
|
||||
return _core8(kernel_pop,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_pop,image,selem,mask,out,shift_x,shift_y,.0,.0,<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,
|
||||
@@ -309,7 +309,7 @@ def threshold(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""returns 255 if gray level higher than local mean, 0 else
|
||||
"""
|
||||
return _core8(kernel_threshold,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_threshold,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
def tophat(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
np.ndarray[np.uint8_t, ndim=2] selem,
|
||||
@@ -318,5 +318,5 @@ def tophat(np.ndarray[np.uint8_t, ndim=2] image,
|
||||
char shift_x=0, char shift_y=0):
|
||||
"""top hat
|
||||
"""
|
||||
return _core8(kernel_tophat,image,selem,mask,out,shift_x,shift_y)
|
||||
return _core8(kernel_tophat,image,selem,mask,out,shift_x,shift_y,.0,.0,<Py_ssize_t>0,<Py_ssize_t>0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user