<chrono> <TCLAP>与<CImg>

来源:互联网 发布:淘宝模特神速拍照 编辑:程序博客网 时间:2024/05/22 14:47

*******************************************************************

chrono --------C++ 时间统计demo

*******************************************************************

#include <chrono>

using namespace std; // chrono is defined under std namespace

chrono::time_point<chrono::system_clock> start_t,  end_t;
chrono::duration<double> time_diff;

start_t = chrono::system_clock::now();

end_t = chrono::system_clock::now();


time_diff = end_t  - fprop_t;

printf(" Time load %f s fprop %f s write %f s",
               time_diff.count() );


*******************************************************************

TCLAP -------C++ lib 参数解析demo

*******************************************************************

#include <tclap/CmdLine.h>

try {
    TCLAP::CmdLine cmd("ConvNet CPU-based Feature Extractor", ' ', "1.0");
    TCLAP::MultiArg<string> layer_arg(
        "l", "layer", "layer name", true, "string");
    TCLAP::ValueArg<std::string> model_file_arg(
        "m", "model", "Model file", true, "", "string");
    TCLAP::ValueArg<std::string> param_arg(
        "p", "parameters", "Parameter file", true, "", "string");
    TCLAP::ValueArg<std::string> mean_file_arg(
        "s", "mean", "Pixel mean file", true, "", "string");
    TCLAP::ValueArg<std::string> output_arg(
        "o", "output", "Output directory", true, "", "string");
    

    cmd.add(layer_arg);
    cmd.add(model_file_arg);
    cmd.add(param_arg);
    cmd.add(mean_file_arg);
    cmd.add(output_arg);

    cmd.parse(argc, argv);

    const vector<string>& layer_names = layer_arg.getValue();
    const string& model = model_file_arg.getValue();
    const string& param = param_arg.getValue();
    const string& mean_file = mean_file_arg.getValue();
    const string& output_dir = output_arg.getValue();

    //TODO:


} catch (TCLAP::ArgException &e)  {
    cerr << "error: " << e.error() << " for arg " << e.argId() << endl;
}

*******************************************************************************

CImg -------C++ lib 《参见CImg文档》

*******************************************************************************

CImg是一个跨平台的C++的图像处理库,提供了加载、处理、显示、保存等一系列功能,其中的图像处理功能尤其强大。
首先,建议先到这里欣赏一下使用CImg代码做的Demo,就是它使我这个没有图像处理经验的童鞋也心动得以致于研究了一星期^_^

主页地址:http://cimg.sourceforge.net/
下载地址:http://cimg.sourceforge.net/download.shtml
下载时注意应该下载源码包,里面附带的大量的例程。而实际上CImg库只是一个头文件CImg.h,这个头文件里包含了CImg库所有的代码。
另外不要错过下载列表中的一个部分完成的《CImg中文参考手册》。

CImg的Hello World

这段代码是从《CImg参考手册》里抄的,可以大致了解一下CImg的框架。
以VC为例:新建控制台程序,输入下面的代码。项目属性的链接器附加依赖项加入kernel.lib user32.libgdi32.lib。最后,把CImg.h拷贝到项目路径下,即可成功编译运行

  1. #include "CImg.h"
  2. using namespace cimg_library;
  3. int main() 
  4. {
  5.     // 定义一个每个颜色 位(bit)的 640x400 的彩色图像
  6.     CImg<unsigned charimg(640,400,1,3);  
  7.     //将像素值设为 0(黑色)
  8.     img.fill(0); 
  9.     // 定义一个紫色
  10.     unsigned char purple[] 255,0,255 };
  11.     
  12.     // 在坐标(100, 100)处画一个紫色的“Hello world”
  13.     img.draw_text(100,100,"Hello World",purple);  
  14.     // 在一个标题为“My first CImg code”的窗口中显示这幅图像
  15.     img.display("My first CImg code");                  
  16.     
  17.     return 0;


运行这段代码,显示结果:

在CImg体系中,图像有x,y,z,v四个轴,前三个当然是3维空间的三个方向(知道了吧?CImg可以处理3维图像),第四个v一般表示色彩通道数,比如RGB三色就是3。

上面的代码每行的注释已经写得很详细,从代码里可以看出CImg处于namespacecimg_library名空间之下;模板类CImg<>是主要的图像类,提供了大量的图像处理方法。

在CImg库里,还有一个重要的类是CImgDisplay,它提供了一个显示窗口,不仅可以显示CImg的内容,还可以接收键盘鼠标事件,我们可以暂时把它看成是一个CImg专用窗体类。

