import torch


def binary_image_to_points(img: torch.Tensor, invert=False) -> torch.Tensor:
	"""
	Convert (~binary) image tensor to a list of mean-centred coordinates of the non-zero pixels.

	Args:
		img: [1, H, W] the image tensor
		invert: if true the input image will be inverted (e.g. when strokes are assumed to be zero and background one)

	Returns:
		the mean-centred coordinates

	"""

	if invert:
		img = torch.logical_not(img.squeeze(0))
	else:
		img = img.squeeze(0)

	coords = torch.nonzero(img).float()
	coords = coords - coords.mean(dim=0)

	return coords