dpm-srm 0.1.0__cp310-cp310-macosx_14_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dpm-srm might be problematic. Click here for more details.

dpm_srm/__init__.py ADDED
@@ -0,0 +1,11 @@
1
+ from .py_srm import segment
2
+ from ._dpm_srm import SRM2D_u8, SRM2D_u16, SRM2D_u32, SRM3D_u8, SRM3D_u16, SRM3D_u32
3
+
4
+ __all__ = [
5
+ # C++ wrapped functions
6
+ "SRM2D_u8", "SRM2D_u16", "SRM2D_u32",
7
+ "SRM3D_u8", "SRM3D_u16", "SRM3D_u32",
8
+
9
+ # Python getter function
10
+ "segment",
11
+ ]
dpm_srm/py_srm.py ADDED
@@ -0,0 +1,110 @@
1
+ from ._dpm_srm import SRM2D_u8, SRM2D_u16, SRM2D_u32, SRM3D_u8, SRM3D_u16, SRM3D_u32
2
+ import numpy as np
3
+
4
+
5
+ def segment(image: np.ndarray, Q: float = 25., rescale: bool = True) -> np.ndarray:
6
+ """
7
+ Statistical Region Merging Segmentation as implemented in <NAME> et al.
8
+
9
+ Parameters:
10
+ ---
11
+ image: A 2D or 3D Numpy array of the image to be segmented
12
+ Q: Used as part of the merging criterion. Larger Q -> more regions. Default: 25.
13
+ rescale: Rescale the image to fit [0-dtype::max()]. This will provide better segmentation results. Default: True
14
+
15
+ Returns:
16
+ ---
17
+ segmented_image: A 2D or 3D Numpy array of the image with each region labelled.
18
+
19
+ """
20
+ srm_dtypes = ["uint8", "uint16", "uint32", "uint64"]
21
+ assert image.ndim == 2 or image.ndim == 3, f"Image must be 2D or 3D, not {image.ndim}D."
22
+ assert str(image.dtype) in srm_dtypes, f"Image must be of type {srm_dtypes}."
23
+
24
+ if rescale:
25
+ original_dtype = image.dtype
26
+ original_bytes = image.itemsize
27
+ try:
28
+ dtype_min = np.iinfo(original_dtype).min
29
+ bytes_max = 2**(original_bytes * 8) - 1
30
+
31
+ except ValueError:
32
+ dtype_min = np.finfo(original_dtype).min
33
+ bytes_max = 2**(original_bytes * 8) - 1
34
+
35
+ min_val = np.percentile(image, 1)
36
+ max_val = np.percentile(image, 99)
37
+ image[image < min_val] = min_val
38
+ image[image > max_val] = max_val
39
+ image = (image - min_val) / (max_val - min_val) * bytes_max + dtype_min
40
+ image = image.astype(original_dtype)
41
+
42
+ srm_funcs = {"2": {"uint8": SRM2D_u8,
43
+ "uint16": SRM2D_u16,
44
+ "uint32": SRM2D_u32},
45
+ "3": {"uint8": SRM3D_u8,
46
+ "uint16": SRM3D_u16,
47
+ "uint32": SRM3D_u32}}
48
+ srm_func = srm_funcs[str(image.ndim)][str(image.dtype)]
49
+ srm_obj = srm_func(image, Q)
50
+ srm_obj.segment()
51
+ segmentation = srm_obj.get_result()
52
+
53
+ return segmentation
54
+
55
+ # if __name__ == "__main__":
56
+ # import tifffile
57
+ # import matplotlib.pyplot as plt
58
+ # image = tifffile.imread("/mnt/d/Petrobras_Data/uCT/Depth_1/F4635H_CLEAN_P_13000nm_small.tif")
59
+ # image = image[:50, :50, :50]
60
+ # image = image.astype(np.uint32)
61
+
62
+ # # image = image.astype(np.float32)
63
+ # # min_val = np.percentile(image, 1)
64
+ # # max_val = np.percentile(image, 99)
65
+ # # image[image < min_val] = min_val
66
+ # # image[image > max_val] = max_val
67
+ # # image = (image - min_val) / (max_val - min_val) * 255 - 128
68
+ # # image = image.astype(np.uint8)
69
+ # # rescale = False
70
+
71
+ # # if rescale:
72
+ # # original_dtype = image.dtype
73
+ # # original_bytes = image.itemsize
74
+ # # try:
75
+ # # dtype_min = np.iinfo(original_dtype).min
76
+ # # bytes_max = 2**(original_bytes * 8) - 1
77
+
78
+ # # except ValueError:
79
+ # # dtype_min = np.finfo(original_dtype).min
80
+ # # bytes_max = 2**(original_bytes * 8) - 1
81
+
82
+ # # print(dtype_min, bytes_max)
83
+ # # min_val = np.percentile(image, 1)
84
+ # # max_val = np.percentile(image, 99)
85
+ # # image[image < min_val] = min_val
86
+ # # image[image > max_val] = max_val
87
+ # # image = (image - min_val) / (max_val - min_val) * bytes_max + dtype_min
88
+ # # image = image.astype(original_dtype)
89
+
90
+
91
+
92
+ # # print(np.amin(image), np.amax(image), image.dtype)
93
+
94
+ # segmentation = srm(image, Q=25, rescale=True)
95
+
96
+ # plt.figure()
97
+ # plt.imshow(image[20], cmap="Greys_r")
98
+ # plt.colorbar()
99
+
100
+
101
+ # plt.figure()
102
+ # plt.imshow(segmentation[20], cmap="Greys_r")
103
+ # plt.colorbar()
104
+
105
+ # plt.figure()
106
+ # plt.hist(image.flatten(), bins=50, range=(np.amin(image), np.amax(image)), density=True)
107
+ # plt.hist(segmentation.flatten(), bins=50, range=(np.amin(image), np.amax(image)), density=True)
108
+ # # plt.colorbar()
109
+ # plt.show()
110
+
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.1
2
+ Name: dpm_srm
3
+ Version: 0.1.0
4
+ Summary: Statistical Region Merging Segmentation
5
+ Author-Email: Digital Porous Media <bcchang@utexas.edu>
6
+ License: MIT
7
+ Requires-Python: >=3.8
8
+ Requires-Dist: numpy
@@ -0,0 +1,6 @@
1
+ dpm_srm/__init__.py,sha256=3PRYCBbGnevQdRoWk6Lwp4INySbtKBuxn_fW4BCb0pE,289
2
+ dpm_srm/py_srm.py,sha256=QYU7bQ_w5dYb9mdz6zEF-dA-75UcJyyvgW8k5KNViOU,4044
3
+ dpm_srm-0.1.0.dist-info/METADATA,sha256=KnoqOaSaLuqy-1RAcVt4OELDkC-mWKQ6Dq-4dMl1Jhs,213
4
+ dpm_srm-0.1.0.dist-info/WHEEL,sha256=VXk61i1nf3sOvr06rM-yjYh1D_tpHM2qhkUOd9d9DEg,114
5
+ dpm_srm-0.1.0.dist-info/licenses/LICENSE,sha256=DPkH2YUOqq6GA9joNyrjSii3kGn-7Wn2CMYV5rY-Gok,3073
6
+ dpm_srm-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: scikit-build-core 0.10.7
3
+ Root-Is-Purelib: false
4
+ Tag: cp310-cp310-macosx_14_0_arm64
5
+
@@ -0,0 +1,57 @@
1
+ BSD 2-Clause License
2
+
3
+ # Attribution
4
+ This project includes code adapted from Statistical Region Merging by Johannes Schindelin, licensed under the BSD 2-Clause License.
5
+
6
+ # Original License
7
+ Copyright (C) 2009 - 2013, Johannes Schindelin
8
+
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted provided that the following conditions are met:
11
+ *
12
+ * 1. Redistributions of source code must retain the above copyright notice,
13
+ * this list of conditions and the following disclaimer.
14
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
15
+ * this list of conditions and the following disclaimer in the documentation
16
+ * and/or other materials provided with the distribution.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ * POSSIBILITY OF SUCH DAMAGE.
29
+ *
30
+ * The views and conclusions contained in the software and documentation are
31
+ * those of the authors and should not be interpreted as representing official
32
+ * policies, either expressed or implied, of any organization.
33
+
34
+
35
+ # Our Contributions
36
+ Copyright (c) 2024, Digital Porous Media
37
+
38
+ Redistribution and use in source and binary forms, with or without
39
+ modification, are permitted provided that the following conditions are met:
40
+
41
+ 1. Redistributions of source code must retain the above copyright notice, this
42
+ list of conditions and the following disclaimer.
43
+
44
+ 2. Redistributions in binary form must reproduce the above copyright notice,
45
+ this list of conditions and the following disclaimer in the documentation
46
+ and/or other materials provided with the distribution.
47
+
48
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
52
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
55
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.