CImg类介绍

CImg类提供的方法非常多,为了便于查阅,我用Doxygen重新生成了一份CImg库的说明文档,并做成chm格式,可以本文后面找到下载地址。
CImg模板类提供了图像的载入、保存、处理功能,是整个库的核心组件。它的声明如下:

template<typename T> struct cimg_library::CImg;


模板参数T指明CImg中元素的类型。在自带的参考手册中称这些元素为像素(pixel),不过因为这里的像素和我们平时的像素概念稍微有点不同。因为前面说过CImg体系中有xyzv四个轴,CImg手册称它为4维图像,把4维图像里的单个元素称为像素。而事实上第四维的v通常就是我们的色彩通道,所以要在屏幕上显示出一个真正的像素往往要取同一xyz轴上所有v轴的点(所有色彩通道合成一个真正的彩色像素)。在本文中我决定把这个组成图像的最小单位称为元素,由所有色彩通道v组成一个像素。

CImg类中的6个成员变量:

  1. // 分别对应x,y,z,v四个轴的大小,即宽度、高度、深度和通道数。
  2. unsigned int width, height, depth, dim;
  3. // 指向内存中的图像数据
  4. *data;
  5. // 指明data是否是共用的,即是否有data的拥有权。
  6. bool is_shared;

CImg类里的成员变量都是public的,我们可以直接存取它们,不过为了防止破坏完整性,建议使用成员方法如dimx(),dimy(), dimz(), dimv() 和ptr()来操作。

CImg的构造函数,大部分构造函数都很直白,就不列出了,可以查看手册,下面列出的是一些特殊的构造函数。

  1. // 以字符串指定的数据填充,其中的values字符串包含了一串十进制数字表示的数据。
  2. // 比如参数values为字符串"20,30,40,50", repeat_pattern为true时
  3. // 图像内的数据就以20,30,40,50,20,30,40,50,20...填充。
  4. CImg (
  5.     const unsigned int dx,
  6.     const unsigned int dy,
  7.     const unsigned int dz,
  8.     const unsigned int dv,
  9.     const char *const values,
  10.     const bool repeat_pattern)

 

  1. // 由shared参数决定是否直接引用img中的data数据还是自己持有一份拷贝(是否共享)
  2. CImg (const CImg< &img, const bool shared)

 

  1. // 参考img的大小构造一个新的CImg对象
  2. CImg (const CImg< &img, const char *const dimensions)

这个构造函数的dimensions参数由一串数字或转义符组成,分别对应x,y,z,v的大小。比如:
参数为"20 20 1 3"时新CImg对象的x,y,z,v的大小分别是20 20 1 3。
转义符以%开头,后缀可以是:

x, dx, dimx, width 表示img.width y, dy, dimy, height 表示img.height z, dz, dimz, depth 表示img.depth v, dv, dimv, dim 表示img.dim

比如CImg(img, "%y %x 13");可以生成一个和img的宽高正好互换的CImg对象。

  1. // 从文件里载入图像,文件类型由扩展名确定
  2. CImg (const char *const filename)

CImg库本身支持BMP,RAW,HDR,INR,PGM,PPM,PAN,DLM格式
安装了ImageMagick(Unix系)后可支持JPG,GIF,PNG,TIF等多种格式
CImg也能使用jpeg库,zlib/png库,tiff库等来支持多种图像格式,只需编译时加入这些库即可。可以到这里下载这些库文件。

  1. // 从CImgDisplay对象的内容创建图像
  2. CImg (const CImgDisplay &disp)

 

CImg部分成员方法

