图像处理之高斯金字塔 (八)

2014-11-24 10:41:14 · 作者: · 浏览: 9
+ 2] * green;
sumBlue += keneralData[subRow + 2][subCol + 2] * blue;
}
}

oudex = row * ow + col;
outPixels[oudex] = (ta << 24) | (clamp(sumRed) << 16) | (clamp(sumGreen) << 8) | clamp(sumBlue);
}
}
setRGB( dest, 0, 0, ow, oh, outPixels );
return dest;
}

public BufferedImage createSubCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
if ( dstCM == null )
dstCM = src.getColorModel();
return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth()/2, src.getHeight()/2), dstCM.isAlphaPremultiplied(), null);
}

public BufferedImage createTwiceCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
if ( dstCM == null )
dstCM = src.getColorModel();
return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth()*2, src.getHeight()*2), dstCM.isAlphaPremultiplied(), null);
}

public BufferedImage pyramidExpand(BufferedImage src) {
int width = src.getWidth();
int height = src.getHeight();
int[] inPixels = new int[width*height];
getRGB(src, 0, 0, width, height, inPixels );
int ow = 2*width;
int oh =2*height;
int[] outPixels = new int[ow * oh];
int index = 0, outdex = 0, ta = 0;
float[][] keneralData = this.getHVGaussianKeneral();
BufferedImage dest = createTwiceCompatibleDestImage(src, null);
for(int row=0; row for(int col=0; col float sumRed = 0, sumGreen = 0, sumBlue = 0;
for(int subRow = -2; subRow <= 2; subRow++) {
double srcRow = (row + subRow)/2.0;
double j = Math.floor(srcRow);
double t = srcRow - j;
if(t > 0) {
continue;
}
if(srcRow >= height || srcRow < 0) {
srcRow = 0;
}
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;
}

}
特别注意:我没有处理像素的宽与高,如果宽与高不是偶数可能
会有问题,使用时请自己处理吧。

UI实现源代码如下:


[java] package com.gloomyfish.image.pyramid;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PyramidDemoUI extends JComponent implements ActionListener {

/**
*
*/
private static final long serialVersionUID = 1L;
private JButton upButton;
private JButton do