zoukankan      html  css  js  c++  java
  • 图像处理------边缘褪化效果 分类: 视频图像处理 2015-07-24 09:54 30人阅读 评论(0) 收藏

    很多图像处理软件都提供边缘褪化效果滤镜,其实原理非常的简单,网上搜索了一把,

    实现了基于Java的图像边缘褪化效果。边缘褪化效果取决于以下三个参数:

    1.      设定的图像边缘宽度

    2.      褪化比率– 其实质是图像融合的百分比数

    3.      选择的边框颜色

     

    主要原理是计算图像中的像素点到中心点的距离,对边缘像素根据褪化比率与选择的

    边框颜色融合从而产生褪化效果。程序效果如下:

    原图:


    处理以后图像:


    滤镜的完全源代码如下:

    1. package com.process.blur.study;  
    2.   
    3. import java.awt.Color;  
    4. import java.awt.image.BufferedImage;  
    5.   
    6. /** 
    7.  * @author gloomy fish 
    8.  * Vignette - a photograph whose edges shade off gradually 
    9.  *  
    10.  */  
    11. public class VignetteFilter extends AbstractBufferedImageOp {  
    12.           
    13.     private int vignetteWidth;  
    14.     private int fade;  
    15.     private Color vignetteColor;  
    16.       
    17.     public VignetteFilter() {  
    18.         vignetteWidth = 50;  
    19.         fade = 35;  
    20.         vignetteColor = Color.BLACK;  
    21.     }  
    22.       
    23.     @Override  
    24.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
    25.         int width = src.getWidth();  
    26.         int height = src.getHeight();  
    27.   
    28.         if ( dest == null )  
    29.             dest = createCompatibleDestImage( src, null );  
    30.   
    31.         int[] inPixels = new int[width*height];  
    32.         int[] outPixels = new int[width*height];  
    33.         getRGB( src, 00, width, height, inPixels );  
    34.         int index = 0;  
    35.         for(int row=0; row<height; row++) {  
    36.             int ta = 0, tr = 0, tg = 0, tb = 0;  
    37.             for(int col=0; col<width; col++) {  
    38.                   
    39.                 int dX = Math.min(col, width - col);  
    40.                 int dY = Math.min(row, height - row);  
    41.                 index = row * width + col;  
    42.                 ta = (inPixels[index] >> 24) & 0xff;  
    43.                 tr = (inPixels[index] >> 16) & 0xff;  
    44.                 tg = (inPixels[index] >> 8) & 0xff;  
    45.                 tb = inPixels[index] & 0xff;  
    46.                 if ((dY <= vignetteWidth) & (dX <= vignetteWidth))  
    47.                 {  
    48.                     double k = 1 - (double)(Math.min(dY, dX) - vignetteWidth + fade) / (double)fade;  
    49.                     outPixels[index] = superpositionColor(ta, tr, tg, tb, k);  
    50.                     continue;  
    51.                 }  
    52.   
    53.                 if ((dX < (vignetteWidth - fade)) | (dY < (vignetteWidth - fade)))  
    54.                 {  
    55.                     outPixels[index] = (ta << 24) | (vignetteColor.getRed() << 16) | (vignetteColor.getGreen() << 8) | vignetteColor.getBlue();  
    56.                 }  
    57.                 else  
    58.                 {  
    59.                     if ((dX < vignetteWidth)&(dY>vignetteWidth))  
    60.                     {  
    61.                         double k = 1 - (double)(dX - vignetteWidth + fade) / (double)fade;  
    62.                         outPixels[index] = superpositionColor(ta, tr, tg, tb, k);  
    63.                     }  
    64.                     else  
    65.                     {  
    66.                         if ((dY < vignetteWidth)&(dX > vignetteWidth))  
    67.                         {  
    68.                             double k = 1 - (double)(dY - vignetteWidth + fade) / (double)fade;  
    69.                             outPixels[index] = superpositionColor(ta, tr, tg, tb, k);  
    70.                         }  
    71.                         else  
    72.                         {  
    73.                             outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
    74.                         }  
    75.                     }  
    76.                 }  
    77.             }  
    78.         }  
    79.           
    80.         setRGB( dest, 00, width, height, outPixels );  
    81.         return dest;  
    82.     }  
    83.       
    84.     public int superpositionColor(int ta, int red, int green, int blue, double k) {  
    85.         red = (int)(vignetteColor.getRed() * k + red *(1.0-k));  
    86.         green = (int)(vignetteColor.getGreen() * k + green *(1.0-k));  
    87.         blue = (int)(vignetteColor.getBlue() * k + blue *(1.0-k));  
    88.         int color = (ta << 24) | (clamp(red) << 16) | (clamp(green) << 8) | clamp(blue);  
    89.         return color;  
    90.     }  
    91.       
    92.     public int clamp(int value) {  
    93.         return value > 255 ? 255 :((value < 0) ? 0 : value);  
    94.     }  
    95.       
    96.     public int getVignetteWidth() {  
    97.         return vignetteWidth;  
    98.     }  
    99.   
    100.     public void setVignetteWidth(int vignetteWidth) {  
    101.         this.vignetteWidth = vignetteWidth;  
    102.     }  
    103.   
    104.     public int getFade() {  
    105.         return fade;  
    106.     }  
    107.   
    108.     public void setFade(int fade) {  
    109.         this.fade = fade;  
    110.     }  
    111.       
    112.     public Color getVignetteColor() {  
    113.         return vignetteColor;  
    114.     }  
    115.   
    116.     public void setVignetteColor(Color vignetteColor) {  
    117.         this.vignetteColor = vignetteColor;  
    118.     }  
    119.       
    120. }  

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    struts tomcat 中文乱码解决
    struts 2 自定义模板
    xsd 生成 xsd 文件为类文件
    struts2 从 action 到 jsp 页面
    根据查询生成简单得临时表
    DBGridEh 提示 Incompatible types
    ubuntu 收集的命令
    sp_executesql 使用
    arcims [ERR0134] Requested Service is not available 错误
    mysql 备份还原数据
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706366.html
Copyright © 2011-2022 走看看