wkentaro/imgviz

`text_in_rectangle` behavior is unclear

vtyw opened this issue · 1 comments

vtyw commented

Here's a short example that demonstrates multiple oddities with the use case of drawing text within a box.

import imgviz
import numpy as np
import matplotlib.pyplot as plt

img = np.zeros((600, 400, 3), np.uint8)

x1, y1 = (100, 100)
x2, y2 = (300, 200)

img = imgviz.draw.text_in_rectangle(img, loc="lt", text="long texttttttttttttttttttttttt", size=30, background=(0, 0, 255), aabb1=(y1, x1), aabb2=(y2, x2))
img = imgviz.draw.text_in_rectangle(img, loc="lt", text="\nshort", size=30, background=(255, 0, 0), aabb1=(y1, x1), aabb2=(y2, x2))
img = imgviz.draw.rectangle(img, (y1, x1), (y2, x2), outline=(0, 255, 0), width=1)

plt.figure()
plt.imshow(img)
plt.show()

Result:
imgviz

Observations:

  • text_in_rectangle sounds like it would draw a rectangle specified by the two provided rectangle corners, whereas what it does is draw text with a background for that text
  • It's unclear to me how to make sense of loc and keep_size even after playing around with it
  • It's unclear to me why the second rectangle corner should be provided given that no rectangle is being drawn there per se, and the size of the text background doesn't respect the specified rectangle dimensions in any way.
  • The background for the text doesn't always cover the extent of the text.

@vtyw

text_in_rectangle sounds like it would draw a rectangle specified by the two provided rectangle corners, whereas what it does is draw text with a background for that text

text_in_rectangle was originally designed to help to visualize a bounding box detection like below:

image

In this case, loc="lt+" (left-top and above of the rectangle) is used.

It's unclear to me how to make sense of loc and keep_size even after playing around with it

keep_size is very advanced and only necessary with lt+, rb- etc (so please read the code if really needed), but loc should make sense after reading the above reply.

It's unclear to me why the second rectangle corner should be provided given that no rectangle is being drawn there per se, and the size of the text background doesn't respect the specified rectangle dimensions in any way.

Whenloc="lt" is selected, actually the second corner is unnecessary as you wonder; however, second corner is needed when "r" (right) or "b" (bottom) is provided.

The background for the text doesn't always cover the extent of the text.

That sounds like a bug, so let me take a look.