Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Morph snake smoothing #296

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
410 changes: 410 additions & 0 deletions demo/smoothing.ipynb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyclesperanto_prototype/_tier1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
from ._greater_or_equal import greater_or_equal
from ._greater_or_equal_constant import greater_or_equal_constant
from ._hessian_eigenvalues import hessian_eigenvalues
from ._inferior_superior import inferior_superior
from ._laplace_box import laplace_box
from ._laplace_diamond import laplace_diamond
from ._logarithm import logarithm
Expand Down Expand Up @@ -161,6 +162,7 @@
from ._sum_x_projection import sum_x_projection
from ._sum_y_projection import sum_y_projection
from ._sum_z_projection import sum_z_projection
from ._superior_inferior import superior_inferior
from ._transpose_xy import transpose_xy
from ._transpose_xz import transpose_xz
from ._transpose_yz import transpose_yz
Expand Down
36 changes: 36 additions & 0 deletions pyclesperanto_prototype/_tier1/_inferior_superior.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from .._tier0 import execute, create_binary_like
from .._tier0 import plugin_function
from .._tier0 import Image

@plugin_function(categories=['binary processing'], output_creator=create_binary_like)
def inferior_superior(source : Image, destination : Image = None) -> Image:
"""Dilates the image respectively with a number of kernels and takes the minimum
value across all results for each pixel.

Parameters
----------
source : Image
destination : Image, optional

Returns
-------
destination

Examples
--------
>>> import pyclesperanto_prototype as cle
>>> cle.inferior_superior(source, destination)

References
----------
Implemented in inf_sup function in scikit morphological snakes:
.. [1] https://github.com/scikit-image/scikit-image/blob/00177e14097237ef20ed3141ed454bc81b308f82/skimage/segmentation/morphsnakes.py
"""

parameters = {
"src":source,
"dst":destination
}

execute(__file__, './inferior_superior_' + str(len(destination.shape)) + 'd_x.cl', 'inferior_superior_' + str(len(destination.shape)) + 'd', destination.shape, parameters)
return destination
36 changes: 36 additions & 0 deletions pyclesperanto_prototype/_tier1/_superior_inferior.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from .._tier0 import execute, create_binary_like
from .._tier0 import plugin_function
from .._tier0 import Image

@plugin_function(categories=['binary processing'], output_creator=create_binary_like)
def superior_inferior(source : Image, destination : Image = None) -> Image:
"""Erodes the image respectively with a number of kernels and takes the maximum
value across all results for each pixel.

Parameters
----------
source : Image
destination : Image, optional

Returns
-------
destination

Examples
--------
>>> import pyclesperanto_prototype as cle
>>> cle.inferior_superior(source, destination)

References
----------
Implemented in sup_inf function in scikit morphological snakes:
.. [1] https://github.com/scikit-image/scikit-image/blob/00177e14097237ef20ed3141ed454bc81b308f82/skimage/segmentation/morphsnakes.py
"""

parameters = {
"src":source,
"dst":destination
}

execute(__file__, './superior_inferior_' + str(len(destination.shape)) + 'd_x.cl', 'superior_inferior_' + str(len(destination.shape)) + 'd', destination.shape, parameters)
return destination
71 changes: 71 additions & 0 deletions pyclesperanto_prototype/_tier1/inferior_superior_2d_x.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;

__kernel void inferior_superior_2d (
IMAGE_src_TYPE src,
IMAGE_dst_TYPE dst
)
{
const int x = get_global_id(0);
const int y = get_global_id(1);

const int2 pos = (int2){x,y};

float value = READ_src_IMAGE(src, sampler, pos).x;

// if value is already 1, dilate will return 1
if (value == 1) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(1));
return;
}

/* Dilate with kernel [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, 1})).x;
if (value == 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, -1})).x;
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}
}

/* Dilate with kernel [[0, 1, 0],
[0, 1, 0],
[0, 1, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){0, 1})).x;
if (value == 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){0, -1})).x;
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}
}

/* Dilate with kernel [[0, 0, 1],
[0, 1, 0],
[1, 0, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, 1})).x;
if (value == 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, -1})).x;
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}
}

/* Dilate with kernel [[0, 0, 0],
[1, 1, 1],
[0, 0, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, 0})).x;
if (value == 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, 0})).x;
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}
}

// If all dilates are 1 then return 1
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(1));
}
181 changes: 181 additions & 0 deletions pyclesperanto_prototype/_tier1/inferior_superior_3d_x.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;

__kernel void inferior_superior_3d (
IMAGE_src_TYPE src,
IMAGE_dst_TYPE dst
)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
const int z = get_global_id(2);

const int4 pos = (int4){x, y, z, 0};

float value = READ_src_IMAGE(src, sampler, pos).x;

// if value is already 0, erode will return 0
if (value != 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(1));
return;
}

// printf("pixel at coord x%i y%i z%i\n", x, y, z);


// P0
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){i, j, 0, 0})).x;
// printf("value %i\n", value);
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// P1
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){i, 0, j, 0})).x;
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// P2
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){0, i, j, 0})).x;
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// P3
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){i, j, j, 0})).x;
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// P4
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){j, i, -i, 0})).x;
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// P5
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){i, j, i, 0})).x;
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// P6
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){i, j, -i, 0})).x;
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// P7
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){i, i, j, 0})).x;
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// P8
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
value = READ_src_IMAGE(src, sampler, (pos + (int4){i, -i, j, 0})).x;
if (value != 0) {
break;
}
}
if (value != 0) {
break;
}
}
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
}

// If all erodes are 0 then return 0
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(1));
}
Loading