将一幅图像看成是由像素组成的矩形,每个像素由R(Red),G(Green), B(Blue)三个值组成,图像模糊的原理就是算出每个像素相对于它相邻的多个元素的平均R,G,B值(每个R,G,B值分别处理), 其中边界点有六个相邻元素,内部点有9个相邻元素,顶点有4个相邻元素。可以对图像进行多次模糊。下面是核心算法实现:
public PixImage boxBlur(int numIterations) {
if(numIterations <= 0){
return this;
}
pixImages = new PixImage[numIterations + 1];
pixImages[0] = this;
for(int iteration = 1; iteration <= numIterations; iteration++){
pixImages[iteration] = new PixImage(this.getWidth(), this.getHeight());
for(int hIndex = 0; hIndex < this.getHeight(); hIndex++){
for(int wIndex = 0; wIndex < this.getWidth(); wIndex++){
short red = pixImages[iteration - 1].getPixel(wIndex, hIndex).getRed();
short green = pixImages[iteration - 1].getPixel(wIndex, hIndex).getGreen();
short blue = pixImages[iteration - 1].getPixel(wIndex, hIndex).getBlue();
// deal with the 4 corners and 4 edges
if(hIndex == 0 || hIndex == this.getHeight() - 1 || wIndex == 0 || wIndex == this.getWidth() - 1){
//deal with the left-up corner
if(hIndex == 0 && wIndex == 0){
short red1 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex).getRed();
short green1 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex).getGreen();
short blue1 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex).getBlue();
short red2 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex + 1).getRed();
short green2 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex + 1).getGreen();
short blue2 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex + 1).getBlue();
short red3 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getRed();
short green3 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getGreen();
short blue3 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getBlue();
short aveRed = (short) ((red + red1 + red2 + red3) / 4);
short aveGreen = (short) ((green + green1 + green2 +green3) / 4);
short aveBlue = (short) ((blue + blue1 + blue2 + blue3) / 4);
pixImages[iteration].getPixel(wIndex, hIndex).setRed(aveRed);
pixImages[iteration].getPixel(wIndex, hIndex).setGreen(aveGreen);
pixImages[iteration].getPixel(wIndex, hIndex).setBlue(aveBlue);
}
//deal with the right-up corner
if(hIndex == 0 && wIndex == this.getWidth() - 1){
short red1 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getRed();
short green1 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getGreen();
short blue1 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getBlue();
short red2 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex + 1).getRed();
short green2 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex + 1).getGreen();
short blue2 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex + 1).getBlue();
short red3 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex).getRed();
short green3 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex).getGreen();
short blue3 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex).getBlue();
short aveRed = (short) ((red + red1 + red2 + red3) / 4);
short aveGreen = (short) ((green + green1 + green2 +green3) / 4);
short aveBlue = (short) ((blue + blue1 + blue2 + blue3) / 4);
pixImages[iteration].getPixel(wIndex, hIndex).setRed(av