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

Custom read zero outside image #301

Open
wants to merge 7 commits into
base: morph_snake_smoothing
Choose a base branch
from
Open
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
42 changes: 20 additions & 22 deletions demo/smoothing.ipynb

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pyclesperanto_prototype/_tier1/_inferior_superior.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def inferior_superior(source : Image, destination : Image = None) -> Image:
Implemented in inf_sup function in scikit morphological snakes:
.. [1] https://github.com/scikit-image/scikit-image/blob/00177e14097237ef20ed3141ed454bc81b308f82/skimage/segmentation/morphsnakes.py
"""
import numpy as np
if source.dtype != np.uint8:
# the read function in the kernel below is custom and only supports images of type uint8
source = source.astype(np.uint8)

parameters = {
"src":source,
Expand Down
4 changes: 4 additions & 0 deletions pyclesperanto_prototype/_tier1/_superior_inferior.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def superior_inferior(source : Image, destination : Image = None) -> Image:
Implemented in sup_inf function in scikit morphological snakes:
.. [1] https://github.com/scikit-image/scikit-image/blob/00177e14097237ef20ed3141ed454bc81b308f82/skimage/segmentation/morphsnakes.py
"""
import numpy as np
if source.dtype != np.uint8:
# the read function in the kernel below is custom and only supports images of type uint8
source = source.astype(np.uint8)

