Reference: Synthetic Image Generator for YOLO#
Backend for the Image Generator notebook
Original authors(s): Diptabrata Paul, Martin Fränzl Edited by: Till Pfaff
getRandom#
def getRandom(parameters: Sequence[Dict[str, Union[Number, str,
Sequence[Number]]]],
image_size: Union[int, Tuple[int, int]],
distance: float = 0,
distance_consider_object_size: bool = False,
offset: float = 0,
rng: np.random.Generator = np.random.default_rng(),
max_tries: int = 10000) -> pd.DataFrame
Generate a list of random objects with specified properties.
Arguments:
parameters
Sequence[Dict[str, Union[Number, str, Sequence[Number]]]] - A list of dictionaries, each specifying the properties of the objects to generate. Each dictionary may look like this:
{
"label": "object_type", # Mandatory, string label for the object type
"n": ["uniform", (min_value, max_value)], # Mandatory, number of objects to generate, follows the same stle as the other parameters
# Other parameters can be defined as follows:
"some-parameter1": ["uniform", (min_value, max_value)], # Uniform distribution
"some-parameter2": ["gaussian", (mean, std_dev)], # Gaussian distribution
"some-parameter3": [my_function, (*args)], # Custom function with arguments
"some-parameter4": 0.5, # Fixed value
...
}
image_size
Union[int, Tuple[int, int]] - Size of the image frame. Can be an integer (square) or a tuple (height, width).distance
float, optional - Minimum distance between objects. Defaults to 0.distance_consider_object_size
bool, optional - Include the object size (mean of obj. width and height) into the distance calculation. Defaults to False.offset
float, optional - Offset from the image border within which objects cannot be placed. Defaults to 0.rng
np.random.Generator, optional - Random number generator for reproducibility. Defaults to np.random.default_rng().max_tries
int, optional - Maximum number of attempts to place an object while respecting distance and offset. Defaults to 10000.
Returns:
pd.DataFrame
- A DataFrame containing the list of objects with random positions and parameters.
generateImage#
def generateImage(
objects: pd.DataFrame,
image_size: Union[int, Tuple[int, int]],
refstack: np.ndarray = None,
noise: Union[float, List[float]] = None,
snr: Union[float, List[float]] = None,
refstack_center: Tuple[float, float] = None,
rng: np.random.Generator = np.random.default_rng(),
background: float = 2e4
) -> Tuple[List[np.ndarray], List[str], List[float], np.ndarray]
Generates a synthetic image with the specified objects and parameters
Arguments:
objects
pd.DataFrame - Objects to be added to the imageimage_size
Union[int, Tuple[int, int]] - Size of the image frame. Eitherint
for a square size frame or(y: int,x: int)
for a rectangular imagerefstack
np.ndarray, optional - Reference stack to be used for generating the image.noise
Union[float, List[float]], optional - Standard deviation of the Gaussian noise to be added to the image. If a list is provided, it will be randomly selected from the range.snr
Union[float, List[float]], optional - Signal-to-noise ratio range. If a list is provided, it will be randomly selected from the range. If neithernoise
norsnr
is provided, the image will be generated without noise.refstack_center
Tuple[float, float], optional - center point (y,x) for the refstack, if it is not defined, the center of the image will be used. The coordinates must be in the range of the refstack image size.rng
np.random.Generator, optional - Random number generator to be used. Defaults tonp.random.default_rng()
.background
float, optional - Background intensity of the image. Defaults to 2e4.
Returns:
np.ndarray
- the generated image
place_ripples#
def place_ripples(image: np.ndarray, ripples: pd.DataFrame,
refstack: np.ndarray, refstack_center: Tuple[float, float],
image_size: Tuple[int, int]) -> None
Places ripples in the image based on the provided DataFrame of ripples.
place_others#
def place_others(image: np.ndarray, objects: pd.DataFrame,
image_size: Tuple[int, int]) -> None
Places other objects in the image based on the provided DataFrame of objects.