}
for(int subCol = -2; subCol <= 2; subCol++) {
double srcColOff = (col + subCol)/2.0;
j = Math.floor(srcColOff);
t = srcColOff - j;
if(t > 0) {
continue;
}
if(srcColOff >= width || srcColOff < 0) {
srcColOff = 0;
}
index = (int)(srcRow * width + srcColOff);
ta = (inPixels[index] >> 24) & 0xff;
int red = (inPixels[index] >> 16) & 0xff;
int green = (inPixels[index] >> 8) & 0xff;
int blue = inPixels[index] & 0xff;
sumRed += keneralData[subRow + 2][subCol + 2] * red;
sumGreen += keneralData[subRow + 2][subCol + 2] * green;
sumBlue += keneralData[subRow + 2][subCol + 2] * blue;
}
}
outdex = row * ow + col;
outPixels[outdex] = (ta << 24) | (clamp(4.0f * sumRed) << 16) | (clamp(4.0f * sumGreen) << 8) | clamp(4.0f * sumBlue);
// outPixels[outdex] = (ta << 24) | (clamp(sumRed) << 16) | (clamp(sumGreen) << 8) | clamp(sumBlue);
}
}
setRGB( dest, 0, 0, ow, oh, outPixels );
return dest;
}图像金字塔的reduce与expand过程都是卷积采样实现。特别注意的是expand
操作不是reduce的可逆操作。
关于什么是卷积,高斯滤波请参见博客上的其它相关文章。
高斯金字塔全部算法源代码如下:
[java] package com.gloomyfish.image.pyramid;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
public class PyramidAlgorithm extends GaussianFilter {
private float a;
public PyramidAlgorithm() {
a = 0.4f;
}
public void setParameter(float p) {
this.a = p;
}
public BufferedImage[] pyramidDown(BufferedImage src, int level) {
BufferedImage[] imagePyramids = new BufferedImage[level + 1];
imagePyramids[0] = src;
for(int i=1; i
}
return imagePyramids;
}
public BufferedImage[] pyramidUp(BufferedImage[] srcImage) {
BufferedImage[] imagePyramids = new BufferedImage[srcImage.length];
for(int i=0; i
}
return imagePyramids;
}
/***
* l1 = g1 - expand(g2)
* l2 = g2 - expand(g3)
* l0 = g0 - expand(g1)
* @param reduceImages
* @param expandImages
* @return
*/
public BufferedImage[] getLaplacianPyramid(BufferedImage[] reduceImages) {
BufferedImage[] laplaciImages = new BufferedImage[reduceImages.length -1];
for(int i=1; i
laplaciImages[i-1] = createCompatibleDestImage(expandImage, null);
int width = reduceImages[i-1].getWidth();
int height = reduceImages[i-1].getHeight();
int ewidth = expandImage.getWidth();
width = (width > ewidth) ewidth : width;
height = (height > expandImage.getHeight()) expandImage.getHeight():height;
System.out.println(" width = " + width + " expand width = " + ewidth);
int[] reducePixels = new int[width*height];
int[] expandPixels = new int[width*height];
int[] laPixels = new int[width*height];
getRGB( reduceImages[i-1], 0, 0, width, height, reducePixels);
getRGB( expandImage, 0, 0, width, height, expandPixels );
int index = 0;
int er = 0, eg = 0, eb = 0;
for(int row=0; row
for(int col=0; col
ta = (reducePix