OpenCV3 字體文字(putText()和 getTextSize())

 

 

 

文字繪製函數
函數名稱 描述
cv::putText() 在圖像中繪製指定文字
cv::getTextSize() 獲取一個文字的寬度和高度

1  cv::putText()函數

/** @brief Draws a text string.

The function 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 cv::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 the line for details.
@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 
                        );

 

 這個函數是OpenCV的一個主要文字繪製方法,它可以簡單地在圖像上繪製一些文字,由text指定地文字將在以左上角爲原點地文字框中以color指定地顏色繪製出來,除非bottomLeftOrigin標誌設置爲真,這種情況以左下角爲原點,使用的字體由fontFace參數決定

可以使用的字體
標識符 描述
 FONT_HERSHEY_SIMPLEX                   = 0 !< normal size sans-serif font  普通大小無襯線字體
FONT_HERSHEY_PLAIN                         = 1 !< small size sans-serif font     小號無襯線字體
FONT_HERSHEY_DUPLEX                     = 2 !< normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)      普通大小無襯線字體
FONT_HERSHEY_COMPLEX                 = 3 !< normal size serif font   普通大小無襯線字體比 FONT_HERSHEY_DUPLEX 更復雜
FONT_HERSHEY_TRIPLEX                    = 4 

!< normal size serif font (more complex than FONT_HERSHEY_COMPLEX  

普通大小無襯字體,比 FONT_HERSHEY_COMPLEX 更復雜

  FONT_HERSHEY_COMPLEX_SMALL = 5

!< smaller version of FONT_HERSHEY_COMPLEX

小號版本的  FONT_HERSHEY_COMPLEX

FONT_HERSHEY_SCRIPT_SIMPLEX   = 6 !< hand-writing style font     手寫字體
FONT_HERSHEY_SCRIPT_COMPLEX = 7

!< more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX

比 FONT_HERSHEY_SCRIPT_SIMPLEX  更復雜的變體

FONT_ITALIC                                          = 16 !< flag for italic font  

 

表中列出來的任何一個字體都可以和CV::FONT_ITALIC 組合使用(通過或操作)來得到斜體。每種字體都有一個“自然”大小,當fontScale不是1.0時,在文字繪製之前字體大小將由這個數縮放。

#include<iostream>
#include<opencv2\core.hpp>
#include<opencv2\highgui.hpp>
#include<opencv2\imgproc.hpp>
const char *path1 = "D:/Coder/vs/1_OpenCV/3096.jpg";

int main()
{
	cv::Mat img = cv::imread(path1,cv::IMREAD_UNCHANGED);
	const std::string str1 = "Hello World!";
	cv::putText(img, str1, cv::Point2i(0, 0), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 230), 1, 8, false);
	cv::putText(img, str1, cv::Point2i(0, 30), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 230), 1, 8, false);
	cv::putText(img, str1, cv::Point2i(0, 60), cv::FONT_HERSHEY_DUPLEX, 1, cv::Scalar(0, 255, 230), 1, 8, false);
	cv::putText(img, str1, cv::Point2i(0, 90), cv::FONT_HERSHEY_COMPLEX, 1, cv::Scalar(0, 255, 230), 1, 8, false);
	cv::putText(img, str1, cv::Point2i(0, 120), cv::FONT_HERSHEY_TRIPLEX, 1, cv::Scalar(0, 255, 230), 1, 8, false);
	cv::putText(img, str1, cv::Point2i(0, 150), cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(0, 255, 230), 1, 8, false);
	cv::putText(img, str1, cv::Point2i(0, 180), cv::FONT_HERSHEY_SCRIPT_SIMPLEX, 1, cv::Scalar(0, 255, 230), 1, 8, false);
	cv::putText(img, str1, cv::Point2i(0, 210), cv::FONT_HERSHEY_SCRIPT_COMPLEX, 1, cv::Scalar(0, 255, 230), 1, 8, false);
	cv::putText(img, str1, cv::Point2i(0, 250), cv::FONT_ITALIC|cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 230), 1, 8, false);

	cv::imshow("1", img);
	cv::waitKey(0);
	return 0;
}

 

襯線指的是字母結構筆畫之外的裝飾性筆畫。有襯線的字體叫襯線體(serif);沒有襯線的字體,則叫做無襯線體(sans-serif)。

襯線字體(serif)

特徵:在字的筆畫開始、結束的地方有額外的裝飾,而且筆畫的粗細會有所不同

用途:serif字體容易識別,它強調了每個字母筆畫的開始和結束,因此易讀性比較高。在整文閱讀的情況下,適合使用serif字體進行排版,易於換行閱讀的識別性,避免發生行間的閱讀錯誤。

中文字體中的宋體就是一種最標準的serif字體,襯線的特徵非常明顯。字形結構也和手寫的楷書一致。因此宋體一直被做爲最適合的正文字體之一。

 

無襯線字體(sans-serif)

特徵:sans serif是無襯線字體,沒有這些額外的裝飾,而且筆畫的粗細差不多。該類字體通常是機械的和統一線條的,它們往往擁有相同的曲率,筆直的線條,銳利的轉角。

用途:無襯線字體醒目,適合用於標題、DM、海報等,需要醒目但不需要長時間閱讀的地方。但現在市場上很多app正文都開始採用無襯線字體,因爲無襯線字體更簡約、清新,比較有藝術感。

無襯線字體與漢字字體中的黑體相對應。爲了起到醒目的作用,筆畫比較粗,不適合長時間閱讀,不適合用作正文字體。但是現代的 Macintosh、iOS、Android、Windows Vista 以上 等系統默認使用的無襯線字體基本上都是基於細黑體演化而來,不再像傳統的無襯線字體那麼重,因此用作正文字體時易讀性也很高。

在傳統的正文印刷中,普遍認爲襯線體能帶來更佳的可讀性。尤其是在大段落的文章中,襯線增加了閱讀時對字母的視覺參照。而無襯線體往往被用在標題、較短的文字段落或者一些通俗讀物中。相比嚴肅正經的襯線體,無襯線體給人一種休閒輕鬆的感覺。隨着現代生活和流行趨勢的變化,如今的人們越來越喜歡用無襯線體,因爲他們看上去“更乾淨”。

 2  cv::getTextSize() 函數

/** @brief Calculates the width and height of a text string.

The function 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 cv::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 cv::putText
 */
CV_EXPORTS_W Size getTextSize(const String& text, int fontFace,
                            double fontScale, int thickness,
                            CV_OUT int* baseLine);

參數意義

. const string& text: 輸入的文本文字 
. int fontFace: 文字字體類型 
. double fontScale: 字體縮放係數 
. int thickness: 字體筆畫線寬 
. CV_OUT int* baseLine: 文字最底部y座標

cv::getTextSize()函數回答瞭如果把文字繪製出來將有多大的問題,而不用實際將文字繪製到圖上,cv::getTextSize()唯一的新的參數就是baseLine,這實際上是一個輸出參數,baseLine是和文字最低點相關的文字基線的y座標值。

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include<opencv2\imgproc.hpp>

using namespace std;
using namespace cv;

int main()
{
	string text = "Funny text inside the box";
	//int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;     //手寫風格字體
	int fontFace = FONT_HERSHEY_SCRIPT_COMPLEX;
	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));
	line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));
	putText(img, text, textOrg, fontFace, fontScale, Scalar::all(255), thickness, 8);
	imshow("text", img);
	waitKey(0);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章