opencv 显示中文汉字(添加中文支持)

这篇博客介绍了如何利用CvxText和FreeType库在OpenCV中实现中文汉字的显示。首先,详细讲解了编译安装FreeType库的过程,包括下载、解压以及在Visual Studio中编译生成库文件。接着,展示了CvxText的源代码,包括支持IplImage和Mat的数据结构。最后,作者重写了putText函数,并给出了具体的调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


opencv 目前仍无法实现在图片中写入汉字,所以如果有需求的话可以通过更改opencv 或者添加支持进行实现

  • 利用CvxText和FreeType库
  • 重写putText函数

利用CvxText和FreeType库

编译安装FreeType库

FreeType库DownLoad链接

  1. 下载FreeType并解压
  2. 用VS打开builds/windows/vs2010/xxx.sln项目文件并进行编译相应的debug以及release版本的库文件( 如果编译成功就可以看到相应的lib文件以及dll文件)
  3. 如果使用的话添加相应的include路径以及lib路径即可

CvxText

支持IplImage

CvxText.h 代码:

//====================================================================
//====================================================================
//
// 文件: CvxText.h
//
// 说明: OpenCV汉字输出
//
// 时间: 
//
// 作者: chaishushan#gmail.com
//
//====================================================================
//====================================================================
 
#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 <cv.h>
#include <highgui.h>
 
/**
* \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 代码:

#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]);

		FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
	}
	if(underline)
	{
   
		m_fontUnderline   = *underline;
	}
	if(diaphaneity)
	{
   
		m_fontDiaphaneity = *diaphaneity;
	}

}

// »Ö¸´Ô­Ê¼µÄ×ÖÌåÉèÖÃ

void CvxText::restoreFont()
{
   
	m_fontType = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值