scikit-image

Форк
0
/
_binary_blobs.py 
61 строка · 2.1 Кб
1
import numpy as np
2

3
from .._shared.filters import gaussian
4

5

6
def binary_blobs(
7
    length=512, blob_size_fraction=0.1, n_dim=2, volume_fraction=0.5, rng=None
8
):
9
    """
10
    Generate synthetic binary image with several rounded blob-like objects.
11

12
    Parameters
13
    ----------
14
    length : int, optional
15
        Linear size of output image.
16
    blob_size_fraction : float, optional
17
        Typical linear size of blob, as a fraction of ``length``, should be
18
        smaller than 1.
19
    n_dim : int, optional
20
        Number of dimensions of output image.
21
    volume_fraction : float, default 0.5
22
        Fraction of image pixels covered by the blobs (where the output is 1).
23
        Should be in [0, 1].
24
    rng : {`numpy.random.Generator`, int}, optional
25
        Pseudo-random number generator.
26
        By default, a PCG64 generator is used (see :func:`numpy.random.default_rng`).
27
        If `rng` is an int, it is used to seed the generator.
28

29
    Returns
30
    -------
31
    blobs : ndarray of bools
32
        Output binary image
33

34
    Examples
35
    --------
36
    >>> from skimage import data
37
    >>> data.binary_blobs(length=5, blob_size_fraction=0.2)  # doctest: +SKIP
38
    array([[ True, False,  True,  True,  True],
39
           [ True,  True,  True, False,  True],
40
           [False,  True, False,  True,  True],
41
           [ True, False, False,  True,  True],
42
           [ True, False, False, False,  True]])
43
    >>> blobs = data.binary_blobs(length=256, blob_size_fraction=0.1)
44
    >>> # Finer structures
45
    >>> blobs = data.binary_blobs(length=256, blob_size_fraction=0.05)
46
    >>> # Blobs cover a smaller volume fraction of the image
47
    >>> blobs = data.binary_blobs(length=256, volume_fraction=0.3)
48

49
    """
50

51
    rs = np.random.default_rng(rng)
52
    shape = tuple([length] * n_dim)
53
    mask = np.zeros(shape)
54
    n_pts = max(int(1.0 / blob_size_fraction) ** n_dim, 1)
55
    points = (length * rs.random((n_dim, n_pts))).astype(int)
56
    mask[tuple(indices for indices in points)] = 1
57
    mask = gaussian(
58
        mask, sigma=0.25 * length * blob_size_fraction, preserve_range=False
59
    )
60
    threshold = np.percentile(mask, 100 * (1 - volume_fraction))
61
    return np.logical_not(mask < threshold)
62

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.