code: def generate_binary_structure(rank: int, connectivity: int) -> Tensor:
      """Translated version of the function from scipy.ndimage.morphology.
  
      Args:
          rank: The rank of the structuring element.
          connectivity: The number of neighbors connected to a given pixel.
  
      Returns:
          The structuring element.
  
      Examples::
          >>> from torchmetrics.functional.segmentation.utils import generate_binary_structure
          >>> import torch
          >>> generate_binary_structure(2, 1)
          tensor([[False,  True, False],
                  [ True,  True,  True],
                  [False,  True, False]])
          >>> generate_binary_structure(2, 2)
          tensor([[True,  True,  True],
                  [True,  True,  True],
                  [True,  True,  True]])
          >>> generate_binary_structure(3, 2)  # doctest: +NORMALIZE_WHITESPACE
          tensor([[[False,  True, False],
                   [ True,  True,  True],
                   [False,  True, False]],
                  [[ True,  True,  True],
                   [ True,  True,  True],
                   [ True,  True,  True]],
                  [[False,  True, False],
                   [ True,  True,  True],
                   [False,  True, False]]])
  
      """
      if connectivity < 1:
          connectivity = 1
      if rank < 1:
          return torch.tensor([1], dtype=torch.uint8)
      grids = torch.meshgrid(
          [torch.arange(3) for _ in range(rank)], indexing="ij"
      )
      output = torch.abs(torch.stack(grids, dim=0) - 1)
      output = torch.sum(output, dim=0)
      return output <= connectivity
  
file_path: torchmetrics/src/torchmetrics/functional/segmentation/utils.py
imports: import functools
  import math
  from typing import List, Optional, Tuple, Union
  import torch
  from torch import Tensor
  from torch.nn.functional import conv2d, conv3d, pad, unfold
  from typing_extensions import Literal
  from torchmetrics.utilities.checks import _check_same_shape
  from torchmetrics.utilities.imports import _SCIPY_AVAILABLE
label_desc: Use image node_id 3 for calling a global function with example usage: generate_binary_structure(rank, connectivity) and returns: unknown, torch