"""Generate a mask from a polygon. Args: polygon (np.ndarray): The polygon for which the mask should be generated, given as a list of vertices. resolution_wh (Tuple[int, int]): The width and height of the desired resolution. Returns: np.ndarray: The generated 2D mask, where the polygon is marked with `1`'s and the rest is filled with `0`'s. """
时间: 2024-02-14 11:30:18 浏览: 25
根据你提供的函数说明,你可以使用以下代码来实现 `extract_yolov8_masks` 函数:
```python
import numpy as np
from PIL import Image, ImageDraw
def extract_yolov8_masks(yolov8_results):
resolution_wh = (yolov8_results.image_width, yolov8_results.image_height)
masks = []
for polygon in yolov8_results.polygons:
mask = np.zeros(resolution_wh, dtype=np.uint8)
image = Image.fromarray(mask)
draw = ImageDraw.Draw(image)
draw.polygon(polygon, fill=1)
mask = np.array(image, dtype=np.uint8)
masks.append(mask)
return masks
```
这个函数将根据传入的 `yolov8_results` 对象中的多边形信息生成相应的掩码。它会遍历多边形列表,创建一个与图像分辨率相同的全零矩阵作为初始掩码,然后使用 `ImageDraw` 库将多边形区域填充为 `1`,最后将掩码添加到结果列表中并返回。
请确保已经正确导入了 `numpy` 和 `PIL` 库以及它们的相应模块。如果还有其他要求或需要进一步的帮助,请提供更多详细信息。