设为首页 加入收藏

TOP

利用OpenCV给图像添加中文标注(一)
2018-10-21 18:10:18 】 浏览:181
Tags:利用 OpenCV 图像 添加 中文 标注

 利用OpenCV给图像添加中文标注 :

参考:
http://blog.sina.com.cn/s/blog_6bbd2dd101012dbh.html  和
https://blog.csdn.net/ubunfans/article/details/45719009

OpenCV不支持汉字输出,参考了网上的相关内容,将解决步骤简要记录如下:
1、从 http://download.savannah.gnu.org/releases/freetype/ 下载FreeType库,windows下,根据自己用的编译器版本,打开相应的工程文件。比如,我用的VS2008,则打开目录 \builds\win32\vc2008 下的工程文件,编译成功,关闭工程退出。
2、编译成功后,在工具->选项->C++目录中添加freetype下的include文件夹以及lib文件夹,将objs\win32\vc2008中的库文件添加到当前工程的附加依赖项中。
3、参考opencv中文论坛这篇帖子: http://www.opencv.org.cn/forum/viewtopic.php?f=1&t=2083&hilit=汉字 直接copy前两个源码文件,保存为CvxText.h和CvxText.cpp,分别添加到当前工程中。
4、接下来就可以直接调用函数了,最简单的例子:

       IplImage *img = cvLoadImage("test.jpg", 1);      
       CvxText text("simsun.ttf");//这个是系统自带的宋体字体文件,可以选别的
       const char *msg = "汉字";
       float p = 0.5;
       text.setFont(NULL, NULL, NULL, &p);   // 透明处理(第二个参数可以设置字体大小旋转等)
       text.putText(img, msg, cvPoint(100, 150), CV_RGB(255,0,0));
这样就可以往图像test.jpg中坐标为(100,150)的位置添加红色的“汉字”二字啦。

//-----------------------------------------------------------------------------
注意:需要显示的图片一定是IplImage的 下面是Opencv Mat与Iplimage的相互转换:
1、将Mat转换为IplImage
//! converts header to IplImage; no data is copied
    operator IplImage() const;
举例:Mat img;
            IplImage *src;
             src=&IplImage(img);


2、将IplImage转换为Mat
//! converts old-style IplImage to the new matrix; the data is not copied by default
    Mat(const IplImage* img, bool copyData=false);
//-----------------------------------------------------------------------------------------------
CvxText.h 代码:
// CvxText.h
#ifndef OPENCV_CVX_TEXT_2007_08_31_H
#define OPENCV_CVX_TEXT_2007_08_31_H
#include <ft2build.h>
#include FT_FREETYPE_H
#include<opencv.hpp>
 
class CvxText  
{
   CvxText& operator=(const CvxText&);
public:
   CvxText(const char *freeType);
   virtual ~CvxText();
   void getFont(int *type,
      CvScalar *size=NULL, bool *underline=NULL, float *diaphaneity=NULL);
   void setFont(int *type,
      CvScalar *size=NULL, bool *underline=NULL, float *diaphaneity=NULL);
   void restoreFont();

   int putText(IplImage *img, const char    *text, CvPoint pos);
   int putText(IplImage *img, const wchar_t *text, CvPoint pos);
   int putText(IplImage *img, const char    *text, CvPoint pos, CvScalar color);
   int putText(IplImage *img, const wchar_t *text, CvPoint pos, CvScalar color);
private:
   void putWChar(IplImage *img, wchar_t wc, CvPoint &pos, CvScalar color);
private:
   FT_Library   m_library;   // 字á?库a
   FT_Face      m_face;      // 字&a

首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【共读Primer】15.[3.2] 标准库st.. 下一篇Codevs P1474十进制转m进制

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目