html5門戶網(wǎng)站模板百度人工客服電話多少
OpenCV版本:4.4
IDE:VS2019
功能描述
OpenCV繪制文本的函數(shù)putText()不支持中文的顯示,網(wǎng)上很多方法推薦的都是使用FreeType來支持,FreeType是什么呢?FreeType的官網(wǎng)上有介紹
FreeType官網(wǎng)
https://www.freetype.org/index.html
網(wǎng)站上都是英文的介紹,閑著沒事,翻譯了一下其介紹,水平有限,湊活著看,如下:
FreeType是一款免費(fèi)的用來渲染字體的軟件庫。
他是用c語言編寫的,體量小,效率高,可高度定制,同時(shí)能夠產(chǎn)生大多數(shù)矢量和位圖字體格式的高質(zhì)量輸出(字形圖像)。
選擇一個(gè)版本進(jìn)行下載,其下載地址如下:
https://sourceforge.net/projects/freetype/files/freetype2/2.10.1/freetype-2.10.1.tar.xz/download
解壓后如下圖所示:
?
配置FreeType
?FreeType文件夾里自帶了vs的工程,在freetype-2.10.4\builds\windows\vc2010\目錄下,直接用vs2019打開freetype.sln,就可以編譯,選擇x64編譯,編譯成功后在freetype-2.10.4\objs\x64\Debug文件夾下面,生成freetype.dll和freetype.lib,如下圖:
新建一個(gè)工程,工程中配置相關(guān)的庫路徑和包含路徑,如下圖所示添加庫的過程。
? 點(diǎn)擊下拉列表,如下圖所示:
點(diǎn)擊編輯,打開添加庫的窗口,如下圖:
把freetype.lib添加進(jìn)去,點(diǎn)擊確定后完成。
然后添加庫的路徑,步驟如下圖:
單擊下拉列表,如下圖:
添加上庫的路徑,單擊確定,完成。
添加包含頭文件的目錄。如下圖:
單擊下拉列表,打開添加包含路徑的窗口,如下圖:
添加路徑后,單擊確定,成功。
添加FreeType的封裝類文件
CvxText.h
#ifndef OPENCV_CVX_TEXT_HPP_
#define OPENCV_CVX_TEXT_HPP_// 支持OpenCV中文漢字輸入#include <ft2build.h>
#include FT_FREETYPE_H#include <opencv2/opencv.hpp>class CvxText {
public:/*** 裝載字庫文件*/CvxText(const char* freeType);virtual ~CvxText();/*** 獲取字體.目前有些參數(shù)尚不支持.** \param font 字體類型, 目前不支持* \param size 字體大小/空白比例/間隔比例/旋轉(zhuǎn)角度* \param underline 下畫線* \param diaphaneity 透明度** \sa setFont, restoreFont*/void getFont(int* type, cv::Scalar* size = nullptr, bool* underline = nullptr, float* diaphaneity = nullptr);/*** 設(shè)置字體.目前有些參數(shù)尚不支持.** \param font 字體類型, 目前不支持* \param size 字體大小/空白比例/間隔比例/旋轉(zhuǎn)角度* \param underline 下畫線* \param diaphaneity 透明度** \sa getFont, restoreFont*/void setFont(int* type, cv::Scalar* size = nullptr, bool* underline = nullptr, float* diaphaneity = nullptr);/*** 恢復(fù)原始的字體設(shè)置.** \sa getFont, setFont*/void restoreFont();/*** 輸出漢字(顏色默認(rèn)為黑色).遇到不能輸出的字符將停止.** \param img 輸出的影象* \param text 文本內(nèi)容* \param pos 文本位置** \return 返回成功輸出的字符長(zhǎng)度,失敗返回-1.*/int putText(cv::Mat& img, const char* text, cv::Point pos);/*** 輸出漢字(顏色默認(rèn)為黑色).遇到不能輸出的字符將停止.** \param img 輸出的影象* \param text 文本內(nèi)容* \param pos 文本位置** \return 返回成功輸出的字符長(zhǎng)度,失敗返回-1.*/int putText(cv::Mat& img, const wchar_t* text, cv::Point pos);/*** 輸出漢字.遇到不能輸出的字符將停止.** \param img 輸出的影象* \param text 文本內(nèi)容* \param pos 文本位置* \param color 文本顏色** \return 返回成功輸出的字符長(zhǎng)度,失敗返回-1.*/int putText(cv::Mat& img, const char* text, cv::Point pos, cv::Scalar color);/*** 輸出漢字.遇到不能輸出的字符將停止.** \param img 輸出的影象* \param text 文本內(nèi)容* \param pos 文本位置* \param color 文本顏色** \return 返回成功輸出的字符長(zhǎng)度,失敗返回-1.*/int putText(cv::Mat& img, const wchar_t* text, cv::Point pos, cv::Scalar color);private:// 禁止copyCvxText& operator=(const CvxText&);// 輸出當(dāng)前字符, 更新m_pos位置void putWChar(cv::Mat& img, wchar_t wc, cv::Point& pos, cv::Scalar color);FT_Library m_library; // 字庫FT_Face m_face; // 字體// 默認(rèn)的字體輸出參數(shù)int m_fontType;cv::Scalar m_fontSize;bool m_fontUnderline;float m_fontDiaphaneity;
};#endif // OPENCV_CVX_TEXT_HPP_
CvxText.cpp
#include <wchar.h>
#include <assert.h>
#include <locale.h>
#include <ctype.h>
#include <cmath>
#include "CvxText.h"// 打開字庫
CvxText::CvxText(const char* freeType)
{//INFO("font lib path:%s", freeType);assert(freeType != NULL);// 打開字庫文件, 創(chuàng)建一個(gè)字體FT_Error error;error = FT_Init_FreeType(&m_library);if (error){//INFOE("an error occurred during library initialization");//fprintf(stderr, "an error occurred during library initialization\n");}//if(FT_Init_FreeType(&m_library)) throw;if (FT_New_Face(m_library, freeType, 0, &m_face)) throw;// 設(shè)置字體輸出參數(shù)restoreFont();// 設(shè)置C語言的字符集環(huán)境setlocale(LC_ALL, "");
}// 釋放FreeType資源
CvxText::~CvxText()
{FT_Done_Face(m_face);FT_Done_FreeType(m_library);
}// 設(shè)置字體參數(shù):
//
// font - 字體類型, 目前不支持
// size - 字體大小/空白比例/間隔比例/旋轉(zhuǎn)角度
// underline - 下畫線
// diaphaneity - 透明度
void CvxText::getFont(int* type, cv::Scalar* size, bool* underline, float* diaphaneity)
{if (type) *type = m_fontType;if (size) *size = m_fontSize;if (underline) *underline = m_fontUnderline;if (diaphaneity) *diaphaneity = m_fontDiaphaneity;
}void CvxText::setFont(int* type, cv::Scalar* size, bool* underline, float* diaphaneity)
{// 參數(shù)合法性檢查if (type) {if (type >= 0) m_fontType = *type;}if (size) {m_fontSize.val[0] = std::fabs(size->val[0]);m_fontSize.val[1] = std::fabs(size->val[1]);m_fontSize.val[2] = std::fabs(size->val[2]);m_fontSize.val[3] = std::fabs(size->val[3]);}if (underline) {m_fontUnderline = *underline;}if (diaphaneity) {m_fontDiaphaneity = *diaphaneity;}FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}// 恢復(fù)原始的字體設(shè)置
void CvxText::restoreFont()
{m_fontType = 0; // 字體類型(不支持)m_fontSize.val[0] = 80; // 字體大小m_fontSize.val[1] = 0.5; // 空白字符大小比例m_fontSize.val[2] = 0.1; // 間隔大小比例m_fontSize.val[3] = 0; // 旋轉(zhuǎn)角度(不支持)m_fontUnderline = false; // 下畫線(不支持)m_fontDiaphaneity = 1.0; // 色彩比例(可產(chǎn)生透明效果)// 設(shè)置字符大小FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}// 輸出函數(shù)(顏色默認(rèn)為白色)
int CvxText::putText(cv::Mat& img, const char* text, cv::Point pos)
{return putText(img, text, pos, CV_RGB(255, 255, 255));
}int CvxText::putText(cv::Mat& img, const wchar_t* text, cv::Point pos)
{return putText(img, text, pos, CV_RGB(255, 255, 255));
}int CvxText::putText(cv::Mat& img, const char* text, cv::Point pos, cv::Scalar color)
{if (img.data == nullptr) return -1;if (text == nullptr) return -1;int i;for (i = 0; text[i] != '\0'; ++i) {wchar_t wc = text[i];// 解析雙字節(jié)符號(hào)if (!isascii(wc)) mbtowc(&wc, &text[i++], 2);// 輸出當(dāng)前的字符putWChar(img, wc, pos, color);}return i;
}int CvxText::putText(cv::Mat& img, const wchar_t* text, cv::Point pos, cv::Scalar color)
{if (img.data == nullptr) return -1;if (text == nullptr) return -1;int i;for (i = 0; text[i] != '\0'; ++i) {// 輸出當(dāng)前的字符putWChar(img, text[i], pos, color);}return i;
}// 輸出當(dāng)前字符, 更新m_pos位置
void CvxText::putWChar(cv::Mat& img, wchar_t wc, cv::Point& pos, cv::Scalar color)
{// 根據(jù)unicode生成字體的二值位圖FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO);FT_GlyphSlot slot = m_face->glyph;// 行列數(shù)int rows = slot->bitmap.rows;int cols = slot->bitmap.width;for (int i = 0; i < rows; ++i) {for (int j = 0; j < cols; ++j) {int off = i * slot->bitmap.pitch + j / 8;if (slot->bitmap.buffer[off] & (0xC0 >> (j % 8))) {int r = pos.y - (rows - 1 - i);int c = pos.x + j;if (r >= 0 && r < img.rows && c >= 0 && c < img.cols) {cv::Vec3b pixel = img.at<cv::Vec3b>(cv::Point(c, r));cv::Scalar scalar = cv::Scalar(pixel.val[0], pixel.val[1], pixel.val[2]);// 進(jìn)行色彩融合float p = m_fontDiaphaneity;for (int k = 0; k < 4; ++k) {scalar.val[k] = scalar.val[k] * (1 - p) + color.val[k] * p;}img.at<cv::Vec3b>(cv::Point(c, r))[0] = (unsigned char)(scalar.val[0]);img.at<cv::Vec3b>(cv::Point(c, r))[1] = (unsigned char)(scalar.val[1]);img.at<cv::Vec3b>(cv::Point(c, r))[2] = (unsigned char)(scalar.val[2]);}}}}// 修改下一個(gè)字的輸出位置double space = m_fontSize.val[0] * m_fontSize.val[1];double sep = m_fontSize.val[0] * m_fontSize.val[2];pos.x += (int)((cols ? cols : space) + sep);
}
源碼
#include <iostream>
#include <opencv2/opencv.hpp>
#include "CvxText.h"int main()
{cv::Mat image = cv::imread("D:\\OpenCVtest\\images\\juice.png");std::string text = "我是果汁";int font_face = cv::FONT_HERSHEY_COMPLEX;double font_scale = 3;int thickness = 8;int baseline;//獲取文本框的長(zhǎng)寬cv::Size text_size = cv::getTextSize(text, font_face, font_scale, thickness, &baseline);//將文本框居中繪制cv::Point origin;origin.x = image.cols / 2 - text_size.width / 2;origin.y = image.rows / 2 + text_size.height / 2;CvxText chinese("D:\\OpenCVtest\\PutTextChinese\\SimHei.ttf");chinese.putText(image, text.c_str(), origin, cv::Scalar(255, 0, 0));imshow("文本繪制", image);cv::waitKey(0);return 0;
}