Opencv中定义了一些用于显示文字相关的函数,将文字放于图像中。
文字结构有:CvFont
文字输出函数有:cvInitFont、getTextSize、putText
CvFont结构定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** Font structure */ typedef struct CvFont { const char* nameFont; //Qt:nameFont CvScalar color; //Qt:ColorFont -> cvScalar(blue_component, green_component, red_component[, alpha_component]) int font_face; //Qt: bool italic /** =CV_FONT_* */ const int* ascii; //!< font data and metrics const int* greek; const int* cyrillic; float hscale, vscale; float shear; //!< slope coefficient: 0 - normal, >0 - italic int thickness; //!< Qt: weight /** letters thickness */ float dx; //!< horizontal interval between letters int line_type; //!< Qt: PointSize } CvFont; |
cvInitFont函数定义与描述:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** @brief Initializes font structure (OpenCV 1.x API). The function initializes the font structure that can be passed to text rendering functions. @param font Pointer to the font structure initialized by the function @param font_face Font name identifier. See cv::HersheyFonts and corresponding old CV_* identifiers. @param hscale Horizontal scale. If equal to 1.0f , the characters have the original width depending on the font type. If equal to 0.5f , the characters are of half the original width. @param vscale Vertical scale. If equal to 1.0f , the characters have the original height depending on the font type. If equal to 0.5f , the characters are of half the original height. @param shear Approximate tangent of the character slope relative to the vertical line. A zero value means a non-italic font, 1.0f means about a 45 degree slope, etc. @param thickness Thickness of the text strokes @param line_type Type of the strokes, see line description @sa cvPutText */ CVAPI(void) cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear CV_DEFAULT(0), int thickness CV_DEFAULT(1), int line_type CV_DEFAULT(8)); CV_INLINE CvFont cvFont( double scale, int thickness CV_DEFAULT(1) ) { CvFont font; cvInitFont( &font, CV_FONT_HERSHEY_PLAIN, scale, scale, 0, thickness, CV_AA ); return font; } |
getTextSize函数定义与描述:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/** @brief 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: : @code 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); @endcode @param text Input text string. @param fontFace Font to use, see #HersheyFonts. @param fontScale Font scale factor that is multiplied by the font-specific base size. @param thickness Thickness of lines used to render the text. See #putText for details. @param[out] baseLine y-coordinate of the baseline relative to the bottom-most text point. @return The size of a box that contains the specified text. @see putText */ CV_EXPORTS_W Size getTextSize(const String& text, int fontFace, double fontScale, int thickness, CV_OUT int* baseLine); |
getTextSize函数定义与描述:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** @brief Draws a text string. The function cv::putText renders the specified text string in the image. Symbols that cannot be rendered using the specified font are replaced by question marks. See #getTextSize for a text rendering code example. @param img Image. @param text Text string to be drawn. @param org Bottom-left corner of the text string in the image. @param fontFace Font type, see #HersheyFonts. @param fontScale Font scale factor that is multiplied by the font-specific base size. @param color Text color. @param thickness Thickness of the lines used to draw a text. @param lineType Line type. See #LineTypes @param bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner. */ CV_EXPORTS_W void putText( InputOutputArray img, const String& text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1, int lineType = LINE_8, bool bottomLeftOrigin = false ); |