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

ren*_*nsz 3 c++ opencv

我知道如何传递 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 ) );
}
Run Code Online (Sandbox Code Playgroud)

套路怎么做?我想缩放文本以适合其边界框。(字体可能是未使用的输入。)

Kor*_*nel 5

这有点棘手,但你可以玩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);
}
Run Code Online (Sandbox Code Playgroud)

并称之为:

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);
Run Code Online (Sandbox Code Playgroud)

作为该PutText()功能的结果,您可以在图像的任意 ROI 上渲染任何文本,例如:

在此处输入图片说明 在此处输入图片说明

希望它有所帮助并有效:)


更新 #1:

请记住,OpenCV 中的文本渲染(使用或不使用此技巧)非常昂贵,并且会影响应用程序的运行时。其他库可能优于 OpenCV 渲染系统。