图像处理---《在图片上打印文字 FreeType库》
目的:想在处理之后的图像上打印输出结果。
方法: (1)只在图像上打印 数字、字母的话:
1.Mat格式的图像,可以使用opencv自带的putText()。
2.IPLImage格式的图像,可以使用自带的cvInitFont和cvPutText函数。
(2)在图像上打印 汉字的话,可以使用FreeType库。
FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件。
第一步:下载 FreeType(),https://sourceforge.net/projects/freetype/files/freetype2/2.4.10/,下载一个想用的 FreeType版本,这里如freetype-2.4.10.tar.gz 。
第二步:解压FreeType(),点击D:\freetype-2.4.10\builds\win32\vc2010\freetype.sln,用相应的版本---VS2010打开,右键---生成,编译一下。
在D:\freetype-2.4.10\objs\win32\vc2010文件下生成一个freetype2410_D.lib文件。
说明:第一、二步,如果可以从别人那里拷贝,可以直接拷贝过来,比如我刚开始拷贝的就是一个cvtext文件夹,里面含有include和lib文件,就是上面两步生成的。
********************************************************************************************
第三步:在VS中新建一个项目,比如名字putText_FreeType,配置一下VS2010与freetype的关系,和刚开始用opencv的方法类似。
freetype路径配置好后,工程中加入中文字体,如simhei.ttf,以及CvxText.h、CvxText.cpp、text_in_image.cpp。然后可以愉快玩耍了。
CvxText.h、CvxText.cpp、text_in_image.cpp,参照网上资源。
********************************************************************************************
********************************************************************************************
********************************************************************************************
********************************************************************************************
********************************************************************************************
********************************************************************************************
********************************************************************************************
********************************************************************************************
//====================================================================
//====================================================================
//
// 文件: CvxText.h
//
// 说明: OpenCV汉字输出
//
//====================================================================
//====================================================================
#ifndef OPENCV_CVX_TEXT_2007_08_31_H
#define OPENCV_CVX_TEXT_2007_08_31_H
/**
* \file CvxText.h
* \brief OpenCV汉字输出接口
*
* 实现了汉字输出功能。
*/
#include <ft2build.h>
#include FT_FREETYPE_H
#include <opencv2\opencv.hpp>
//modified
//将CvxText.h中的#include<cv.h> #include <highgui.h>用#include<opnecv2/opencv.hpp>替代;
/**
* \class CvxText
* \brief OpenCV中输出汉字
*
* OpenCV中输出汉字。字库提取采用了开源的FreeFype库。由于FreeFype是
* GPL版权发布的库,和OpenCV版权并不一致,因此目前还没有合并到OpenCV
* 扩展库中。
*
* 显示汉字的时候需要一个汉字字库文件,字库文件系统一般都自带了。
* 这里采用的是一个开源的字库:“文泉驿正黑体”。
*
* 关于"OpenCV扩展库"的细节请访问
* http://code.google.com/p/opencv-extension-library/
*
* 关于FreeType的细节请访问
* http://www.freetype.org/
*
* 例子:
*
* \code
int main(int argc, char *argv[])
{
// 定义CvxApplication对象
CvxApplication app(argc, argv);
// 打开一个影象
IplImage *img = cvLoadImage("test.jpg", 1);
// 输出汉字
{
// "wqy-zenhei.ttf"为文泉驿正黑体
CvText text("wqy-zenhei.ttf");
const char *msg = "在OpenCV中输出汉字!";
float p = 0.5;
text.setFont(NULL, NULL, NULL, &p); // 透明处理
text.putText(img, msg, cvPoint(100, 150), CV_RGB(255,0,0));
}
// 定义窗口,并显示影象
CvxWindow myWin("myWin");
myWin.showImage(img);
// 进入消息循环
return app.exec();
}
* \endcode
*/
class CvxText
{
// 禁止copy
CvxText& operator=(const CvxText&);
//================================================================
//================================================================
public:
/**
* 装载字库文件
*/
CvxText(const char *freeType);
virtual ~CvxText();
//================================================================
//================================================================
/**
* 获取字体。目前有些参数尚不支持。
*
* \param font 字体类型, 目前不支持
* \param size 字体大小/空白比例/间隔比例/旋转角度
* \param underline 下画线
* \param diaphaneity 透明度
*
* \sa setFont, restoreFont
*/
void getFont(int *type,
CvScalar *size=NULL, bool *underline=NULL, float *diaphaneity=NULL);
/**
* 设置字体。目前有些参数尚不支持。
*
* \param font 字体类型, 目前不支持
* \param size 字体大小/空白比例/间隔比例/旋转角度
* \param underline 下画线
* \param diaphaneity 透明度
*
* \sa getFont, restoreFont
*/
void setFont(int *type,
CvScalar *size=NULL, bool *underline=NULL, float *diaphaneity=NULL);
/**
* 恢复原始的字体设置。
*
* \sa getFont, setFont
*/
void restoreFont();
//================================================================
//================================================================
/**
* 输出汉字(颜色默认为黑色)。遇到不能输出的字符将停止。
*
* \param img 输出的影象
* \param text 文本内容
* \param pos 文本位置
*
* \return 返回成功输出的字符长度,失败返回-1。
*/
int putText(IplImage *img, const char *text, CvPoint pos);
/**
* 输出汉字(颜色默认为黑色)。遇到不能输出的字符将停止。
*
* \param img 输出的影象
* \param text 文本内容
* \param pos 文本位置
*
* \return 返回成功输出的字符长度,失败返回-1。
*/
int putText(IplImage *img, const wchar_t *text, CvPoint pos);
/**
* 输出汉字。遇到不能输出的字符将停止。
*
* \param img 输出的影象
* \param text 文本内容
* \param pos 文本位置
* \param color 文本颜色
*
* \return 返回成功输出的字符长度,失败返回-1。
*/
int putText(IplImage *img, const char *text, CvPoint pos, CvScalar color);
/**
* 输出汉字。遇到不能输出的字符将停止。
*
* \param img 输出的影象
* \param text 文本内容
* \param pos 文本位置
* \param color 文本颜色
*
* \return 返回成功输出的字符长度,失败返回-1。
*/
int putText(IplImage *img, const wchar_t *text, CvPoint pos, CvScalar color);
//================================================================
//================================================================
private:
// 输出当前字符, 更新m_pos位置
void putWChar(IplImage *img, wchar_t wc, CvPoint &pos, CvScalar color);
//================================================================
//================================================================
private:
FT_Library m_library; // 字库
FT_Face m_face; // 字体
//================================================================
//================================================================
// 默认的字体输出参数
int m_fontType;
CvScalar m_fontSize;
bool m_fontUnderline;
float m_fontDiaphaneity;
//================================================================
//================================================================
};
#endif // OPENCV_CVX_TEXT_2007_08_31_H
//====================================================================
//====================================================================
//
// 文件: CvxText.cpp
//
// 说明: OpenCV汉字输出
//
//====================================================================
//====================================================================
#include <wchar.h>
#include <assert.h>
#include <locale.h>
#include <ctype.h>
#include "CvxText.h"
//====================================================================
//====================================================================
// 打开字库
CvxText::CvxText(const char *freeType)
{
assert(freeType != NULL);
// 打开字库文件, 创建一个字体
if(FT_Init_FreeType(&m_library)) throw;
if(FT_New_Face(m_library, freeType, 0, &m_face)) throw;
// 设置字体输出参数
restoreFont();
// 设置C语言的字符集环境
setlocale(LC_ALL, "");
}
// 释放FreeType资源
CvxText::~CvxText()
{
FT_Done_Face (m_face);
FT_Done_FreeType(m_library);
}
// 设置字体参数:
//
// font - 字体类型, 目前不支持
// size - 字体大小/空白比例/间隔比例/旋转角度
// underline - 下画线
// diaphaneity - 透明度
void CvxText::getFont(int *type, CvScalar *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, CvScalar *size, bool *underline, float *diaphaneity)
{
// 参数合法性检查
if(type)
{
if(type >= 0) m_fontType = *type;
}
if(size)
{
m_fontSize.val[0] = fabs(size->val[0]);
m_fontSize.val[1] = fabs(size->val[1]);
m_fontSize.val[2] = fabs(size->val[2]);
m_fontSize.val[3] = fabs(size->val[3]);
}
if(underline)
{
m_fontUnderline = *underline;
}
if(diaphaneity)
{
m_fontDiaphaneity = *diaphaneity;
}
}
// 恢复原始的字体设置
void CvxText::restoreFont()
{
m_fontType = 0; // 字体类型(不支持)
m_fontSize.val[0] = 20; // 字体大小
m_fontSize.val[1] = 0.5; // 空白字符大小比例
m_fontSize.val[2] = 0.1; // 间隔大小比例
m_fontSize.val[3] = 0; // 旋转角度(不支持)
m_fontUnderline = false; // 下画线(不支持)
m_fontDiaphaneity = 1.0; // 色彩比例(可产生透明效果)
// 设置字符大小
FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}
// 输出函数(颜色默认为黑色)
int CvxText::putText(IplImage *img, const char *text, CvPoint pos)
{
return putText(img, text, pos, CV_RGB(255,255,255));
}
int CvxText::putText(IplImage *img, const wchar_t *text, CvPoint pos)
{
return putText(img, text, pos, CV_RGB(255,255,255));
}
//
int CvxText::putText(IplImage *img, const char *text, CvPoint pos, CvScalar color)
{
if(img == NULL) return -1;
if(text == NULL) return -1;
//
int i;
for(i = 0; text[i] != '\0'; ++i)
{
wchar_t wc = text[i];
// 解析双字节符号
if(!isascii(wc)) mbtowc(&wc, &text[i++], 2);
// 输出当前的字符
putWChar(img, wc, pos, color);
}
return i;
}
int CvxText::putText(IplImage *img, const wchar_t *text, CvPoint pos, CvScalar color)
{
if(img == NULL) return -1;
if(text == NULL) return -1;
//
int i;
for(i = 0; text[i] != '\0'; ++i)
{
// 输出当前的字符
putWChar(img, text[i], pos, color);
}
return i;
}
// 输出当前字符, 更新m_pos位置
void CvxText::putWChar(IplImage *img, wchar_t wc, CvPoint &pos, CvScalar color)
{
// 根据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;
// 行列数
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 = ((img->origin==0)? i: (rows-1-i))
* slot->bitmap.pitch + j/8;
if(slot->bitmap.buffer[off] & (0xC0 >> (j%8)))
{
int r = (img->origin==0)? pos.y - (rows-1-i): pos.y + i;;
int c = pos.x + j;
if(r >= 0 && r < img->height
&& c >= 0 && c < img->width)
{
CvScalar scalar = cvGet2D(img, r, c);
// 进行色彩融合
float p = m_fontDiaphaneity;
for(int k = 0; k < 4; ++k)
{
scalar.val[k] = scalar.val[k]*(1-p) + color.val[k]*p;
}
cvSet2D(img, r, c, scalar);
}
}
} // end for
} // end for
// 修改下一个字的输出位置
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);
}
//====================================================================
//====================================================================
//
// 文件: text_in_image.cpp
//
// 说明: OpenCV汉字输出
//
//====================================================================
//====================================================================
#include <iostream>
#include "opencv2/opencv.hpp"
#include"CvxText.h"
using namespace std;
using namespace cv;
//--------------------------------IPLImage格式的图像, 使用FreeType库---------------------------------------
int main(int argc, char *argv[])
{
IplImage *img = cvLoadImage("D:\\005_test_4\\testImg\\road_6.png");
{
CvxText text("simfang.ttf");
const char *msg = "汉字!汉字!汉字!";
float p = 1.5;
text.setFont(NULL, NULL, NULL, &p);
text.putText(img, msg, cvPoint(100, 150), CV_RGB(0,255,0));
}
cvShowImage("test", img ); cvWaitKey(-1);
cvReleaseImage(&img);
return 0;
}
致谢:https://blog.youkuaiyun.com/ubunfans/article/details/45719009;https://blog.youkuaiyun.com/shakevincent/article/details/52950294
https://blog.youkuaiyun.com/fengbingchun/article/details/8029337;https://blog.youkuaiyun.com/qq_30490125/article/details/51817321