由于最近的Java作業(yè)要求將圖片放大縮小,主要就是選用一種插值算法,如最鄰近插值、雙線性二次插值、雙線性三次插值,Lanczos插值算法等。本文主要講如何用Java實現(xiàn)Lanczos算法,詳細原理見Lanczos Resampling
Lanczos插值的原理其實就是在原圖像中取一個小窗口,通過計算權(quán)重映射到新的圖像中的一個像素點中。
權(quán)重計算公式如下:
weight compute1
等同于:
weight compute1
可以看出,對于一維的數(shù)組,這個窗口其實就是?a ≤ x ≤ a
然后根據(jù)如下公式取加權(quán)平均值:
weight average
擴展到二維,權(quán)重計算為:
2D weight compute
2D weight average
可以看到窗口的大小就是2a * 2a
。
a
當a = 2
時,該算法適合圖像縮小,當a = 3
時,該算法適合圖像放大。
接下來是我用Java實現(xiàn)Lanczos插值(只支持了jpg、png和bmp格式的圖片):
public BufferedImage imageScale(String pathName, float widthScale, float heightScale) throws IOException {
if (!pathName.endsWith("png") && !pathName.endsWith("jpg") && !pathName.endsWith("bmp")) {
throw new RuntimeException("Wrrong File!");
}
File file = new File(pathName);
BufferedImage bufferedImage = ImageIO.read(file);
widthScale = 1/widthScale;
heightScale = 1/heightScale;
lanczosSize = widthScale > 1 ? 3 : 2;
int srcW = bufferedImage.getWidth();
int srcH = bufferedImage.getHeight();
int destW = (int)(bufferedImage.getWidth() / widthScale);
int destH = (int)(bufferedImage.getHeight() / heightScale);
int[] inPixels = bufferedImage.getRGB(0, 0, srcW, srcH, null, 0, srcW);
int[] outPixels = new int[destW * destH];
for (int col = 0; col < destW; col++) {
double x = col * widthScale;
double fx = (double)Math.floor(col * widthScale);
for (int row = 0; row < destH; row ++) {
double y = row * heightScale;
double fy = (double)Math.floor(y);
double[] argb = {0, 0, 0, 0};
int[] pargb = {0, 0, 0, 0};
double totalWeight = 0;
// 計算窗口的權(quán)重
for (int subrow = (int)(fy - lanczosSize + 1); subrow <= fy + lanczosSize; subrow++) {
if (subrow < 0 || subrow >= srcH)
continue;
for (int subcol = (int)(fx - lanczosSize + 1); subcol <= fx + lanczosSize; subcol++) {
if (subcol < 0 || subcol >= srcW)
continue;
double weight = getLanczosFactor(x - subcol) * getLanczosFactor(y - subrow);
if (weight > 0) {
int index = (subrow * srcW + subcol);
for (int i = 0; i < 4; i++)
pargb[i] = (inPixels[index] >> 24 - 8 * i) & 0xff;
totalWeight += weight;
for (int i = 0; i < 4; i++)
argb[i] += weight * pargb[i];
}
}
}
for (int i = 0; i < 4; i++)
pargb[i] = (int)(argb[i] / totalWeight);
outPixels[row * destW + col] = (clamp(pargb[0]) << 24) |
(clamp(pargb[1]) << 16) |
(clamp(pargb[2]) << 8) |
clamp(pargb[3]);
}
}
BufferedImage bufImg = new BufferedImage(destW, destH,
pathName.endsWith("png") ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
bufImg.setRGB(0, 0, destW, destH, outPixels, 0, destW);
return bufImg;
}
private int clamp(int v)
{
return v > 255 ? 255 : (v < 0 ? 0 : v);
}
private double getLanczosFactor(double x) {
if (x >= lanczosSize)
return 0;
if (Math.abs(x) < 1e-16)
return 1;
x *= Math.PI;
return Math.sin(x) * Math.sin(x/lanczosSize) / (x*x);
}
我們來看一下處理效果:

source
<small>原圖</small>

zoom out
<small>放大</small>

zoom in
<small>縮小</small>
源碼可見我的github