转自:http://www.cppprog.com/2009/0426/108.html
上回介绍了CImg模板类的一些函数,象我这种不在图像处理行业混的人来说很多术语实在是太专业了-_-,不理不理,看不懂就直接写测试代码看它们的作用是什么不就知道啦~~嘿嘿^_^。
上测试代码先:
- #include"CImg.h"
- usingnamespacecimg_library;
- intmain()
- {
- CImg<unsignedchar>src("test.bmp");
- //设置原图大小,貌似haar计算要求图像宽高是4的倍数
- src.resize(src.width-src.width%4,src.height-src.height%4);
- CImgList<unsignedchar>visu;
- visu
- <<src.get_crop(0,0,src.width/2,src.height/2)
- <<src.get_quantize(5)
- <<src.get_rotate(45,1)
- <<src.get_permute_axes("yxzv")
- <<src.get_erode(5)
- <<src.get_haar()
- <<src.get_dilate(3)
- <<src.get_blur(3)
- <<src.get_noise(3)
- <<src.get_deriche(3)
- <<src.get_blur_anisotropic(8)
- <<src.get_blur_bilateral(1,2)
- <<src.get_blur_patch(4,3)
- <<src.get_sharpen(3)
- <<src.get_blur_median(3)
- ;//如果愿意可以测试更多CImg的图像处理方法
- //用来显示效果
- CImgDisplaydisp(src.width*2,src.height);
- inti=0;
- unsignedchartextcolor[]={255,255,255};
- while(!disp.is_closed&&!disp.is_keyQ&&!disp.is_keyESC)
- {
- i=i%visu.size;
- charbuf[20];
- ::sprintf(buf,"img:%d",i);
- //显示效果,(CImg<<CImg)会生成一个新的CImgList
- //左边是原图,右边是处理图,外加写了个序号在上面以便区别
- disp.display(src<<(+visu[i]).draw_text(0,0,buf,textcolor)).wait();
- //按方向键下则显示下一个
- if(disp.is_keyARROWDOWN)i++;
- //方向键上则显示上一个
- if(disp.is_keyARROWUP)
- {
- i--;
- if(i<0)i=visu.size-1;
- }
- }
- return0;
- }
这个例子用到了CImg、CImgList、CImgDisplay三个类。
CImg类前面已有介绍。
CImgList是CImg的容器,用来保存一组CImg,主要方法有:
- CImgList<T>&remove(constunsignedintpos)//删除指定位置
- CImgList<T>&pop_back()//删除最后一个
- CImgList<T>&pop_front()//删除前端
- CImgList<T>&push_back(constCImg<t>&img)//从后面添加
- CImgList<T>&push_front(constCImg<t>&img)//从前面添加
- CImgList<T>&insert(constCImg<t>&img,constunsignedintpos)//插入到指定位置之前
- CImgList<T>&clear()//清空
- CImg<T>&operator[](constunsignedintpos)//取指定位置的图像
上面这些是它作为容器的基本功能,同时它也重载了一些操作符以便于使用,比如本例中的"<<"操作其实就是push_back方法。另外,它还有大量的运算功能用于给容器中的图像批量运算。最后,还有一些好玩的方法不可错过:从视频中载入或把图像保存到视频中:
- CImgList<T>&load_ffmpeg(constchar*constfilename,
- constunsignedintfirst_frame=0,
- constunsignedintlast_frame=~0U,
- constunsignedintstep_frame=1,
- constboolpixel_format=true,
- constboolresume=false)
- constCImgList<T>&save_ffmpeg(
- constchar*constfilename,
- constunsignedintfirst_frame=0,
- constunsignedintlast_frame=~0U,
- constunsignedintfps=25)
这两个方法要求链接ffmpeg库,如果没有这个库文件,还可以使用load_ffmpeg_external和save_ffmpeg_external方法调用已外部安装的ffmpeg程序编解码。
CImgDisplay类是一个窗口类,它主要用来显示CImg和CImgList。一般使用它的流程是:
- 新建CImgDisplay对象
- 设置它的大小,除直接输入宽高外也能用直接用CImg、CImgList或另一个CImgDisplay对象作为调整大小的依据。这时,CImgDisplay对象内部已经建立了一个窗口了。
- 使用display方法显示图像
- 使用wait方法等待事件发生(键盘、鼠标、超时等)
- 检查is_keyXXXX、is_closed、button、wheel等成员变量确定是什么事件,再决定我们该做什么操作。
- 如果需要,循环回第三步
- 析构时窗口收回。
在本例中,如果窗体关闭或按了Q键或按了ESC键则退出循环,程序结束。或者显示由原图和处理后的图组成的CImgList图像,如果按了上下方向键,则改变当前显示的处理图。
这是本例运行时的截图:
水墨画风格的《清明上河图》
CImg的图像处理方法绝不止上面例子中写的那么一点点,如果都写上去的话光运算就得等很长的时间,内存资源就更不用说了。所以建议大家一批批地写上去试验。或者可以修改一下代码,用逐次运算来试验效果。当然,如果你是图像处理领域的大牛,看方法名应试就知道是干啥的了,上面的代码就当熟悉一下CImg库吧。
在Windows里使用CImg
现在,我们已经可以使用CImg的现成方法做一些图像处理了。不过,有一个大问题需要解决,怎样把CImg的强大功能和我们的GUI程序相结合呢?我们总不能只使用CImgDisplay作为最终产品的显示界面吧?我们急需一个把CImg显示到指定HDC上的方法!
好在CImg库就一个头文件,所有的源代码都在这个头文件里。只要看一下它是怎么把CImg对象显示到CImgDisplay上的,我们就能依样画葫芦地把它显示到指定HDC上。
经过一番摸索,终于发现可以这样把一个CImg对象显示到HDC上:
- template<classT>
- voidDrawToHDC(HDCdc,constCImg<T>&src)
- {
- CImgDisplaydisp;
- disp.assign(src,0,3,false,true);//最后一个true指明m_disp不要显示
- disp.render(m_src);//把src的内容解析到disp的data中
- SetDIBitsToDevice(dc,0,0,disp.width,disp.height,0,0,
- 0,disp.height,disp.data,&disp.bmi,DIB_RGB_COLORS);
- }
CImgDisplay在显示过程中会填充bmi和data成员变量,而这两个变量正好是DIB数据,这段代码正好利用了这点。
但是它的运行效率实在是不怎样,经调试发现disp.assign方法会生成窗体、互斥量等东东,析构时又要删除它们,我们在这里根本用不着这些东西,浪费啊~~,于是不得不写一个新的画图方法了。
在说新版本的画图方法之前,得先说一下CImg库的另一个好功能:插件支持。只要在#include "CImg.h"之前
#define cimg_plugin "插件文件" #define cimg_plugin1 "插件文件1" #define cimg_plugin2 "插件文件2" ... #define cimg_plugin8 "插件文件8"
CImg库就会把插件文件中的方法加入到CImg类中,看来起很酷,其实工作原理很简单,头文件中CImg类是这样定义的:
- template<typenameT>
- structCImg{
- ...
- #ifdefcimg_plugin
- #includecimg_plugin
- #endif
- #ifdefcimg_plugin1
- #includecimg_plugin1
- #endif
- #ifdefcimg_plugin2
- #includecimg_plugin2
- #endif
- #ifdefcimg_plugin3
- #includecimg_plugin3
- #endif
- #ifdefcimg_plugin4
- #includecimg_plugin4
- #endif
- #ifdefcimg_plugin5
- #includecimg_plugin5
- #endif
- #ifdefcimg_plugin6
- #includecimg_plugin6
- #endif
- #ifdefcimg_plugin7
- #includecimg_plugin7
- #endif
- #ifdefcimg_plugin8
- #includecimg_plugin8
- #endif
- ...
- }
当我们如下定义时
#define cimg_plugin "cimg4hdc.h"
cimg4hdc.h文件的内容就插入到了CImg类的定义中间。
我们现在就可以写一个插件文件为CImg类加入与Windows GUI交互的方法了,下面的代码是我写的插件文件,文件名为“cimg4hdc.h”,新加了五个方法:
- HBITMAPto_bmp(HDCdc=0)const//从CImg产生一个HBITMAP,使用完要记得DeleteObject
- CImg<T>&display(HDCdc,RECT&rc)//把CImg显示到HDC上,rc指定显示位置和大小
- CImg(HBITMAPbmp)//从HBITMAP直接生成一个CImg,HBITMAP必须是16位色以上
- CImg<T>&assign(HBITMAPbmp)//同上
- CImg<T>&load_bmp(HBITMAPbmp)//同上
插件文件代码(点击下载,标准做法是右击,链接另存为...):
- #ifndefcimg_plugin_cimg4hdc
- #definecimg_plugin_cimg4hdc
- HBITMAPto_bmp(HDCdc)const
- {
- CImgDisplaydisp;
- disp.width=width;
- disp.height=height;
- disp.normalization=3;
- disp.is_closed=true;
- BITMAPINFObmi;
- BITMAPINFOHEADER&bh=bmi.bmiHeader;
- bh.biSize=sizeof(BITMAPINFOHEADER);
- bh.biWidth=disp.width;
- bh.biHeight=-(int)disp.height;
- bh.biPlanes=1;
- bh.biBitCount=32;
- bh.biCompression=BI_RGB;
- bh.biSizeImage=0;
- bh.biXPelsPerMeter=1;
- bh.biYPelsPerMeter=1;
- bh.biClrUsed=0;
- bh.biClrImportant=0;
- void*pvBits;
- HBITMAPbmp=CreateDIBSection(dc,
- &bmi,
- DIB_RGB_COLORS,
- &pvBits,
- NULL,0
- );
- if(bmp==NULL)returnbmp;
- disp.bmi=bmi;
- disp.data=(unsignedint*)pvBits;
- disp.render(*this);
- disp.width=disp.height=0;
- disp.data=NULL;
- returnbmp;
- }
- CImg<T>&display(HDCdc,RECT&rc)
- {
- CImgDisplaydisp;
- disp.width=rc.right-rc.left;
- disp.height=rc.bottom-rc.top;
- disp.normalization=3;
- disp.is_closed=true;
- BITMAPINFOHEADER&bh=disp.bmi.bmiHeader;
- bh.biSize=sizeof(BITMAPINFOHEADER);
- bh.biWidth=disp.width;
- bh.biHeight=-(int)disp.height;
- bh.biPlanes=1;
- bh.biBitCount=32;
- bh.biCompression=BI_RGB;
- bh.biSizeImage=0;
- bh.biXPelsPerMeter=1;
- bh.biYPelsPerMeter=1;
- bh.biClrUsed=0;
- bh.biClrImportant=0;
- disp.data=newunsignedint[disp.width*disp.height];
- disp.render(*this);
- ::SetDIBitsToDevice(dc,rc.left,rc.top,
- disp.width,disp.height,0,0,
- 0,disp.height,disp.data,&disp.bmi,DIB_RGB_COLORS);
- disp.width=disp.height=0;
- delete[]disp.data;
- return*this;
- }
- CImg(HBITMAPbmp)
- :width(0),height(0),depth(0),dim(0),is_shared(false),data(0)
- {
- load_bmp(bmp);
- }
- CImg<T>&assign(HBITMAPbmp)
- {
- returnload_bmp(bmp);
- }
- CImg<T>&load_bmp(HBITMAPbmp)
- {
- BITMAPbmpobj;
- if(!::GetObject(bmp,sizeof(bmpobj),&bmpobj)||bmpobj.bmBitsPixel<16)return*this;
- LONGcbBuffer=bmpobj.bmWidthBytes*(bmpobj.bmHeight);
- BYTE*lvbit=newBYTE[cbBuffer];
- if(!::GetBitmapBits(bmp,cbBuffer,lvbit))
- {
- delete[]lvbit;
- return*this;
- }
- unsignedchar*ptrs=lvbit;
- intalign= (4 - bmpobj.bmWidthBytes%4)%4;
- //Readpixeldata
- assign(bmpobj.bmWidth,bmpobj.bmHeight,1,3);
- switch(bmpobj.bmBitsPixel){
- case16:{//16bitscolors
- for(inty=height-1;y>=0;--y){cimg_forX(*this,x){
- constunsignedcharc1=*(ptrs++),c2=*(ptrs++);
- constunsignedshortcol=(unsignedshort)(c1|(c2<<8));
- (*this)(x,y,2)=(T)(col&0x1F);
- (*this)(x,y,1)=(T)((col>>5)&0x1F);
- (*this)(x,y,0)=(T)((col>>10)&0x1F);
- }ptrs+=align;}
- }break;
- case24:{//24bitscolors
- for(inty=height-1;y>=0;--y){cimg_forX(*this,x){
- (*this)(x,y,2)=(T)*(ptrs++);
- (*this)(x,y,1)=(T)*(ptrs++);
- (*this)(x,y,0)=(T)*(ptrs++);
- }ptrs+=align;}
- }break;
- case32:{//32bitscolors
- for(inty=height-1;y>=0;--y){cimg_forX(*this,x){
- (*this)(x,y,2)=(T)*(ptrs++);
- (*this)(x,y,1)=(T)*(ptrs++);
- (*this)(x,y,0)=(T)*(ptrs++);
- ++ptrs;
- }ptrs+=align;}
- }break;
- }
- mirror('y');
- delete[]lvbit;
- return*this;
- }
- #endif
测试代码(在WTL里测试):
- #definecimg_plugin"cimg4hdc.h"
- #include"CImg.h"
- usingnamespacecimg_library;
- CImg<unsignedchar>m_show;
- //生成部分
- {
- //生成一个100*100的HBITMAP
- HDCdc=::CreateCompatibleDC(0);
- HBITMAPbmp=::CreateCompatibleBitmap(::GetDC(0),100,100);
- ::SelectObject(dc,bmp);
- ::TextOut(dc,10,10,_T("HelloWorld"),11);
- //从HBITMAP生成一个CImg
- CImg<unsignedchar>src(bmp);
- //模糊计算,结果与原图并排送给m_show
- m_show=(src<<src.get_blur(2)).get_append('x');
- //删除HBITMAP和内存DC
- ::DeleteObject(bmp);
- ::DeleteDC(dc);
- }
- //显示部分
- LRESULTOnPaint(UINT/*uMsg*/,WPARAM/*wParam*/,LPARAM/*lParam*/,BOOL&/*bHandled*/)
- {
- CPaintDCdc(m_hWnd);
- RECTrc={
- 10,10,10+m_show.width,10+m_show.height
- };
- //在HDC上显示m_show
- m_show.display(dc,rc);
- return0;
- }
效果如下: