freetype+opencv+vs2010圖片/視頻顯示漢字

參考了很多別的大牛們寫的博客,在此整理一下:

  • opencv+vs配置
  • freeType+vs:編譯與配置
  • 工程demo中須添加的代碼和資源等
  • 測試
  • 參考網址
  • 遇到的問題

1.opencv+vs配置

熟悉opencv也有一段時間了,最重要的是版本匹配問題!!!

vs2010以前的下載opencv2.x就好了,更高版本的vs可以體驗opencv3.x,自己cmake一下也是可以跨版本使用的,然而何必那麼麻煩呢==

當你需要顯示漢字的時候,opencv一定用的很6啦,所以這個配置就不詳細說了==
(1)配置系統變量
(2)添加包含目錄、庫目錄
(3)添加附加依賴項(.lib)
幾乎所有的第三方庫都是這個流程。

另外,可以只對當前工程配置,也可以在屬性管理器配置(會被以後建立的工程繼承)或者添加屬性表(一表寫好,有必要就添加)。

2.freeType庫的編譯

下載

http://download.savannah.gnu.org/releases/freetype/

編譯

  1. 進入\freetype-2.x\builds\win32\vc2010,打開工程並編譯(編譯選項可選);
  2. 在\freetype-2.x\objs\win32\vc2010裏可看到生成的lib文件;
  3. 添加包含目錄路徑:\freetype-2.x\include
    添加庫目錄路徑:\freetype-2.x\objs\win32\vc2010
    添加附加依賴項:(eg.)freetype2410_D.lib
    (便於管理,也可以把\include和\objs\win32\vc2010放在一個單獨的文件夾中,分別名爲include和lib);

3.工程demo中須添加的代碼和資源等

添加頭文件

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<opencv2/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/
*/


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

添加源文件

#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;
    }
    //FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}

// 恢復原始的字體設置

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);
}

添加字體

從C:\Windows\Fonts複製一箇中文字體(.ttf)到工程目錄下。

4.測試

test

#include "CvxText.h"

using namespace cv;

int main(int argc, char *argv[]) 
{ 
    IplImage *img=cvLoadImage("test.jpg");
    CvxText text("msyh.ttf"); 
    const char *msg = "hi,你好";
    float p = 0.5; 
    text.setFont(NULL, NULL, NULL, &p);   
    text.putText(img, msg, cvPoint(150, 200), CV_RGB(0,0,255)); 

    cvShowImage("test",img);
    waitKey();
    return 0; 
} 

這裏寫圖片描述

5.參考網址

6.遇到的問題

在編譯test工程時,遇到了LNK2005的問題,錯誤提示:

“msvcrt.lib(MSVCRT.dll) : error LNK2005: _memmove already defined in libcmtd.lib(memmove.obj)”
“……”

我的解決方法是在“屬性->鏈接器->輸入->忽略特定默認庫”里加上了LIBCMTD.lib,如果遇到了其他的errors則需要進行對應的分析。

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