很多圖像處理軟體都提供邊緣褪化效果濾鏡,其實原理非常的簡單,網上搜尋了一把,
實作了基于java的圖像邊緣褪化效果。邊緣褪化效果取決于以下三個參數:
1. 設定的圖像邊緣寬度
2. 褪化比率– 其實質是圖像融合的百分比數
3. 選擇的邊框顔色
主要原理是計算圖像中的像素點到中心點的距離,對邊緣像素根據褪化比率與選擇的
邊框顔色融合進而産生褪化效果。程式效果如下:
原圖:
處理以後圖像:
濾鏡的完全源代碼如下:
package com.process.blur.study;
import java.awt.color;
import java.awt.image.bufferedimage;
/**
* @author gloomy fish
* vignette - a photograph whose edges shade off gradually
*
*/
public class vignettefilter extends abstractbufferedimageop {
private int vignettewidth;
private int fade;
private color vignettecolor;
public vignettefilter() {
vignettewidth = 50;
fade = 35;
vignettecolor = color.black;
}
@override
public bufferedimage filter(bufferedimage src, bufferedimage dest) {
int width = src.getwidth();
int height = src.getheight();
if ( dest == null )
dest = createcompatibledestimage( src, null );
int[] inpixels = new int[width*height];
int[] outpixels = new int[width*height];
getrgb( src, 0, 0, width, height, inpixels );
int index = 0;
for(int row=0; row<height; row++) {
int ta = 0, tr = 0, tg = 0, tb = 0;
for(int col=0; col<width; col++) {
int dx = math.min(col, width - col);
int dy = math.min(row, height - row);
index = row * width + col;
ta = (inpixels[index] >> 24) & 0xff;
tr = (inpixels[index] >> 16) & 0xff;
tg = (inpixels[index] >> 8) & 0xff;
tb = inpixels[index] & 0xff;
if ((dy <= vignettewidth) & (dx <= vignettewidth))
{
double k = 1 - (double)(math.min(dy, dx) - vignettewidth + fade) / (double)fade;
outpixels[index] = superpositioncolor(ta, tr, tg, tb, k);
continue;
}
if ((dx < (vignettewidth - fade)) | (dy < (vignettewidth - fade)))
outpixels[index] = (ta << 24) | (vignettecolor.getred() << 16) | (vignettecolor.getgreen() << 8) | vignettecolor.getblue();
else
if ((dx < vignettewidth)&(dy>vignettewidth))
{
double k = 1 - (double)(dx - vignettewidth + fade) / (double)fade;
outpixels[index] = superpositioncolor(ta, tr, tg, tb, k);
}
else
if ((dy < vignettewidth)&(dx > vignettewidth))
{
double k = 1 - (double)(dy - vignettewidth + fade) / (double)fade;
outpixels[index] = superpositioncolor(ta, tr, tg, tb, k);
}
else
outpixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
}
}
setrgb( dest, 0, 0, width, height, outpixels );
return dest;
public int superpositioncolor(int ta, int red, int green, int blue, double k) {
red = (int)(vignettecolor.getred() * k + red *(1.0-k));
green = (int)(vignettecolor.getgreen() * k + green *(1.0-k));
blue = (int)(vignettecolor.getblue() * k + blue *(1.0-k));
int color = (ta << 24) | (clamp(red) << 16) | (clamp(green) << 8) | clamp(blue);
return color;
public int clamp(int value) {
return value > 255 ? 255 :((value < 0) ? 0 : value);
public int getvignettewidth() {
return vignettewidth;
public void setvignettewidth(int vignettewidth) {
this.vignettewidth = vignettewidth;
public int getfade() {
return fade;
public void setfade(int fade) {
this.fade = fade;
public color getvignettecolor() {
return vignettecolor;
public void setvignettecolor(color vignettecolor) {
this.vignettecolor = vignettecolor;
}