CImg提供了丰富的成员方法,在这里全部列出是不可能的,这里只作一些简单介绍。更多方法请参考手册。

  1. // 赋值,它有多个重载的版本,参数和构造函数一样,实际上构造函数最终都是调用它来实现的。
  2. CImg<  assign ();
  3.  
  4. // 把data数据转交给img,自己不再有data数据的拥有权
  5. CImg<  transfer_to (CImg< &img);
  6.  
  7. // 清除数据
  8. CImg<  clear ();
  9.  
  10. // 获得所有元素总数
  11. unsigned long  size () const
  12.  
  13. // 获得某个轴向的大小
  14. int  dimx () const;
  15. int  dimy () const;
  16. int  dimz () const;
  17. int  dimv () const;
  18.  
  19. // 取得遍历元素的迭代器,这里的iterator其实就是T*。
  20. iterator  begin ();
  21. iterator  end ();
  22.  
  23. // 取得首尾数据
  24.  first ();
  25.  last ();
  26.  
  27. // 取得图像内部数据(第二个版本取得数据并定位到指定位置)
  28.  ptr ();
  29.  ptr (
  30.     const unsigned int x,
  31.     const unsigned int y=0,
  32.     const unsigned int z=0,
  33.     const unsigned int v=0);
  34.  
  35. // 快速存取指定位置上的数据
  36.  operator() (
  37.     const unsigned int x,
  38.     const unsigned int y=0,
  39.     const unsigned int z=0,
  40.     const unsigned int v=0);
  41.  
  42. // 按索引直接存取data数组
  43.  operator[] (const unsigned long off)
  44.  
  45. // 得到指定位置的索引
  46. long  offset (
  47.     const int x,
  48.     const int y=0,
  49.     const int z=0,
  50.     const int v=0) const;
  51.  
  52. // 存取指定位置上的数据,当指定的轴的超出边界时返回最边上的值
  53.  at (const int off);
  54. T& atX(const int x, const int y, const int z, const int v);
  55. T& atXY(const int x, const int y, const int z, const int v);
  56. T& atXYZ(const int x, const int y, const int z, const int v);
  57. T& atXYZV(const int x, const int y, const int z, const int v);
  58.  
  59. // 存取指定位置上的数据,当指定的轴的超出边界时返回out_val
  60.  at (const int off, const out_val);
  61. T& atX(const int x, const int y, const int z, const int v, const out_val);
  62. T& atXY(const int x, const int y, const int z, const int v, const out_val);
  63. T& atXYZ(const int x, const int y, const int z, const int v, const out_val);
  64. T& atXYZV(const int x, const int y, const int z, const int v, const out_val);

值得一提的是还有两种插值版本的at方法,分别是线性插值和三次插值。其中线性插值以linear_作为前缀,三次插值以cubic_作为前缀。参数和上面的类似,只是各轴位置的类型不是int而是float,这批方法会按插值法算出小数点位置上的数据。

到这里可以发现CImg类实际上提供了类似于vector容器的编程接口,这样我们的STL算法也能用于CImg的操作了,随后就可以看到库里有时也会把CImg类直接当作数据容器来使用。比如下面CImg中就有这个方法:

  1. // 返回字符串的形式的图像中的数据,默认是逗号分隔的一长串数字。
  2. //这里返回的CImg<charT>不代表图像,而是一个一维的字符串数据,这时它只是一个容器而已,可以把它看作是vector<charT>。
  3. CImg< charT  value_string (const char separator=','const unsigned int max_size=0);

CImg类的画图方法

draw_point draw_line draw_polygon draw_spline draw_arrow draw_image draw_rectangle draw_triangle draw_ellipse draw_circle draw_text draw_quiver draw_graph draw_axis draw_grid draw_fill draw_plasma draw_mandelbrot draw_gaussian draw_object3d


CImg的画图方法真的是非常多,每种方法都有2D和3D的重载,具体可以参考手册

CImg类的图像处理方法

CImg类的图像处理方法分两种版本:一种是直接在自己的数据上计算和保存;另一种是返回一个计算后的CImg对象,自己的数据不会改变,这个版本以get_作为前缀并且总是const方法。

  1. // 填充图像,其中的valx参数用于顺序填充图像,最多可以有15个。
  2. CImg<  fill (const val0, const val1, ...);
  3. // 以values指定的字符串来填充图像,values格式参考上文的构造函数参数
  4. CImg<  fill (const char *const values, const bool repeat_pattern)
  5. // 以values图像里的值填充。
  6. CImg<  fill (const CImg< &values, const bool repeat_pattern=true)

相应的,有get_fill方法,返回填充后的新CImg对象。后面的方法同相也有get_方法,不再累述。

  1. // 只填充指定的yzv位置上的x轴的数据
  2. CImg<  fillX (
  3.     const unsigned int y,
  4.     const unsigned int z,
  5.     const unsigned int v,
  6.     const int a0,...);

