图像处理算法(模糊,锐化,水彩,素描,反 {MOD},马塞克,灰度转化等)

2019-07-13 05:37发布

SPGUI(Simple Powerfull Graphics User Interface)是使用简单、功能强大的嵌入式图形开发系统。主要为开发嵌入式LINUX图形窗口应用提供工具集。她具有美观友好的图形控件,面向对像的编程接口,多平台可移植性等特点SPGUI为设计运行于嵌入式设备,个人电脑及工作站平台的图形窗口应用程序,提供一个完整的开发平台。她的核心是为应用程序提供资源库及编译链接环境,开发者使用SPGUI提供的资源及环境,来开发自己的应用程序。SPGUI为应用程序设计者提供的资源包括:嵌入式图形引擎,图形控件,数据库引擎,底层驱动封装,常用数据结构封装及中文支持等 SPGUI是源码级高度可移植的, 目前经过本人的努力已经移植到window xp 使用vc 2005开发(可称作:模拟器),开发出来的程序只需要在嵌入式平台编译就可运行. 同时也扩展了如下功能. 图片支持 png ,gif, bmp,jpg 字库管理  freetype 矢量字库 ,fnt字库 压缩 zip 脚本 xml expat 窗口框架 : ems ui( 下个文章更新 )   提供帮助:773179801@qq.com
本章所讲述源码下载路径:http://download.csdn.net/detail/icemanyandy/3956604 了解我更多:www.softboy.uqc.cn 本文文章所讲述一些基本图像算法代码。目前在系统上做一些复杂的,比如视频监控系统(运动检测,运动跟踪等). 本演示图像是在上面的系统中截图生成,如果有希望在上面平台开发,请联系我谈合作业务.   注释 素描算法复杂未完成. 算法结果清晰,便于阅读. 
看一下水彩效果


看一下雕刻效果