parameters = {
"src":source,
Expand Down
30 changes: 21 additions & 9 deletions pyclesperanto_prototype/_tier1/inferior_superior_2d_x.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;

#define READ_IMAGE_ZERO_OUTSIDE(a,b,c) read_buffer2duc_zero_outside(GET_IMAGE_WIDTH(a),GET_IMAGE_HEIGHT(a),GET_IMAGE_DEPTH(a),a,b,c)

inline uchar2 read_buffer2duc_zero_outside(int read_buffer_width, int read_buffer_height, int read_buffer_depth, __global uchar * buffer_var, sampler_t sampler, int2 position )
{
int2 pos = (int2){position.x, position.y};
int pos_in_buffer = pos.x + pos.y * read_buffer_width;
if (pos.x < 0 || pos.x >= read_buffer_width || pos.y < 0 || pos.y >= read_buffer_height) {
return (uchar2){0, 0};
}
return (uchar2){buffer_var[pos_in_buffer],0};
}

__kernel void inferior_superior_2d (
IMAGE_src_TYPE src,
IMAGE_dst_TYPE dst
Expand All @@ -10,7 +22,7 @@ __kernel void inferior_superior_2d (

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

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

// if value is already 1, dilate will return 1
if (value == 1) {
Expand All @@ -21,9 +33,9 @@ __kernel void inferior_superior_2d (
/* Dilate with kernel [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, 1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){1, 1})).x;
if (value == 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, -1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){-1, -1})).x;
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
Expand All @@ -33,9 +45,9 @@ __kernel void inferior_superior_2d (
/* Dilate with kernel [[0, 1, 0],
[0, 1, 0],
[0, 1, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){0, 1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){0, 1})).x;
if (value == 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){0, -1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){0, -1})).x;
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
Expand All @@ -45,9 +57,9 @@ __kernel void inferior_superior_2d (
/* Dilate with kernel [[0, 0, 1],
[0, 1, 0],
[1, 0, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, 1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){-1, 1})).x;
if (value == 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, -1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){1, -1})).x;
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
Expand All @@ -57,9 +69,9 @@ __kernel void inferior_superior_2d (
/* Dilate with kernel [[0, 0, 0],
[1, 1, 1],
[0, 0, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, 0})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){1, 0})).x;
if (value == 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, 0})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){-1, 0})).x;
if (value == 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(0));
return;
Expand Down
33 changes: 23 additions & 10 deletions pyclesperanto_prototype/_tier1/inferior_superior_3d_x.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;


#define READ_IMAGE_ZERO_OUTSIDE(a,b,c) read_buffer3duc_zero_outside(GET_IMAGE_WIDTH(a),GET_IMAGE_HEIGHT(a),GET_IMAGE_DEPTH(a),a,b,c)

inline uchar2 read_buffer3duc_zero_outside(int read_buffer_width, int read_buffer_height, int read_buffer_depth, __global uchar * buffer_var, sampler_t sampler, int4 position )
{
int4 pos = (int4){position.x, position.y, position.z, 0};
int pos_in_buffer = pos.x + pos.y * read_buffer_width + pos.z * read_buffer_width * read_buffer_height;
if (pos.x < 0 || pos.x >= read_buffer_width || pos.y < 0 || pos.y >= read_buffer_height || pos.z < 0 || pos.z >= read_buffer_depth) {
return (uchar2){0, 0};
}
return (uchar2){buffer_var[pos_in_buffer],0};
}

__kernel void inferior_superior_3d (
IMAGE_src_TYPE src,
IMAGE_dst_TYPE dst
Expand All @@ -11,7 +24,7 @@ __kernel void inferior_superior_3d (

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

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

// if value is already 0, erode will return 0
if (value != 0) {
Expand All @@ -25,7 +38,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, j, 0, 0})).x;
// printf("value %i\n", value);
if (value != 0) {
break;
Expand All @@ -43,7 +56,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, 0, j, 0})).x;
if (value != 0) {
break;
}
Expand All @@ -60,7 +73,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){0, i, j, 0})).x;
if (value != 0) {
break;
}
Expand All @@ -77,7 +90,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, j, j, 0})).x;
if (value != 0) {
break;
}
Expand All @@ -94,7 +107,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){j, i, -i, 0})).x;
if (value != 0) {
break;
}
Expand All @@ -111,7 +124,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, j, i, 0})).x;
if (value != 0) {
break;
}
Expand All @@ -128,7 +141,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, j, -i, 0})).x;
if (value != 0) {
break;
}
Expand All @@ -145,7 +158,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, i, j, 0})).x;
if (value != 0) {
break;
}
Expand All @@ -162,7 +175,7 @@ __kernel void inferior_superior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, -i, j, 0})).x;
if (value != 0) {
break;
}
Expand Down
32 changes: 23 additions & 9 deletions pyclesperanto_prototype/_tier1/superior_inferior_2d_x.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;


#define READ_IMAGE_ZERO_OUTSIDE(a,b,c) read_buffer2duc_zero_outside(GET_IMAGE_WIDTH(a),GET_IMAGE_HEIGHT(a),GET_IMAGE_DEPTH(a),a,b,c)

inline uchar2 read_buffer2duc_zero_outside(int read_buffer_width, int read_buffer_height, int read_buffer_depth, __global uchar * buffer_var, sampler_t sampler, int2 position )
{
int2 pos = (int2){position.x, position.y};
int pos_in_buffer = pos.x + pos.y * read_buffer_width;
if (pos.x < 0 || pos.x >= read_buffer_width || pos.y < 0 || pos.y >= read_buffer_height) {
return (uchar2){0, 0};
}
return (uchar2){buffer_var[pos_in_buffer],0};
}


__kernel void superior_inferior_2d (
IMAGE_src_TYPE src,
IMAGE_dst_TYPE dst
Expand All @@ -10,7 +24,7 @@ __kernel void superior_inferior_2d (

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

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

// if value is already 0, erode will return 0
if (value == 0) {
Expand All @@ -21,9 +35,9 @@ __kernel void superior_inferior_2d (
/* Erode with kernel [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, 1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){1, 1})).x;
if (value != 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, -1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){-1, -1})).x;
if (value != 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(1));
return;
Expand All @@ -33,9 +47,9 @@ __kernel void superior_inferior_2d (
/* Erode with kernel [[0, 1, 0],
[0, 1, 0],
[0, 1, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){0, 1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){0, 1})).x;
if (value != 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){0, -1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){0, -1})).x;
if (value != 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(1));
return;
Expand All @@ -45,9 +59,9 @@ __kernel void superior_inferior_2d (
/* Erode with kernel [[0, 0, 1],
[0, 1, 0],
[1, 0, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, 1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){-1, 1})).x;
if (value != 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, -1})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){1, -1})).x;
if (value != 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(1));
return;
Expand All @@ -57,9 +71,9 @@ __kernel void superior_inferior_2d (
/* Erode with kernel [[0, 0, 0],
[1, 1, 1],
[0, 0, 0]] */
value = READ_src_IMAGE(src, sampler, (pos + (int2){1, 0})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){1, 0})).x;
if (value != 0) {
value = READ_src_IMAGE(src, sampler, (pos + (int2){-1, 0})).x;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int2){-1, 0})).x;
if (value != 0) {
WRITE_dst_IMAGE(dst, pos, CONVERT_dst_PIXEL_TYPE(1));
return;
Expand Down
30 changes: 21 additions & 9 deletions pyclesperanto_prototype/_tier1/superior_inferior_3d_x.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;

#define READ_IMAGE_ZERO_OUTSIDE(a,b,c) read_buffer3duc_zero_outside(GET_IMAGE_WIDTH(a),GET_IMAGE_HEIGHT(a),GET_IMAGE_DEPTH(a),a,b,c)

inline uchar2 read_buffer3duc_zero_outside(int read_buffer_width, int read_buffer_height, int read_buffer_depth, __global uchar * buffer_var, sampler_t sampler, int4 position )
{
int4 pos = (int4){position.x, position.y, position.z, 0};
int pos_in_buffer = pos.x + pos.y * read_buffer_width + pos.z * read_buffer_width * read_buffer_height;
if (pos.x < 0 || pos.x >= read_buffer_width || pos.y < 0 || pos.y >= read_buffer_height || pos.z < 0 || pos.z >= read_buffer_depth) {
return (uchar2){0, 0};
}
return (uchar2){buffer_var[pos_in_buffer],0};
}

__kernel void superior_inferior_3d (
IMAGE_src_TYPE src,
IMAGE_dst_TYPE dst
Expand All @@ -25,7 +37,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, j, 0, 0})).x;
// printf("value %i\n", value);
if (value == 0) {
break;
Expand All @@ -43,7 +55,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, 0, j, 0})).x;
if (value == 0) {
break;
}
Expand All @@ -60,7 +72,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){0, i, j, 0})).x;
if (value == 0) {
break;
}
Expand All @@ -77,7 +89,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, j, j, 0})).x;
if (value == 0) {
break;
}
Expand All @@ -94,7 +106,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){j, i, -i, 0})).x;
if (value == 0) {
break;
}
Expand All @@ -111,7 +123,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, j, i, 0})).x;
if (value == 0) {
break;
}
Expand All @@ -128,7 +140,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, j, -i, 0})).x;
if (value == 0) {
break;
}
Expand All @@ -145,7 +157,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, i, j, 0})).x;
if (value == 0) {
break;
}
Expand All @@ -162,7 +174,7 @@ __kernel void superior_inferior_3d (
// 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;
value = READ_IMAGE_ZERO_OUTSIDE(src, sampler, (pos + (int4){i, -i, j, 0})).x;
if (value == 0) {
break;
}
Expand Down
Loading