Get image space UV grid, ranging in [0, 1]. >>> image_uv(10, 10): [[[0.05, 0.05], [0.15, 0.05], ..., [0.95, 0.05]], [[0.05, 0.15], [0.15, 0.15], ..., [0.95, 0.15]], ... ... ... [[0.05, 0.95], [0.15, 0.95], ..., [0.95, 0.95]]] ### Param
(
height: int = None,
width: int = None,
mask: np.ndarray = None,
left: int = None,
top: int = None,
right: int = None,
bottom: int = None,
dtype: np.dtype = np.float32
)
| 342 | |
| 343 | |
| 344 | def image_uv( |
| 345 | height: int = None, |
| 346 | width: int = None, |
| 347 | mask: np.ndarray = None, |
| 348 | left: int = None, |
| 349 | top: int = None, |
| 350 | right: int = None, |
| 351 | bottom: int = None, |
| 352 | dtype: np.dtype = np.float32 |
| 353 | ) -> np.ndarray: |
| 354 | """ |
| 355 | Get image space UV grid, ranging in [0, 1]. |
| 356 | |
| 357 | >>> image_uv(10, 10): |
| 358 | [[[0.05, 0.05], [0.15, 0.05], ..., [0.95, 0.05]], |
| 359 | [[0.05, 0.15], [0.15, 0.15], ..., [0.95, 0.15]], |
| 360 | ... ... ... |
| 361 | [[0.05, 0.95], [0.15, 0.95], ..., [0.95, 0.95]]] |
| 362 | |
| 363 | ### Parameters |
| 364 | * `width (int)`: image width |
| 365 | * `height (int)`: image height |
| 366 | * `mask (np.ndarray, optional)`: binary mask of shape (height, width), dtype=bool. Defaults to None. |
| 367 | If provided, the UV grid will be computed only for the masked pixels. |
| 368 | For 2-D mask, results is identical to image_ image_uv(height, width)[mask] |
| 369 | Extra dimensions other than the last two will be treated as batch dimensions |
| 370 | |
| 371 | ### Returns |
| 372 | * `*batch_indices (np.ndarray, optional)`: only available when mask is provided and has more than 2 dimensions. |
| 373 | * `uv (np.ndarray)`: shape (height, width, 2) if mask is None, otherwise (N, 2) |
| 374 | """ |
| 375 | if left is None: left = 0 |
| 376 | if top is None: top = 0 |
| 377 | if right is None: right = width |
| 378 | if bottom is None: bottom = height |
| 379 | if mask is None: |
| 380 | assert width is not None and height is not None, "either mask or width and height should be provided" |
| 381 | u = np.linspace((left + 0.5) / width, (right - 0.5) / width, right - left, dtype=dtype) |
| 382 | v = np.linspace((top + 0.5) / height, (bottom - 0.5) / height, bottom - top, dtype=dtype) |
| 383 | u, v = np.meshgrid(u, v, indexing='xy') |
| 384 | return np.stack([u, v], axis=2) |
| 385 | else: |
| 386 | assert (width is None or width == mask.shape[-1]) and (height is None or height == mask.shape[-2]), "width and height should be consistent with mask" |
| 387 | height, width = mask.shape[-2:] |
| 388 | *batch, i, j = np.where(mask) |
| 389 | u, v = (j.astype(dtype) + 0.5) / width, (i.astype(dtype) + 0.5) / height |
| 390 | return *batch, np.stack([u, v], axis=-1) |
| 391 | |
| 392 | |
| 393 | def image_pixel_center( |
no outgoing calls
no test coverage detected