看一下反 {MOD}效果   其余图片效果见上一片文章http://blog.csdn.net/icemanyandy/article/details/7084936   关于算法的原理请参考相关图像算法的书籍.
//--------------------------------------------------------------------------------- // softboy@spgui //www.softboy.uqc.cn // 20110912 // //图象的平滑(去噪声)、锐化等 // // //--------------------------------------------------------------------------------- #include #include #include #include //template array SINT32 Smooth_Box[10]={1,1,1,1,1,1,1,1,1,3}; SINT32 Smooth_Gauss[10]={1,2,1,2,4,2,1,2,1,4}; SINT32 Sharpen_Laplacian[10]={-1,-1,-1,-1,9,-1,-1,-1,-1,0}; SINT32 Sketch_Param[10]={0,-1,0,-1,4,-1,0,-1,0,0}; SINT32 Water_Param[8][2]= { {-1,-1},{0,-1},{1,-1},{0,-1},{1,0},{-1,1},{0,1},{1,1} }; SINT32 Bitmap_SmoothDeal(spBitmap_t *p,spRect_t *rect,int type) { SINT32 *tEffect = NULL; UINT8 *stpos; UINT16 *pbg; int r,g,b,Index,row,col; int x;int y; UINT8 r1,g1,b1; UINT32 colorref; UINT32 sumcol; switch(type) { case SMOOTH_BOX: tEffect = Smooth_Box; break; case SMOOTH_GAUSS: tEffect = Smooth_Gauss; break; case SHARPEN_LL: tEffect = Sharpen_Laplacian; break; default: return SP_FAIL; } stpos = (UINT8 *)( p->pData + (rect->x*p->bpl/p->width + rect->y*p->bpl) ); for(y = 1 ; yheight-1;y++) for( x = 1;x width-1;x++) { r=0,g=0,b=0; Index=0; sumcol = 0 ; for(col=-1;col<=1;col++) { pbg = (UINT16*)(stpos+(y+col)*p->bpl); for(row=-1;row<=1;row++) { colorref=pbg[x+row]; RGB_FROM_RGB565(colorref,r1,g1,b1); r+=r1*tEffect[Index]; g+=g1*tEffect[Index]; b+=b1*tEffect[Index]; Index++; } } r>>=tEffect[Index];//调节亮度. g>>=tEffect[Index]; b>>=tEffect[Index]; RGB565_FROM_RGB(colorref,r,g,b); pbg[x] = (UINT16)colorref; } return SP_OK; } SINT32 Bitmap_WaterColor(spBitmap_t *p,spRect_t *rect,int type) { UINT8 *stpos; UINT16 *pbg; SINT32 x;SINT32 y; UINT8 getrander; UINT8 bits = p->bpl/p->width; stpos = (UINT8 *)( p->pData + (rect->x*p->bpl/p->width + rect->y*p->bpl) ); for(y = 1 ; yheight-1;y++) for( x = 1;x width-1;x++) { getrander = (rand()%8); pbg = (UINT16*)(stpos+(y)*p->bpl+x*bits); *pbg = *(UINT16*)( stpos+ (y+Water_Param[getrander][0])*p->bpl + (x+Water_Param[getrander][1])*bits ); } return SP_OK; } // 雕刻 // references http://dev.yesky.com/SoftChannel/72342371928637440/20050105/1896848_1.shtml SINT32 Bitmap_Sculpture(spBitmap_t *p,spRect_t *rect,int type) { UINT8 *stpos; UINT16 *pbg; SINT32 x;SINT32 y; UINT8 getrander = 6; UINT8 bits = p->bpl/p->width; UINT16 colorref,outcolor; UINT8 r,g,b,r1,g1,b1; stpos = (UINT8 *)( p->pData + (rect->x*p->bpl/p->width + rect->y*p->bpl) ); for(y = 1 ; yheight-1;y++) for( x = 1;x width-1;x++) { pbg = (UINT16*)(stpos+(y)*p->bpl+x*bits); outcolor = *pbg; colorref = *(UINT16*)( stpos+ (y+Water_Param[getrander][0])*p->bpl + (x+Water_Param[getrander][1])*bits ); RGB_FROM_RGB565(colorref,r1,g1,b1); RGB_FROM_RGB565(outcolor,r,g,b); r = r1 - r +127; g = g1 - g +127; b = b1 - b +127; RGB565_FROM_RGB(outcolor,r,g,b); *pbg = outcolor; } return SP_OK; } SINT32 Bitmap_Sketch(spBitmap_t *p,spRect_t *rect,int type) { SINT32 *tEffect = NULL; UINT8 *stpos; UINT16 *pbg; SINT32 x;SINT32 y; UINT8 bits = p->bpl/p->width; UINT16 colorref,outcolor; int gray,r1,g1,b1; int r,g,b,Index,row,col; UINT32 sumcol; tEffect = Sketch_Param; stpos = (UINT8 *)( p->pData + (rect->x*p->bpl/p->width + rect->y*p->bpl) ); for(y = 1 ; yheight-1;y++) for( x = 1;x width-1;x++) { r=0,g=0,b=0; Index=0; sumcol = *(UINT16*)(stpos+(y)*p->bpl +x*2); for(col=-1;col<=1;col++) { pbg = (UINT16*)(stpos+(y+col)*p->bpl); for(row=-1;row<=1;row++) { colorref=pbg[x+row]; RGB_FROM_RGB565(colorref,r1,g1,b1); r+=r1*tEffect[Index]; g+=g1*tEffect[Index]; b+=b1*tEffect[Index]; Index++; } } RGB_FROM_RGB565(sumcol,r1,g1,b1); //r/=8; g/=8; //b/=8; g = g+g1; //r = r1 + (r1 - r); //g = g1 + (g1 - g); //b = b1 + (b1 - b); /*r>255?r=255:(r); g>255?g=255:(g); b>255?b=255:(b); r<0?r=-r:(r); g<0?g=-g:(g); b<0?b=-b:(b);*/ RGB565_FROM_RGB(colorref,g,g,g); pbg[x] = (UINT16)colorref; } return SP_OK; } //反 {MOD} 底片效果 SINT32 Bitmap_Invert(spBitmap_t *p,spRect_t *rect,int type) { UINT8 *stpos; UINT16 *pbg; SINT32 x;SINT32 y; UINT8 bits = p->bpl/p->width; UINT16 colorref,outcolor; UINT8 r1,g1,b1; stpos = (UINT8 *)( p->pData + (rect->x*p->bpl/p->width + rect->y*p->bpl) ); for(y = 1 ; yheight-1;y++) for( x = 1;x width-1;x++) { pbg = (UINT16*)(stpos+(y)*p->bpl+x*bits); colorref = *pbg; RGB_FROM_RGB565(colorref,r1,g1,b1); r1 = 255 - r1; g1 = 255 - g1; b1 = 255 - b1; RGB565_FROM_RGB(outcolor,r1,g1,b1); *pbg = outcolor; } return SP_OK; } //灰度图像算法 type灰度等级 SINT32 Bitmap_Gray(spBitmap_t *p,spRect_t *rect,int type) { UINT8 *stpos; UINT16 *pbg; SINT32 x;SINT32 y; UINT8 bits = p->bpl/p->width; UINT16 colorref,outcolor; UINT8 r1,g1,b1; UINT16 totalcol; UINT8 offsize = 16; if(type == 1) offsize = 128; else if( type ==2 ) offsize = 64; else if( type == 3) offsize = 32; else if( type == 4) offsize = 16; stpos = (UINT8 *)( p->pData + (rect->x*p->bpl/p->width + rect->y*p->bpl) ); for(y = 1 ; yheight-1;y++) for( x = 1;x width-1;x++) { pbg = (UINT16*)(stpos+(y)*p->bpl+x*bits); colorref = *pbg; RGB_FROM_RGB565(colorref,r1,g1,b1); //r1 = 255 - r1; //g1 = 255 - g1; //b1 = 255 - b1; totalcol = (g1*9+ r1*18+b1*5)>>5; r1 = g1 = b1 = ( totalcol/offsize)*offsize; RGB565_FROM_RGB(outcolor,r1,g1,b1); *pbg = outcolor; } return SP_OK; } //马塞克算法 type:模糊块大小 默认4 SINT32 Bitmap_Mosaic(spBitmap_t *p,spRect_t *rect,int type) { UINT8 *stpos; UINT16 *pbg; SINT32 x;SINT32 y; UINT8 bits = p->bpl/p->width; UINT16 colorref,outcolor; UINT8 r1,g1,b1; int i = 0,j = 0; stpos = (UINT8 *)( p->pData + (rect->x*p->bpl/p->width + rect->y*p->bpl) ); for(y = 0 ; yheight-1;y+=type) for( x = 0;x width-1;x+=type) { pbg = (UINT16*)(stpos+(y)*p->bpl+x*bits); colorref = *pbg; for(i = 0;ibpl+(x+i)*bits); *pbg = colorref; } } return SP_OK; } SINT32 effect_do(spBitmap_t *image,spRect_t *rect,SINT32 type) { spRect_t tRect; if(image == NULL) return SP_FAIL; if(rect==NULL){ tRect.x = tRect.y = 0 ; tRect.width = image->width; tRect.height = image->height; } else tRect = *rect; //check rect tRect.x = tRect.x <0 ?0:tRect.x; tRect.y = tRect.y <0 ?0:tRect.y; tRect.width = tRect.width >image->width ?image->width:tRect.width; tRect.height = tRect.height >image->height?image->height:tRect.height; //do effect switch(type) { case SMOOTH_BOX: case SMOOTH_GAUSS: case SHARPEN_LL: Bitmap_SmoothDeal(image,&tRect,type); break; case WATER_COLOUR: Bitmap_WaterColor(image,&tRect,type); break; case SCULPTURE: Bitmap_Sculpture(image,&tRect,type); break; case PENCIL_SKETCH: Bitmap_Sketch(image,&tRect,type); break; case INVERT: Bitmap_Invert(image,&tRect,type); break; case GRAY_LVL2: case GRAY_LVL4: case GRAY_LVL8: case GRAY_LVL16: Bitmap_Gray(image,&tRect,type-GRAY_LVL2+1); break; case MOSAIC: Bitmap_Mosaic(image,&tRect,4); break; } return SP_OK; }