Leptonica在VS2010中的编译及简单使用举例
在tesseract-ocr中會用到leptonica庫,這里對leptonica簡單介紹下。
???????? Leptonica是一個開源的圖像處理和圖像分析庫,它的license是BSD 2-clause。它主要包括的操作有:位圖操作、仿射變換、形態(tài)學(xué)操作、連通區(qū)域填充、圖像變換及像素掩模、融合、增強(qiáng)、算術(shù)運(yùn)算等操作。
???????? 在使用Leptonica的時候,它依賴于開源的zlib、libjpeg、libpng、libtiff、giflib。
???????? zlib是一個很好的壓縮、解壓縮庫。它的license是zlib授權(quán),類似BSD。
???????? libjpeg是一個完全用C語言編寫的庫,包含了被廣泛使用的JPEG解碼、JPEG編碼和其它的JPEG功能的實(shí)現(xiàn)。它的license類似BSD。
???????? libpng是官方的PNG參考庫,它支持幾乎所有的PNG功能,它也是可擴(kuò)展的。它使用zlib庫作為壓縮引擎。它的license是permissive free software license,類似BSD。
???????? libtiff是一個用來讀寫TIFF文件的開源庫,它依賴于libjpeg和zlib。它的license是BSD。
???????? giflib是一個讀、寫GIF圖像的開源庫,它的license類似BSD。
???????? 下面詳細(xì)介紹Leptonica的編譯步驟:
1.????????從http://www.zlib.net/ 下載最新的zlib-1.2.8源碼,新建zlib靜態(tài)庫工程,將/zlib-1.2.8目錄下的.h、.c文件加入到此工程中,編譯生成zlib靜態(tài)庫;
2.????????從http://www.libpng.org/pub/png/libpng.html下載最新的libpng-1.6.16,新建libpng靜態(tài)庫工程,將/lpng1616目錄下的.h、.c文件加入到此工程中,將/lpng1616/scripts目錄下的pnglibconf.h.prebuilt文件拷貝到/lpng1616目錄下,并修改文件名為pnglibconf.h,將此文件也加入到此工程中,將pngstruct.h文件中zlib.h改成正確的存放路徑,編譯生成libpng靜態(tài)庫;
3.????????從http://www.ijg.org/files/ 下載最新的jpeg-9a源碼,新建ligjpeg靜態(tài)庫工程,將/jpeg-9a目錄下的.h、.c文件加入到此工程中(jmemdos.c和jmemmac.c文件除外),將jconfig.vc重命名為jconfig.h,將此文件也加入到此工程中,編譯生成libjpeg靜態(tài)庫;
4.????????從ftp://ftp.remotesensing.org/pub/libtiff下載穩(wěn)定版的tiff-4.0.3源碼,新建libtiff靜態(tài)庫工程,將/tiff-4.0.3/libtiff目錄下相應(yīng)的.h、.c文件加入到此工程中,將tiff_config.vc.h、tiffconf.vc.h分別重命名為tiff_config.h、tiffconf.h,將此兩個文件也加入到此工程中,編譯生成libtiff靜態(tài)庫;
5.????????從http://sourceforge.net/projects/giflib/下載最新的gitlib-5.1.1源碼,新建giflib靜態(tài)庫工程,將/giflib-5.1.1目錄下相應(yīng)的.h、.c文件加入到此工程中,對源文件進(jìn)行調(diào)整,將源文件中用到#include <stdbool.h>的地方,用以下語句替換,
//#include <stdbool.h>
#ifndef __cplusplustypedef int bool;#define false 0#define true 1
#endif
???????? 將文件中用到的#include<unistd.h>的地方均注釋掉,編譯生成giflib靜態(tài)庫;
6.????????從http://www.leptonica.org/download.html下載最新的leptonica-1.71源碼,新建liblept靜態(tài)庫工程,將/leptonica-1.71/src目錄下相應(yīng)的.h、.c文件加入到此工程中,調(diào)整:(1)、將zlib、libtiff、libpng、gif_lib.h、jpeglib.h相關(guān)頭文件通過工程配置(C/C++-->Additional Include Directories)加入到此工程;(2)、將environ.h文件中的#defineHAVE_LIBGIF 0改為#define HAVA_LIBGIF 1 使其支持Gif圖像格式;(3)、有些文件會出現(xiàn)奇怪的C2275和C2146錯誤,解決方法,找到出錯處,將所有變量的聲明放在函數(shù)的開頭即可;(4)、有些文件會出現(xiàn)奇怪的C2057錯誤,解決方法,注釋掉類似的static const l_int32 L_BUF_SIZE = 52;新建一個redefineconstant.h文件,將此文件包含到出現(xiàn)此問題的文件中,redefineconstant.h文件的內(nèi)容為:
#ifndef _REDEFINE_CONSTANT_H
#define _REDEFINE_CONSTANT_H#include "allheaders.h"#define L_BUF_SIZE 512
#define MAX_ALLOWED_DILATION 25#endif // _REDEFINE_CONSTANT_H
7. 新建liblepttest控制臺工程,對liblept庫進(jìn)行簡單測試,相關(guān)文件代碼如下:
stdafx.h:
#pragma once#include "targetver.h"#include <stdio.h>#include "../../../ImgCore/src/zlib/zlib-1.2.8/zlib.h"
#include "../../../ImgCore/src/libpng/lpng1616/png.h"
#include "../../../ImgCore/src/libjpeg/jpeg-9a/jpeglib.h"
#include "../../../ImgCore/src/libtiff/tiff-4.0.3/libtiff/tiff.h"
#include "../../../ImgCore/src/giflib/giflib-5.1.1/lib/gif_lib.h"
#include "../../src/leptonica-1.71/src/allheaders.h"
stdafx.cpp:
#include "stdafx.h"// TODO: reference any additional headers you need in STDAFX.H
// and not in this file#ifdef _DEBUG#pragma comment(lib, "../../../../ImgCore/lib/dbg/x86_vc10/zlib[dbg_x86_vc10].lib")#pragma comment(lib, "../../../../ImgCore/lib/dbg/x86_vc10/libpng[dbg_x86_vc10].lib")#pragma comment(lib, "../../../../ImgCore/lib/dbg/x86_vc10/libjpeg[dbg_x86_vc10].lib")#pragma comment(lib, "../../../../ImgCore/lib/dbg/x86_vc10/libtiff[dbg_x86_vc10].lib")#pragma comment(lib, "../../../../ImgCore/lib/dbg/x86_vc10/giflib[dbg_x86_vc10].lib")#pragma comment(lib, "../../../lib/dbg/x86_vc10/liblept[dbg_x86_vc10].lib")
#else#pragma comment(lib, "../../../../ImgCore/lib/rel/x86_vc10/zlib[rel_x86_vc10].lib")#pragma comment(lib, "../../../../ImgCore/lib/rel/x86_vc10/libpng[rel_x86_vc10].lib")#pragma comment(lib, "../../../../ImgCore/lib/rel/x86_vc10/libjpeg[rel_x86_vc10].lib")#pragma comment(lib, "../../../../ImgCore/lib/rel/x86_vc10/libtiff[rel_x86_vc10].lib")#pragma comment(lib, "../../../../ImgCore/lib/rel/x86_vc10/giflib[rel_x86_vc10].lib")#pragma comment(lib, "../../../lib/rel/x86_vc10/liblept[rel_x86_vc10].lib")
#endif
liblepttest.cpp:
#include "stdafx.h"
#include <iostream>
#include <string>using namespace std;static const l_float32 ANGLE1 = 3.14159265 / 12.;void RotateTest()
{string strSrc = "../../../testdata/weasel.png";string strDst = "../../../testdata/dst.gif";PIX* pixs = pixRead(strSrc.c_str());if (pixs == NULL) {cout<<" read image error "<<endl;return;}l_int32 w, h, d;pixGetDimensions(pixs, &w, &h, &d);PIX* pixd = pixRotate(pixs, ANGLE1, L_ROTATE_SHEAR, L_BRING_IN_WHITE, w, h);pixWrite(strDst.c_str(), pixd, IFF_GIF);pixDestroy(&pixs);pixDestroy(&pixd);
}void EdgeTest()
{string strSrc = "../../../testdata/marge.jpg";string strDst = "../../../testdata/dst.bmp";PIX* pixs = pixRead(strSrc.c_str());if (pixs == NULL) {cout<<" read image error "<<endl;return;}l_int32 w, h, d;pixGetDimensions(pixs, &w, &h, &d);if (d != 8) {cout<<"pixs not 8 bpp"<<endl;return;}PIX* pixf = pixSobelEdgeFilter(pixs, L_HORIZONTAL_EDGES);PIX* pixd = pixThresholdToBinary(pixf, 10);pixInvert(pixd, pixd);pixWrite(strDst.c_str(), pixd, IFF_BMP);pixDestroy(&pixs);pixDestroy(&pixf);pixDestroy(&pixd);
}int main(int argc, char* argv[])
{RotateTest();EdgeTest();cout<<"ok!"<<endl;return 0;
}
GitHub:https://github.com/fengbingchun/Liblept_Test
總結(jié)
以上是生活随笔為你收集整理的Leptonica在VS2010中的编译及简单使用举例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存泄漏检测工具VLD在VS2010中的
- 下一篇: 二维码Data Matrix简介及在VS