[][src]Function opencv::imgproc::get_text_size

pub fn get_text_size(
    text: &str,
    font_face: i32,
    font_scale: f64,
    thickness: i32,
    base_line: &mut i32
) -> Result<Size>

Calculates the width and height of a text string.

The function cv::getTextSize calculates and returns the size of a box that contains the specified text. That is, the following code renders some text, the tight box surrounding it, and the baseline: :

This example is not tested
String text = "Funny text inside the box";
int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
double fontScale = 2;
int thickness = 3;

Mat img(600, 800, CV_8UC3, Scalar::all(0));

int baseline=0;
Size textSize = getTextSize(text, fontFace,
fontScale, thickness, &baseline);
baseline += thickness;

// center the text
Point textOrg((img.cols - textSize.width)/2,
(img.rows + textSize.height)/2);

// draw the box
rectangle(img, textOrg + Point(0, baseline),
textOrg + Point(textSize.width, -textSize.height),
Scalar(0,0,255));
// ... and the baseline first
line(img, textOrg + Point(0, thickness),
textOrg + Point(textSize.width, thickness),
Scalar(0, 0, 255));

// then put the text itself
putText(img, text, textOrg, fontFace, fontScale,
Scalar::all(255), thickness, 8);

Parameters

  • text: Input text string.
  • fontFace: Font to use, see #HersheyFonts.
  • fontScale: Font scale factor that is multiplied by the font-specific base size.
  • thickness: Thickness of lines used to render the text. See #putText for details.
  • baseLine: [out] y-coordinate of the baseline relative to the bottom-most text point.

Returns

The size of a box that contains the specified text.

@see putText