histo_kit.utils.image

Functions

gaussian_window(h, w[, sigma])

Create a 2D Gaussian weighting window with values normalized to [0, 1].

to_tensor_x(x, **kwargs)

Convert an RGB image from a NumPy array to a PyTorch tensor.

histo_kit.utils.image.gaussian_window(h, w, sigma=None)[source]

Create a 2D Gaussian weighting window with values normalized to [0, 1].

The window is symmetric and smoothly attenuates values towards the edges. It is used to blend overlapping patches in whole-slide inference to avoid visible stitching artifacts.

Parameters:
  • h (int) – Height of the window.

  • w (int) – Width of the window.

  • sigma (float, optional) – Standard deviation controlling the spread of the Gaussian. If None (default), the value is set to 0.5 * max(h, w).

Returns:

A 2D Gaussian window normalized such that its maximum value is 1.

Return type:

ndarray of shape (h, w)

Notes

  • The Gaussian is computed independently in x and y dimensions and combined via an outer product.

  • Normalization ensures the central peak equals 1.

Examples

>>> win = gaussian_window(5, 5)
>>> win.shape
(5, 5)
>>> win.max()
1.0
histo_kit.utils.image.to_tensor_x(x, **kwargs)[source]

Convert an RGB image from a NumPy array to a PyTorch tensor.

This function transposes the image from HWC (height, width, channels) format to CHW (channels, height, width) format and casts it to float32, preparing it for input into PyTorch models.

Parameters:
  • x (ndarray of shape (H, W, C)) – RGB image as a NumPy array.

  • **kwargs – Additional keyword arguments (currently unused).

Returns:

tensor – Transposed and type-cast tensor suitable for PyTorch.

Return type:

ndarray of dtype float32, shape (C, H, W)

Notes

  • The function does not normalize pixel values (e.g., to [0,1]). This should be done separately if required by the model.

  • This is a lightweight function and does not create a true torch.Tensor. To convert to a PyTorch tensor, use torch.from_numpy(to_tensor_x(x)).

Examples

>>> x_tensor = to_tensor_x(image_np)
>>> print(x_tensor.shape)  # (3, H, W)