-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from clEsperanto/min-max-along-points
Min max along points
- Loading branch information
Showing
14 changed files
with
275 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
pyclesperanto_prototype/_tier2/_generate_maximum_intensity_between_points_matrix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from .._tier0 import execute, plugin_function, Image, create_none, create_matrix_from_pointlists, create_like | ||
|
||
|
||
@plugin_function(output_creator=create_none) | ||
def generate_maximum_intensity_between_points_matrix(intensity_image: Image, pointlist: Image, touch_matrix: Image = None, | ||
maximum_intensity_matrix_destination: Image = None, | ||
num_samples: int = 10): | ||
"""Determine the maximum intensity between pairs of point coordinates and | ||
write them in a matrix. | ||
Parameters | ||
---------- | ||
intensity_image: Image | ||
image where the intensity will be measured | ||
pointlist: Image | ||
list of coordinates | ||
touch_matrix: Image, optional | ||
if only selected pairs should be measured, use this binary matrix to confige which | ||
maximum_intensity_matrix_destination: Image, optional | ||
matrix where the results are written ito | ||
num_samples: int, optional | ||
Number of samples to take along the line for averaging, default = 10 | ||
Returns | ||
------- | ||
average_intensity_matrix_destination | ||
""" | ||
from .._tier1 import set | ||
|
||
if maximum_intensity_matrix_destination is None: | ||
maximum_intensity_matrix_destination = create_matrix_from_pointlists(pointlist, pointlist) | ||
|
||
if touch_matrix is None: | ||
touch_matrix = create_like(maximum_intensity_matrix_destination) | ||
set(touch_matrix, 1) | ||
|
||
parameters = { | ||
"src_touch_matrix": touch_matrix, | ||
"src_pointlist": pointlist, | ||
"src_intensity": intensity_image, | ||
"dst_maximum_intensity_matrix": maximum_intensity_matrix_destination, | ||
"num_samples": int(num_samples) | ||
} | ||
|
||
execute(__file__, 'maximum_intensity_between_points_matrix_x.cl', 'maximum_intensity_between_points_matrix', touch_matrix.shape, parameters) | ||
|
||
return maximum_intensity_matrix_destination |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
pyclesperanto_prototype/_tier2/_generate_minimum_intensity_between_points_matrix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from .._tier0 import execute, plugin_function, Image, create_none, create_matrix_from_pointlists, create_like | ||
|
||
|
||
@plugin_function(output_creator=create_none) | ||
def generate_minimum_intensity_between_points_matrix(intensity_image: Image, pointlist: Image, touch_matrix: Image = None, | ||
minimum_intensity_matrix_destination: Image = None, | ||
num_samples: int = 10): | ||
"""Determine the minimum intensity between pairs of point coordinates and | ||
write them in a matrix. | ||
Parameters | ||
---------- | ||
intensity_image: Image | ||
image where the intensity will be measured | ||
pointlist: Image | ||
list of coordinates | ||
touch_matrix: Image, optional | ||
if only selected pairs should be measured, use this binary matrix to confige which | ||
minimum_intensity_matrix_destination: Image, optional | ||
matrix where the results are written ito | ||
num_samples: int, optional | ||
Number of samples to take along the line for averaging, default = 10 | ||
Returns | ||
------- | ||
average_intensity_matrix_destination | ||
""" | ||
from .._tier1 import set | ||
|
||
if minimum_intensity_matrix_destination is None: | ||
minimum_intensity_matrix_destination = create_matrix_from_pointlists(pointlist, pointlist) | ||
|
||
if touch_matrix is None: | ||
touch_matrix = create_like(minimum_intensity_matrix_destination) | ||
set(touch_matrix, 1) | ||
|
||
parameters = { | ||
"src_touch_matrix": touch_matrix, | ||
"src_pointlist": pointlist, | ||
"src_intensity": intensity_image, | ||
"dst_minimum_intensity_matrix": minimum_intensity_matrix_destination, | ||
"num_samples": int(num_samples) | ||
} | ||
|
||
execute(__file__, 'minimum_intensity_between_points_matrix_x.cl', 'minimum_intensity_between_points_matrix', touch_matrix.shape, parameters) | ||
|
||
return minimum_intensity_matrix_destination |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
pyclesperanto_prototype/_tier2/maximum_intensity_between_points_matrix_x.cl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
__kernel void maximum_intensity_between_points_matrix ( | ||
IMAGE_src_touch_matrix_TYPE src_touch_matrix, | ||
IMAGE_src_pointlist_TYPE src_pointlist, | ||
IMAGE_src_intensity_TYPE src_intensity, | ||
IMAGE_dst_maximum_intensity_matrix_TYPE dst_maximum_intensity_matrix, | ||
int num_samples | ||
) | ||
{ | ||
const sampler_t intsampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST; | ||
|
||
const int touch_x = get_global_id(0); | ||
const int touch_y = get_global_id(1); | ||
|
||
const int touching = READ_IMAGE(src_touch_matrix, intsampler, POS_src_touch_matrix_INSTANCE(touch_x, touch_y, 0, 0)).x; | ||
if (touching == 0 || touch_x == 0 || touch_y == 0) { | ||
WRITE_IMAGE (dst_maximum_intensity_matrix, POS_dst_maximum_intensity_matrix_INSTANCE(touch_x, touch_y,0,0), 0); | ||
return; | ||
} | ||
|
||
const float x1 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_x-1, 0, 0, 0)).x; | ||
const float y1 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_x-1, 1, 0, 0)).x; | ||
const float z1 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_x-1, 2, 0, 0)).x; | ||
|
||
const float x2 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_y-1, 0, 0, 0)).x; | ||
const float y2 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_y-1, 1, 0, 0)).x; | ||
const float z2 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_y-1, 2, 0, 0)).x; | ||
|
||
|
||
float4 directionVector = (float4){x2 - x1, y2 - y1, z2 - z1, 0}; | ||
|
||
// const float len = length(directionVector); | ||
directionVector.x = directionVector.x / (num_samples - 1); | ||
directionVector.y = directionVector.y / (num_samples - 1); | ||
directionVector.z = directionVector.z / (num_samples - 1); | ||
|
||
if (touch_x == 1 && touch_y == 2) { | ||
printf("DIR %f / %f \n", directionVector.x, directionVector.y); | ||
} | ||
|
||
int width = GET_IMAGE_WIDTH(src_intensity); | ||
int height = GET_IMAGE_HEIGHT(src_intensity); | ||
int depth = GET_IMAGE_DEPTH(src_intensity); | ||
|
||
float4 position = (float4){x1, y1, z1, 0}; | ||
float maximum = 0; | ||
|
||
for (int i = 0; i < num_samples; i++) { | ||
POS_src_intensity_TYPE pos = POS_src_intensity_INSTANCE((int)(position.x + 0.5), (int)(position.y + 0.5), (int)(position.z + 5), 0); | ||
|
||
float value = (float)(READ_IMAGE(src_intensity, intsampler, pos).x); | ||
if (maximum < value || i == 0) { | ||
maximum = value; | ||
} | ||
|
||
position = position + directionVector; | ||
} | ||
|
||
WRITE_IMAGE (dst_maximum_intensity_matrix, POS_dst_maximum_intensity_matrix_INSTANCE(touch_x, touch_y,0,0), maximum); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
pyclesperanto_prototype/_tier2/minimum_intensity_between_points_matrix_x.cl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
__kernel void minimum_intensity_between_points_matrix ( | ||
IMAGE_src_touch_matrix_TYPE src_touch_matrix, | ||
IMAGE_src_pointlist_TYPE src_pointlist, | ||
IMAGE_src_intensity_TYPE src_intensity, | ||
IMAGE_dst_minimum_intensity_matrix_TYPE dst_minimum_intensity_matrix, | ||
int num_samples | ||
) | ||
{ | ||
const sampler_t intsampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST; | ||
|
||
const int touch_x = get_global_id(0); | ||
const int touch_y = get_global_id(1); | ||
|
||
const int touching = READ_IMAGE(src_touch_matrix, intsampler, POS_src_touch_matrix_INSTANCE(touch_x, touch_y, 0, 0)).x; | ||
if (touching == 0 || touch_x == 0 || touch_y == 0) { | ||
WRITE_IMAGE (dst_minimum_intensity_matrix, POS_dst_minimum_intensity_matrix_INSTANCE(touch_x, touch_y,0,0), 0); | ||
return; | ||
} | ||
|
||
const float x1 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_x-1, 0, 0, 0)).x; | ||
const float y1 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_x-1, 1, 0, 0)).x; | ||
const float z1 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_x-1, 2, 0, 0)).x; | ||
|
||
const float x2 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_y-1, 0, 0, 0)).x; | ||
const float y2 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_y-1, 1, 0, 0)).x; | ||
const float z2 = READ_IMAGE(src_pointlist, intsampler, POS_src_pointlist_INSTANCE(touch_y-1, 2, 0, 0)).x; | ||
|
||
|
||
float4 directionVector = (float4){x2 - x1, y2 - y1, z2 - z1, 0}; | ||
|
||
// const float len = length(directionVector); | ||
directionVector.x = directionVector.x / (num_samples - 1); | ||
directionVector.y = directionVector.y / (num_samples - 1); | ||
directionVector.z = directionVector.z / (num_samples - 1); | ||
|
||
if (touch_x == 1 && touch_y == 2) { | ||
printf("DIR %f / %f \n", directionVector.x, directionVector.y); | ||
} | ||
|
||
int width = GET_IMAGE_WIDTH(src_intensity); | ||
int height = GET_IMAGE_HEIGHT(src_intensity); | ||
int depth = GET_IMAGE_DEPTH(src_intensity); | ||
|
||
float4 position = (float4){x1, y1, z1, 0}; | ||
float minimum = 0; | ||
|
||
for (int i = 0; i < num_samples; i++) { | ||
POS_src_intensity_TYPE pos = POS_src_intensity_INSTANCE((int)(position.x + 0.5), (int)(position.y + 0.5), (int)(position.z + 5), 0); | ||
|
||
float value = (float)(READ_IMAGE(src_intensity, intsampler, pos).x); | ||
if (minimum > value || i == 0) { | ||
minimum = value; | ||
} | ||
|
||
position = position + directionVector; | ||
} | ||
|
||
WRITE_IMAGE (dst_minimum_intensity_matrix, POS_dst_minimum_intensity_matrix_INSTANCE(touch_x, touch_y,0,0), minimum); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[metadata] | ||
name = pyclesperanto_prototype | ||
version = 0.23.1 | ||
version = 0.23.2 | ||
author = Robert Haase | ||
author_email = [email protected] | ||
url = https://github.com/clEsperanto/pyclesperanto_prototype | ||
|
27 changes: 27 additions & 0 deletions
27
tests/test_generate_maximum_intensity_between_points_matrix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
def test_generate_maximum_intensity_between_points_matrix(): | ||
import pyclesperanto_prototype as cle | ||
import numpy as np | ||
|
||
image = np.zeros((10, 10)) | ||
image[5:, :5] = 5 | ||
image[5:, 5:] = 10 | ||
|
||
coords = np.asarray([ | ||
[1, 1], # first point | ||
[8, 1], # second ... | ||
[1, 8], | ||
[8, 8] | ||
]).T | ||
|
||
touch_matrix = cle.generate_distance_matrix(coords, coords) > 0 | ||
|
||
average_intensity_matrix = cle.generate_maximum_intensity_between_points_matrix(image, coords, touch_matrix) | ||
|
||
reference = np.asarray([ | ||
[0., 0., 0., 0., 0.], | ||
[0., 0., 0., 5., 10.], | ||
[0., 0., 0., 5., 10.], | ||
[0., 5., 5., 0., 10], | ||
[0., 10., 10., 10, 0.]]) | ||
|
||
assert cle.array_equal(reference, average_intensity_matrix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tests/test_generate_minimum_intensity_between_points_matrix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
def test_generate_minimum_intensity_between_points_matrix(): | ||
import pyclesperanto_prototype as cle | ||
import numpy as np | ||
|
||
image = np.zeros((10, 10)) | ||
image[5:, :5] = 5 | ||
image[5:, 5:] = 10 | ||
|
||
coords = np.asarray([ | ||
[1, 1], # first point | ||
[8, 1], # second ... | ||
[1, 8], | ||
[8, 8] | ||
]).T | ||
|
||
touch_matrix = cle.generate_distance_matrix(coords, coords) > 0 | ||
|
||
average_intensity_matrix = cle.generate_minimum_intensity_between_points_matrix(image, coords, touch_matrix) | ||
|
||
reference = np.asarray([ | ||
[0., 0., 0., 0., 0.], | ||
[0., 0., 0., 0, 0.], | ||
[0., 0., 0., 0, 0.], | ||
[0., 0., 0., 0., 5.], | ||
[0., 0., 0., 5., 0.]]) | ||
|
||
assert cle.array_equal(reference, average_intensity_matrix) |