Image.Polygons#
- classmethod Image.Polygons(polygons: Polygon2 | list[Polygon2] | tuple[Polygon2, ...], *, size: int | Sequence[int] | None = None, color: Any = 1, bg: Any = 0, shift: int = 0, dtype: str | np.dtype = 'uint8', colororder: str | None = None, like=None) Self[source]#
Create an image containing filled polygons
- Parameters:
size (int or 2-tuple, optional) – image size, defaults to None
polygons (
Polygon2or list ofPolygon2) – polygon or list of polygonscolor (int, float, optional) – pixel value for the polygons, defaults to 1
bg (int, float, optional) – pixel value for the background, defaults to 0
shift (int, optional) – number of fractional bits in the vertex coordinates, defaults to 0
dtype (str or NumPy dtype, optional) – image data type, defaults to “uint8”
colororder (str or None, optional) – color plane names for the output image, defaults to None
like (
Imageor None, optional) – template image supplying defaultsize,dtypeandcolororderwhen those are not given explicitly
- Returns:
image containing the polygons
- Return type:
The image is initialized to
bgand the pixels within the polygons are set tocolor.polygonscan be a singlePolygon2or a list ofPolygon2. Ifcoloris a single value then all polygons are filled with the same color, ifcoloris a list then each polygon is filled with the corresponding color.The vertices of the polygons are rounded to the nearest integer. To achieve sub-pixel accuracy the
shiftparameter is passed tofillPolyand allows the vertices to be specified with sub-pixel precision. For example, a coordinate of 11 with ashiftof 2 corresponds to a vertex at 11/4 = 2.75 pixels.Note
The polygon will be automatically clipped to the image boundaries, so vertices outside the image are allowed.
Warning
The integer vertices are considered to lie at the centre of the corresponding pixels, so a vertex at (10, 10) corresponds to the pixel at row 10, column 10. The polygon edges are considered to lie halfway between the pixels, so a square with vertices at (10, 10), (20, 10), (20, 20) and (10, 20) corresponds to a filled square of pixels with rows from 10 to 20 and columns from 10 to 20. It will have an area of 11x11 = 121 pixels, not 10x10 = 100 pixels.
Example:
>>> from machinevisiontoolbox import Image >>> from spatialmath import Polygon2 >>> p1 = Polygon2([(10, 10), (20, 10), (20, 20), (10, 20)]) >>> p2 = Polygon2([(30, 30), (40, 30), (40, 40), (30, 40)]) >>> img = Image.Polygons([p1, p2], size=50) >>> img Image(size=(50, 50), dtype=uint8)
- Seealso:
sm.Polygon2cv2.fillPoly