Return a noisy 3D image and segmentation. Args: height: height of the image. The value should be larger than `2 * rad_max`. width: width of the image. The value should be larger than `2 * rad_max`. depth: depth of the image. The value should be larger than `2 * rad_
(
height: int,
width: int,
depth: int,
num_objs: int = 12,
rad_max: int = 30,
rad_min: int = 5,
noise_max: float = 0.0,
num_seg_classes: int = 5,
channel_dim: int | None = None,
random_state: np.random.RandomState | None = None,
)
| 95 | |
| 96 | |
| 97 | def create_test_image_3d( |
| 98 | height: int, |
| 99 | width: int, |
| 100 | depth: int, |
| 101 | num_objs: int = 12, |
| 102 | rad_max: int = 30, |
| 103 | rad_min: int = 5, |
| 104 | noise_max: float = 0.0, |
| 105 | num_seg_classes: int = 5, |
| 106 | channel_dim: int | None = None, |
| 107 | random_state: np.random.RandomState | None = None, |
| 108 | ) -> tuple[np.ndarray, np.ndarray]: |
| 109 | """ |
| 110 | Return a noisy 3D image and segmentation. |
| 111 | |
| 112 | Args: |
| 113 | height: height of the image. The value should be larger than `2 * rad_max`. |
| 114 | width: width of the image. The value should be larger than `2 * rad_max`. |
| 115 | depth: depth of the image. The value should be larger than `2 * rad_max`. |
| 116 | num_objs: number of circles to generate. Defaults to `12`. |
| 117 | rad_max: maximum circle radius. Defaults to `30`. |
| 118 | rad_min: minimum circle radius. Defaults to `5`. |
| 119 | noise_max: if greater than 0 then noise will be added to the image taken from |
| 120 | the uniform distribution on range `[0,noise_max)`. Defaults to `0`. |
| 121 | num_seg_classes: number of classes for segmentations. Defaults to `5`. |
| 122 | channel_dim: if None, create an image without channel dimension, otherwise create |
| 123 | an image with channel dimension as first dim or last dim. Defaults to `None`. |
| 124 | random_state: the random generator to use. Defaults to `np.random`. |
| 125 | |
| 126 | Returns: |
| 127 | Randomised Numpy array with shape (`height`, `width`, `depth`) |
| 128 | |
| 129 | See also: |
| 130 | :py:meth:`~create_test_image_2d` |
| 131 | """ |
| 132 | |
| 133 | if rad_max <= rad_min: |
| 134 | raise ValueError(f"`rad_min` {rad_min} should be less than `rad_max` {rad_max}.") |
| 135 | if rad_min < 1: |
| 136 | raise ValueError("f`rad_min` {rad_min} should be no less than 1.") |
| 137 | min_size = min(height, width, depth) |
| 138 | if min_size <= 2 * rad_max: |
| 139 | raise ValueError(f"the minimal size {min_size} of the image should be larger than `2 * rad_max` 2x{rad_max}.") |
| 140 | |
| 141 | image = np.zeros((height, width, depth)) |
| 142 | rs: np.random.RandomState = np.random.random.__self__ if random_state is None else random_state # type: ignore |
| 143 | |
| 144 | for _ in range(num_objs): |
| 145 | x = rs.randint(rad_max, height - rad_max) |
| 146 | y = rs.randint(rad_max, width - rad_max) |
| 147 | z = rs.randint(rad_max, depth - rad_max) |
| 148 | rad = rs.randint(rad_min, rad_max) |
| 149 | spy, spx, spz = np.ogrid[-x : height - x, -y : width - y, -z : depth - z] |
| 150 | circle = (spx * spx + spy * spy + spz * spz) <= rad * rad |
| 151 | |
| 152 | if num_seg_classes > 1: |
| 153 | image[circle] = np.ceil(rs.random() * num_seg_classes) |
| 154 | else: |
searching dependent graphs…