remplace emask with is_in_mask function

This commit is contained in:
Olivier Debeir
2012-10-15 14:45:25 +02:00
parent e5e01e3b8f
commit ae73da922f
2 changed files with 79 additions and 53 deletions
+50 -53
View File
@@ -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 = <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
@@ -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
+29
View File
@@ -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()