图像处理之相似图片识别(直方图应用篇)


from: 图像处理之仿画笔效果一

分类: 图像处理 3889人阅读 评论(12) 收藏 举报

图像处理之仿画笔效果一

仿画笔效果最终完成自动完成从一张RGB图像到手工油画效果根据设定好的几个基本参数,

本文章解释算法的前半部分。完整的算法介绍参见这里:

http://lvelho.impa.br/ip/papers/npar2000.pdf

 StrokeAreas

本文的算法主要是通过输入像素计算Color Difference与moment值得到输出像素从而得到

图像上的画笔绘画区域(StrokeArea),需要输入的参数S决定中心像素p(x,y)的相邻区域的大

小。整个处理流程本质是对输入图像的一个非线性高通滤波,结果是图像中频率越强的区域

输出越黑,频率越低的输出越白。

颜色差值(Color Difference):

表示两个像素点RGB颜色值之间的差值,计算差值采用欧几里德距离公式。

图像力矩(Image Moments),计算公式如下:

其中I为单色图像,否则RGB图像要分别代入RGB颜色分量

整个算法流程如下:

1.      根据输入图像生成一幅白色背景单色图像

2.      根据输入参数S计算出卷积区域大小

3.      对每个输入像素点完成卷积计算(注意是计算Color Difference)

4.      对输入像素点P0完成moment00计算

5.      归一化以后将输出像素输出到1中生成的图像对应(x,y)

 程序效果:


另外一幅图效果参数s为30时候:


StrokeArea算法代码:

  1. @Override  
  2. public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  3.     int width = src.getWidth();  
  4.     int height = src.getHeight();  
  5.   
  6.     if ( dest == null ) {  
  7.         dest = createCompatibleDestImage( src, null );  
  8.     }  
  9.   
  10.     int[] inPixels = new int[width*height];  
  11.     int[] outPixels = new int[width*height];  
  12.     getRGB( src, 00, width, height, inPixels );  
  13.     int index = 0, index2 = 0;  
  14.     int semiRow = (int)(size/2);   
  15.     int semiCol = (int)(size/2);  
  16.     int newX, newY;  
  17.       
  18.     // initialize the color RGB array with zero...  
  19.     int[] rgb = new int[3];  
  20.     int[] rgb2 = new int[3];  
  21.     for(int i=0; i<rgb.length; i++) {  
  22.         rgb[i] = rgb2[i] = 0;  
  23.     }  
  24.       
  25.     // start the algorithm process here!!  
  26.     for(int row=0; row<height; row++) {  
  27.         int ta = 0;  
  28.         for(int col=0; col<width; col++) {  
  29.             index = row * width + col;  
  30.             ta = (inPixels[index] >> 24) & 0xff;  
  31.             rgb[0] = (inPixels[index] >> 16) & 0xff;  
  32.             rgb[1] = (inPixels[index] >> 8) & 0xff;  
  33.             rgb[2] = inPixels[index] & 0xff;  
  34.               
  35.             /* adjust region to fit in source image */  
  36.             // color difference and moment Image  
  37.             double moment = 0.0d;  
  38.             for(int subRow = -semiRow; subRow <= semiRow; subRow++) {  
  39.                 for(int subCol = -semiCol; subCol <= semiCol; subCol++) {  
  40.                     newY = row + subRow;  
  41.                     newX = col + subCol;  
  42.                     if(newY < 0) {  
  43.                         newY = 0;  
  44.                     }  
  45.                     if(newX < 0) {  
  46.                         newX = 0;  
  47.                     }  
  48.                     if(newY >= height) {  
  49.                         newY = height-1;  
  50.                     }  
  51.                     if(newX >= width) {  
  52.                         newX = width - 1;  
  53.                     }  
  54.                     index2 = newY * width + newX;  
  55.                     rgb2[0] = (inPixels[index2] >> 16) & 0xff// red  
  56.                     rgb2[1] = (inPixels[index2] >> 8) & 0xff// green  
  57.                     rgb2[2] = inPixels[index2] & 0xff// blue  
  58.                     moment += colorDiff(rgb, rgb2);  
  59.                 }  
  60.             }  
  61.             // calculate the output pixel value.  
  62.             int outPixelValue = clamp((int) (255.0d * moment / (size*size)));  
  63.             outPixels[index] = (ta << 24) | (outPixelValue << 16) | (outPixelValue << 8) | outPixelValue;  
  64.         }  
  65.     }  
  66.   
  67.     setRGB( dest, 00, width, height, outPixels );  
  68.     return dest;  
  69. }  
转载文章请务必注明出自本博客
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值