不少圖像處理軟件都提供邊緣褪化效果濾鏡,其實原理很是的簡單,網上搜索了一把,java
實現了基於Java的圖像邊緣褪化效果。邊緣褪化效果取決於如下三個參數:ide
1. 設定的圖像邊緣寬度this
2. 褪化比率– 其實質是圖像融合的百分比數url
3. 選擇的邊框顏色spa
主要原理是計算圖像中的像素點到中心點的距離,對邊緣像素根據褪化比率與選擇的.net
邊框顏色融合從而產生褪化效果。程序效果以下:orm
原圖:blog
處理之後圖像:ip
濾鏡的徹底源代碼以下:ci
- package com.process.blur.study;
-
- import java.awt.Color;
- import java.awt.image.BufferedImage;
-
-
-
-
-
-
- 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;
- }
-
- }