当然,也有fillY,fillZ,fillV。

  1. // 线性规格化图像,即把图像中的所有元素数值线性放大或减小到a和b之间。
  2. CImg<  normalize (const a, const b) 

 

  1. // 把图像中的所有元素数值大小限制在a和b之间,若有超出,则直接限制在边界上。
  2. CImg<  cut (const a, const b);

 

  1. // 把图像像素数据数字化到n级上,即图像元素数值从小到大被分成n个级别。(类似AD转换)
  2. CImg<  quantize (const unsigned int n, const bool keep_range=true)

 

  1. // 阈值化图像,小于value的为0,大于value的为1。
  2. // 若soft=true,则小于value元素加上value,大于value的元素减去value。
  3. // strict决定比较时使用<操作还是<=操作。
  4. CImg<  threshold (const value, const bool soft=falseconst bool strict=false

 

  1. // 旋转图像,以cx,cy指定点为中心,旋转angle度,放大zoom倍。
  2. CImg<  rotate (
  3.     const float angle,
  4.     const float cx,
  5.     const float cy,
  6.     const float zoom,
  7.     const unsigned int border_conditions=3,
  8.     const unsigned int interpolation=1);

参数border_conditions指定怎样处理边界外的值,0-边界以外以0值填充,1-重复边界点的值,2-重复图像
参数interpolation指定采用何种插值方法,0-不插值,1-线性插值,2-三次插值

  1. // 改变大小
  2. CImg<  resize (
  3.     const int pdx,
  4.     const int pdy=-100,
  5.     const int pdz=-100,
  6.     const int pdv=-100,
  7.     const int interpolation_type=1,
  8.     const int border_condition=-1,
  9.     const bool center=false)

改变图像到指定大小,当pdx,pdy,pdz或pdv<0时,使用百分比。interpolation_type指定插值方法,分别是:

-1 = 不插值 : 图像直接按大小剪切 0 = 不插值 : 多余空间依据border_condition决定。 1 = 临近点插值。 2 = 移动平均数插值。 3 = 线性插值。 4 = 删格插值。 5 = 双三次插值。


另外,还有几个优化的resize版本:

  1. // 优化的缩小至原来的1/2
  2. CImg<  resize_halfXY ()
  3. // 优化的放大至原来的2倍
  4. CImg<  resize_doubleXY ()
  5. // 优化的放大至原来的3倍
  6. CImg<  resize_tripleXY ()

 

  1. // 扭曲图像,由参数warp中相同位置的数据决定该位置元素的去向。
  2. CImg<  warp (
  3.     const CImg< &warp,
  4.     const bool relative=false,
  5.     const bool interpolation=true,
  6.     const unsigned int border_conditions=0) 

参数warp在这里只是一个容器(不要把它看成图像)的作用,其x,y,z点上的数据决定了当前图像x,y,z点上元素的新位置,这个新位置由warp的x,y,z点上的v轴数据给出。如果warp的v轴长度是1时,当前图像元素只会在x轴上移动。如果warp的v轴长度是2时,当前图像元素在x,y轴上移动...relative=ture时,表示wrap中的数据是相对位置。

  1. // 按指定轴镜像,axis可以是'x','y','z','v'
  2. CImg<  mirror (const char axis);

 

  1. // 偏移图像
  2. CImg<  translate (
  3.     const int deltax,
  4.     const int deltay=0,
  5.     const int deltaz=0,
  6.     const int deltav=0,
  7.     const int border_condition=0)

 

  1. // 取原图像中的一块子图
  2. CImg<  crop (
  3.     const int x0, const int y0, const int z0, const int v0,
  4.     const int x1, const int y1, const int z1, const int v1,
  5.     const bool border_condition=false

crop方法还有其它的版本,分别是省略v轴,z轴和y轴的版本。

  1. // 把3D图像转成2D视图。
  2. CImg<  projections2d (
  3.     const unsigned int x0, const unsigned int y0, const unsigned int z0,
  4.     const int dx=-100, const int dy=-100, const int dz=-100);


最后,下面的这些方法提供类似Photoshop的滤镜功能,强啊~~:

noise 噪声 deriche Canny-Deriche算法 blur 模糊 blur_anisotropic 各向异性模糊 blur_bilateral 对称模糊 blur_patch 面片模糊 blur_median 中值模糊 sharpen 尖锐 haar 小波分析 ...还有好多

看了上面的介绍,我想大家肯定对CImg的强大能力有了初步的了解(要知道上面列出的方法只是CImg方法中的冰山一角,而且更强的是CImg库还能通过指定Plugin宏的方式插入新的方法,当CImg自带的方法不能满足我们的要求时,也许已经有人提供了对应的插件了),下次我们开始学习CImg的具体使用吧
0 0
原创粉丝点击