Learn R Programming

tidyllm (version 0.8.0)

box_to_pixels: Turn the fractions of box answers into pixel positions

Description

A box answer from field_box() gives top, bottom, left and right as fractions of the image. box_to_pixels() multiplies them by the image size, after clamping them to 0 and 1, and returns your decide() result with four more columns: x_min, x_max, y_min and y_max, in pixels counted from the top-left corner. Rows that are not boxes, or whose type is missing, get NA. The pixel values are rounded to whole pixels. If .result already has pixel columns from an earlier call, they are replaced.

Usage

box_to_pixels(.result, .width = NULL, .height = NULL, .image = NULL)

Value

.result with the columns x_min, x_max, y_min and y_max.

Arguments

.result

A decide() result that holds a field_box() question.

.width, .height

Image width and height in pixels, one number each.

.image

Instead of .width and .height, the path of the PNG or JPEG file the boxes refer to; its size is read from the file. If you give it, .width and .height are ignored.

Details

Clamping keeps each edge inside the image but does not repair a box the model returned upside down: if left is larger than right, x_min stays larger than x_max. Widths and heights need not be whole numbers.

The result must come from one image size. If your states are images of different sizes, filter the result by state_id and call the function once per size.

See Also

field_box()

Examples

Run this code
res <- tibble::tibble(question = "cat", type = "box",
                      top = 0.2, bottom = 0.7, left = 0, right = 0.8)
box_to_pixels(res, .width = 1000, .height = 600)

Run the code above in your browser using DataLab