如何在OpenCV中将文本放入边界框?

3

我知道如何传递putText的位置和字体大小:

void TextBox( cv::Mat & img, const std::string & text, const cv::Rect & bbox )
{
    cv::Point position;
    double size;
    int face = CV_FONT_HERSHEY_PLAIN;
    Trick( /*in:*/ text, bbox, face /*out:*/ position, size );
    cv::putText( img, text, position, face, size, cv::Scalar( 100, 255, 100 ) );
}

如何实现这个技巧? 我想将文本缩放以适应其边界框。 (字体可能是未使用的输入。)


首先,为什么要使用OpenCV? - user2487382
我有一个视频,需要OSD。 - renonsz
一个视频及其处理基础设施已经在OpenCV中! - renonsz
1个回答

6
这有点棘手,但你可以使用cv::getTextSize()进行操作。函数说明中有一些示例代码,但它只呈现一些文本、紧密的框(围绕文本)和其基线。
如果你想在图像的任意ROI中呈现文本,则首先需要将其呈现到另一个图像中(适合于文本大小),将其调整为所需的ROI大小,然后将其放置在图像上方,如下所示:
void PutText(cv::Mat& img, const std::string& text, const cv::Rect& roi, const cv::Scalar& color, int fontFace, double fontScale, int thickness = 1, int lineType = 8)
{
    CV_Assert(!img.empty() && (img.type() == CV_8UC3 || img.type() == CV_8UC1));
    CV_Assert(roi.area() > 0);
    CV_Assert(!text.empty());

    int baseline = 0;

    // Calculates the width and height of a text string
    cv::Size textSize = cv::getTextSize(text, fontFace, fontScale, thickness, &baseline);

    // Y-coordinate of the baseline relative to the bottom-most text point
    baseline += thickness;

    // Render the text over here (fits to the text size)
    cv::Mat textImg(textSize.height + baseline, textSize.width, img.type());

    if (color == cv::Scalar::all(0)) textImg = cv::Scalar::all(255);
    else textImg = cv::Scalar::all(0);

    // Estimating the resolution of bounding image
    cv::Point textOrg((textImg.cols - textSize.width) / 2, (textImg.rows + textSize.height - baseline) / 2);

    // TR and BL points of the bounding box
    cv::Point tr(textOrg.x, textOrg.y + baseline);
    cv::Point bl(textOrg.x + textSize.width, textOrg.y - textSize.height);

    cv::putText(textImg, text, textOrg, fontFace, fontScale, color, thickness);

    // Resizing according to the ROI
    cv::resize(textImg, textImg, roi.size());

    cv::Mat textImgMask = textImg;
    if (textImgMask.type() == CV_8UC3)
        cv::cvtColor(textImgMask, textImgMask, cv::COLOR_BGR2GRAY);

    // Creating the mask
    cv::equalizeHist(textImgMask, textImgMask);

    if (color == cv::Scalar::all(0)) cv::threshold(textImgMask, textImgMask, 1, 255, cv::THRESH_BINARY_INV);
    else cv::threshold(textImgMask, textImgMask, 254, 255, cv::THRESH_BINARY);

    // Put into the original image
    cv::Mat destRoi = img(roi);
    textImg.copyTo(destRoi, textImgMask);
}

然后像这样调用:

cv::Mat image = cv::imread("C:/opencv_logo.png");
cv::Rect roi(5, 5, image.cols - 5, image.rows - 5);
cv::Scalar color(255, 0, 0);
int fontFace = cv::FONT_HERSHEY_SCRIPT_SIMPLEX;
double fontScale = 2.5;
int thickness = 2;

PutText(image, "OpenCV", roi, color, fontFace, fontScale, thickness);

由于PutText()函数的作用,您可以在图像的任意ROI上渲染任何文本,例如:

enter image description here enter image description here

希望这对您有所帮助并能够正常工作 :)


更新 #1:

请记住,在OpenCV中进行文本渲染(无论是否使用此技巧)非常昂贵,可能会影响到您的应用程序运行时间。其他库可能会优于OpenCV的渲染系统。


你能推荐一些文本渲染库吗? - Micka
我从未使用过,但我的一些团队成员赞扬Cairo库,当然还有OpenGL。 - Kornel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接