【问题标题】:How to insert multiple lines of text into frame/image如何将多行文本插入框架/图像
【发布时间】:2015-02-08 12:38:43
【问题描述】:

我使用 C++ 和 OpenCV 创建了一个框架,并希望在其中插入几行文本。

使用如下代码:

putText(frame, "My text here", cvPoint(30,30), 
    FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(200,200,250), 1, CV_AA); 

但在这里,我想写,假设有 2 行单独的行,“你好”和“欢迎”。 这里的问题是 \n 和 endl 不起作用。 此外,如果可能的话,将文本对齐到框架的中间。

非常感谢。

【问题讨论】:

  • 您需要 2 行 putText。 (它不是文字处理器)。另外,请避免使用 cvScalar、cvPoint 等 c-api 结构,请改用 Scalar 和 Point
  • 第二行怎么写?
  • putText(frame, "第二行", Point(30,30+20), ...);

标签: c++ visual-studio-2010 opencv


【解决方案1】:

您需要为每条线路分别致电putText()。为了计算每个新行的位置,您可以使用 getTextSize() 返回文本的宽度和高度以及基线。在 Python 中,您可以执行以下操作:

    position = (30, 30)
    text = "Some text including newline \n characters."
    font_scale = 0.75
    color = (255, 0, 0)
    thickness = 3
    font = cv2.FONT_HERSHEY_SIMPLEX
    line_type = cv2.LINE_AA

    text_size, _ = cv2.getTextSize(text, font, font_scale, thickness)
    line_height = text_size[1] + 5
    x, y0 = position
    for i, line in enumerate(text.split("\n")):
        y = y0 + i * line_height
        cv2.putText(frame,
                    line,
                    (x, y),
                    font,
                    font_scale,
                    color,
                    thickness,
                    line_type)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2016-09-29
    • 2012-02-20
    相关资源
    最近更新 更多