Drawing#

kornia.image.draw_line(image, p1, p2, color)[source]#

Draw a single line into an image.

Parameters:
  • image (Tensor) – the input image to where to draw the lines with shape :math`(C,H,W)`.

  • p1 (Tensor) – the start point [x y] of the line with shape (2, ) or (B, 2).

  • p2 (Tensor) – the end point [x y] of the line with shape (2, ) or (B, 2).

  • color (Tensor) – the color of the line with shape :math`(C)` where :math`C` is the number of channels of the image.

Return type:

Tensor

Returns:

the image with containing the line.

Examples

>>> image = torch.zeros(1, 8, 8)
>>> draw_line(image, torch.tensor([6, 4]), torch.tensor([1, 4]), torch.tensor([255]))
tensor([[[  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0., 255., 255., 255., 255., 255., 255.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.]]])
kornia.image.draw_rectangle(image, rectangle, color=None, fill=None)[source]#

Draw N rectangles on a batch of image tensors.

Parameters:
  • image (Tensor) – is tensor of BxCxHxW.

  • rectangle (Tensor) – represents number of rectangles to draw in BxNx4 N is the number of boxes to draw per batch index[x1, y1, x2, y2] 4 is in (top_left.x, top_left.y, bot_right.x, bot_right.y).

  • color (Optional[Tensor], optional) – a size 1, size 3, BxNx1, or BxNx3 tensor. If C is 3, and color is 1 channel it will be broadcasted. Default: None

  • fill (Optional[bool], optional) – is a flag used to fill the boxes with color if True. Default: None

Return type:

Tensor

Returns:

This operation modifies image inplace but also returns the drawn tensor for convenience with same shape the of the input BxCxHxW.

Example

>>> img = torch.rand(2, 3, 10, 12)
>>> rect = torch.tensor([[[0, 0, 4, 4]], [[4, 4, 10, 10]]])
>>> out = draw_rectangle(img, rect)
kornia.image.draw_convex_polygon(images, polygons, colors)[source]#

Draws convex polygons on a batch of image tensors.

Parameters:
  • images (Tensor) – is tensor of BxCxHxW.

  • polygons (Union[Tensor, List[Tensor]]) – represents polygons as points, either BxNx2 or List of variable length polygons. N is the number of points. 2 is (x, y).

  • colors (Tensor) – a B x 3 tensor or 3 tensor with color to fill in.

Return type:

Tensor

Returns:

This operation modifies image inplace but also returns the drawn tensor for convenience with same shape the of the input BxCxHxW.

Note

This function assumes a coordinate system (0, h - 1), (0, w - 1) in the image, with (0, 0) being the center of the top-left pixel and (w - 1, h - 1) being the center of the bottom-right coordinate.

Example

>>> img = torch.rand(1, 3, 12, 16)
>>> poly = torch.tensor([[[4, 4], [12, 4], [12, 8], [4, 8]]])
>>> color = torch.tensor([[0.5, 0.5, 0.5]])
>>> out = draw_convex_polygon(img, poly, color)
kornia.image.draw_point2d(image, points, color)[source]#

Set one or more coordinates in a Tensor to a color.

Parameters:
  • image (Tensor) – the input image on which to draw the points with shape :math`(C,H,W)` or :math`(H,W)`.

  • points (Tensor) – the [x, y] points to be drawn on the image.

  • color (Tensor) – the color of the pixel with :math`(C)` where :math`C` is the number of channels of the image.

Return type:

Tensor

Returns:

The image with points set to the color.