dlib库学习之一

来源:互联网 发布:淘宝秒单软件 编辑:程序博客网 时间:2024/05/19 23:57


http://www.cnblogs.com/freegodly/p/4259040.html


dlib库学习之一

1、介绍

跨平台 C++ 通用库 Dlib 发布 ,带来了一些新特性,包括概率 CKY 解析器,使用批量同步并行计算模型来创建应用的工具,新增两个聚合算法:中国低语 (Chinese Whispers) 和纽曼的模块化聚类。

Dlib是一个使用现代C++技术编写的跨平台的通用库,遵守Boost Software licence.

主要特点如下:

1.完善的文档:每个类每个函数都有详细的文档,并且提供了大量的示例代码,如果你发现文档描述不清晰或者没有文档,告诉作者,作者会立刻添加。

2.可移植代码:代码符合ISO C++标准,不需要第三方库支持,支持win32、Linux、Mac OS X、Solaris、HPUX、BSDs 和 POSIX 系统

3.线程支持:提供简单的可移植的线程API

4.网络支持:提供简单的可移植的Socket API和一个简单的Http服务器

5.图形用户界面:提供线程安全的GUI API

6.数值算法:矩阵、大整数、随机数运算等

7.机器学习算法:

8.图形模型算法:

9.图像处理:支持读写Windows BMP文件,不同类型色彩转换

10.数据压缩和完整性算法:CRC32、Md5、不同形式的PPM算法

11.测试:线程安全的日志类和模块化的单元测试框架以及各种测试assert支持

12.一般工具:XML解析、内存管理、类型安全的big/little endian转换、序列化支持和容器类

2.安装使用

  这个和boost使用方法有点像,但小得多,只要下载源码包就可以使用,不需要其他的三方库,帮助文档说了只要添加头文件引用就可以,如果报链接错误需要把all/source.cpp包含在项目中,这个cpp也只是包含一些头文件,假如不需要GUI功能就可以在这个定义宏

DLIB_NO_GUI_SUPPORT  这样可以减小执行文件大小 ,其他的一样

How to compile



To use this library all you have to do is extract it somewhere, make sure the folder containing the dlib folder is in your include path, and finally add dlib/all/source.cpp to your project. It is worth noting that most of dlib is "header-only" which means that, in many cases, you don't actually have to build dlib/all/source.cpp into your application. So if you don't get linker errors when you exclude dlib/all/source.cpp from your project then you don't need it.

 

3.小试牛刀

     这个例子介绍如何使用dlib ,定时器和client、server  pipe信息

     将dlib文件夹包含在项目的LINCLUDEPATH中

      这里用到了socket和线程所以需要包含 dlib/all/source.cpp

     我是用mingw 编译的所以需要指定要链接的系统库,这样编译就不会报错了

复制代码
SOURCES += main.cpp \    D:/Libs/dlib-18.10/dlib/all/source.cppLIBS += -lwsock32  -lws2_32 -limm32  -luser32 -lgdi32 -lcomctl32INCLUDEPATH += D:/Libs/dlib-18.10
复制代码

 

     

client代码

  

复制代码
 1 #include <iostream> 2  3 #include <dlib/bridge.h> 4 #include <dlib/type_safe_union.h> 5 #include <dlib/timer.h> 6  7 using namespace std; 8 using namespace dlib; 9 10 //管道11 dlib::pipe<string>  out(4),in(4);12 13 14 //定时器类15 class timer_task16 {17   public:18        //定时执行的函数19        void timer_send()20        {21            string msg("this client msg");22            out.enqueue(msg);23 24            std::string re;25 26            in.dequeue(re);27            cout<<"client receive:"<<re<<endl;28 29        }30 31 };32 33 34 35 36 int main()37 {38 39     //这里应该是一个链接tcp server  ,因为我开两个client只有一个能收到信息,关闭一个后另一个就能收到40     bridge b1(connect_to_ip_and_port("127.0.0.1", 12345), transmit(out),receive(in));41 42 43 44     timer_task task;45 46     //这个timer应该不和main在一个线程,应为如果不加下面的 dlib::sleep  程序会直接退出47     timer<timer_task> t(task,&timer_task::timer_send);48 49     t.set_delay_time(1000);50 51     t.start();52 53 54     dlib::sleep(10000000);55 56     return 0;57 }
复制代码

 

server

复制代码
 1 #include <iostream> 2  3 #include <dlib/bridge.h> 4 #include <dlib/type_safe_union.h> 5 #include <dlib/timer.h> 6  7 using namespace std; 8 using namespace dlib; 9 10 dlib::pipe<string>  in(4),out(4);11 12 13 14 class timer_task15 {16   public:17        void timer_send()18        {19            string msg;20            in.dequeue(msg);21            cout<<"service receive:"<<msg<<endl;22 23 24            std::string value = "this is server send";25            out.enqueue(value);26 27        }28 29 };30 31 32 33 int main()34 {35     cout << "Hello World!" << endl;36 37     bridge b1(listen_on_port(12345),transmit(out), receive(in));38 39     timer_task task;40 41     timer<timer_task> t(task,&timer_task::timer_send);42 43     t.set_delay_time(1000);44 45     t.start();46     dlib::sleep(10000000);47 48     return 0;49 }
复制代码