JAI - 載圖轉 BufferedImage
JAI 載完圖是 PlanarImage, 可是一般我們處理的時候用 BufferedImage.
如果需要 JAI 幫忙載圖, 可是又想操作 BufferedImage 可以用下面的 method
private BufferedImage createBufferedImage() {
RenderedImage input = JAI.create("fileload", "test.tif");
BufferedImage bimg = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_ARGB);
((Graphics2D) bimg.getGraphics()).drawRenderedImage(input, new AffineTransform());
bimg.getGraphics().dispose();
return bimg;
}
Posted at 02:49下午 十月 06, 2008 by shooeugenesea in Java Image | 迴響[0]
計算讓圖片能夠等比縮小
public class TestFixedSize {
public static void main(String[] args) {
System.out.println(calFixedSize(new Dimension(200,200), new Dimension(1000,30)));
}
public static Dimension calFixedSize(Dimension fixedSize, Dimension originalSize) {
int fixedWidth = fixedSize.width;
int fixedHeight = fixedSize.height;
int imgWidth = originalSize.width;
int imgHeight = originalSize.height;
int resultWidth = 0;
int resultHeight = 0;
if (fixedWidth > imgWidth && fixedHeight > imgHeight) {
resultWidth = (int) imgWidth;
resultHeight = (int) imgHeight;
} else {
if (imgWidth > imgHeight) {
resultWidth = (int) fixedWidth;
resultHeight = (int) (fixedWidth * imgHeight / imgWidth);
} else {
resultWidth = (int) (fixedHeight * imgWidth / imgHeight);
resultHeight = (int) fixedHeight;
}
}
return new Dimension(resultWidth, resultHeight);
}
}
Posted at 06:26下午 九月 27, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - 銳化效果 (ConvolveDescriptor)
public class JAITester {
private RenderedImage doHistogram(RenderedImage image) {
float[] sobelSharp = {
-1, -2, -1,
0, 0, 0,
1, 2, 1
};
float[] prewittSharp = {
-1,-1,-1,
0, 0, 0,
1, 1, 1
};
float[] laplacienSharp = {
-1,-1,-1,
-1, 9,-1,
-1,-1,-1
};
KernelJAI kernel = new KernelJAI(3,3,prewittSharp);
RenderedImage img = JAI.create("convolve", image, kernel);
return img;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doHistogram(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 01:39上午 九月 12, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - 中間值(最大值 最小值)濾波(Median Filter, MaxFilter, MinFilter)
public class JAITester {
private RenderedImage doHistogram(RenderedImage imageToMedianFilter) {
ParameterBlock mfParams = new ParameterBlock();
mfParams.addSource(imageToMedianFilter);
mfParams.add( MedianFilterDescriptor.MEDIAN_MASK_SQUARE );
mfParams.add( Integer.valueOf("5") );
imageToMedianFilter = JAI.create( "MedianFilter", mfParams, null );
return imageToMedianFilter;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doHistogram(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
MaxFilter
public class JAITester {
private RenderedImage doHistogram(RenderedImage imageToMaxFilter) {
ParameterBlock mfParams = new ParameterBlock();
mfParams.addSource(imageToMedianFilter);
mfParams.add( MaxFilterDescriptor.MAX_MASK_SQUARE );
mfParams.add( Integer.valueOf("5") );
imageToMaxFilter = JAI.create( "MaxFilter", mfParams, null );
return imageToMedianFilter;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doHistogram(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
MinFilter
public class JAITester {
private RenderedImage doHistogram(RenderedImage imageToMedianFilter) {
ParameterBlock mfParams = new ParameterBlock();
mfParams.addSource(imageToMedianFilter);
mfParams.add( MinFilterDescriptor.MIN_MASK_SQUARE );
mfParams.add( Integer.valueOf("5") );
imageToMedianFilter = JAI.create( "MinFilter", mfParams, null );
return imageToMedianFilter;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doHistogram(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 02:29上午 九月 09, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - 查表操作 (LookupDescriptor)
public class JAITester {
private RenderedImage doHistogram(RenderedImage imageToHistogram) {
imageToHistogram = Utils.toSingleBand(imageToHistogram);
int brightLevelUp = 128;
byte[] byteHBins = new byte[256];
for ( int i = 0; i < byteHBins.length; i++ ) {
if ( i + brightLevelUp > 255 ) {
byteHBins[i] = (byte) 255;
} else {
byteHBins[i] = (byte) (i + brightLevelUp);
}
}
LookupTableJAI lookupTableJAI = new LookupTableJAI( byteHBins );
ParameterBlock lookupParams = new ParameterBlock();
lookupParams.add( lookupTableJAI );
RenderedImage newImage = JAI.create("lookup", imageToHistogram, lookupTableJAI, null);
return newImage;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doHistogram(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
class Utils {
public static RenderedImage toSingleBand(RenderedImage imageToBinarize) {
ParameterBlock bandCombineParams = new ParameterBlock();
bandCombineParams.addSource(imageToBinarize);
bandCombineParams.add( new double[][]{ {0.2,0.3,0.4,0.5} } );
return JAI.create("bandcombine", bandCombineParams, null);
}
}
Posted at 01:56上午 九月 08, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - RGB 轉 IHS 處理後轉 RGB. (影像辨識的類似操作)
public class JAITester {
private RenderedImage doOrderedDither(RenderedImage srcImage) {
//prepare ihs color model can convert original image pixels' colors
ColorSpace ihsColorSpace = IHSColorSpace.getInstance();
ColorModel ihsColorModel =
new ComponentColorModel(ihsColorSpace,
new int[]{8, 8, 8},
false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
ParameterBlock ihsParams = new ParameterBlock();
ihsParams.addSource(srcImage);
ihsParams.add(ihsColorModel);
RenderedImage ihsImage = JAI.create("colorconvert", ihsParams);
// select Hue data from color converted image data
ParameterBlock bandSelectParams = new ParameterBlock();
bandSelectParams.addSource( ihsImage );
bandSelectParams.add( new int[]{1} );
RenderedImage bandHueImage = JAI.create("bandselect", bandSelectParams);
// prepare Intensity and Saturation data
ParameterBlock constParams = new ParameterBlock();
constParams.add( (float) srcImage.getWidth() );
constParams.add( (float) srcImage.getHeight() );
constParams.add( new Byte[]{ (byte) 255 } );
RenderedImage newI = JAI.create("constant", constParams);
RenderedImage newS = JAI.create("constant", constParams);
// prepare rendered hints to merge data with Hue data
ImageLayout imageLayout = new ImageLayout();
imageLayout.setColorModel(ihsColorModel);
imageLayout.setSampleModel(ihsImage.getSampleModel());
RenderingHints rendHints =
new RenderingHints(JAI.KEY_IMAGE_LAYOUT, imageLayout);
// merge data with rendered hints
ParameterBlock modifiedParams = new ParameterBlock();
modifiedParams.addSource( newI );
modifiedParams.addSource( bandHueImage );
modifiedParams.addSource( newS );
RenderedImage modifiedImage =
JAI.create("bandmerge", modifiedParams, rendHints);
// convert ihs data to rgb data
ParameterBlock finalParams = new ParameterBlock();
finalParams.addSource( modifiedImage );
finalParams.add( srcImage.getColorModel() );
RenderedImage finalImage = JAI.create("colorconvert", finalParams);
return finalImage;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doOrderedDither(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 11:08下午 九月 06, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - 轉換像素資料 (ColorConvertDescriptor)
public class JAITester {
private RenderedImage doOrderedDither(RenderedImage image) {
ColorSpace ihsColorSpace = IHSColorSpace.getInstance();
ColorModel ihsColorModel = new ComponentColorModel(ihsColorSpace, new int[]{8, 8, 8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
ParameterBlock pb = new ParameterBlock();
pb.addSource(image);
pb.add(ihsColorModel);
RenderedImage ihsImage = JAI.create("colorconvert", pb);
return ihsImage;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doOrderedDither(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 06:00下午 九月 06, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - 指定像素資料 (ConstantDescriptor)
public class JAITester {
private RenderedImage doOrderedDither(RenderedImage image) {
ParameterBlock constParams = new ParameterBlock();
constParams.add( (float) image.getWidth() );
constParams.add( (float) image.getHeight() );
constParams.add( new Byte[]{ (byte)20, (byte)200, (byte) 200, (byte) 30} );
image = JAI.create("constant", constParams);
return image;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doOrderedDither(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 04:28下午 九月 06, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - 有序抖色法 Ordered dither 減少影像資料, 但視覺上差不多
public class JAITester {
private RenderedImage doOrderedDither(RenderedImage image) {
pixelToFile( new File("build/before.txt"), image );
ParameterBlock odParams = new ParameterBlock();
odParams.addSource(image);
odParams.add( ColorCube.BYTE_496 );
odParams.add( KernelJAI.DITHER_MASK_443 );
return pixelToFile( new File("build/after.txt"), JAI.create("OrderedDither", odParams) );
}
private RenderedImage pixelToFile(File outputFile, RenderedImage img) {
StringBuffer sb = new StringBuffer();
int w = img.getWidth();
int h = img.getHeight();
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
sb.append("[" + x + ", " + y + "]:[");
double[] pixel = img.getData().getPixel(x, y, (double[]) null);
for (int i = 0; i < pixel.length; i++) {
if (i == pixel.length - 1) {
sb.append(pixel[i]);
} else {
sb.append(pixel[i] + ", ");
}
}
sb.append("]");
}
sb.append("\n");
}
try {
FileWriter f = new FileWriter(outputFile);
f.write(sb.substring(0));
f.flush();
f.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(JAITester.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JAITester.class.getName()).log(Level.SEVERE, null, ex);
}
return img;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doOrderedDither(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 01:58上午 九月 01, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - open / close 效果. 清除雜點
public class JAITester {
float[] kernelMatrix = new float[]{
0,0,0,0,0,
0,1,1,1,0,
0,1,1,1,0,
0,1,1,1,0,
0,0,0,0,0
};
private enum OPEN_CLOSE { OPEN, CLOSE }
private OPEN_CLOSE selected = OPEN_CLOSE.CLOSE;
private RenderedImage doOpenOrClose(RenderedImage image) {
image = toGrayScale(image);
KernelJAI kernel = new KernelJAI( 5, 5, kernelMatrix );
if ( selected == OPEN_CLOSE.OPEN ) {
image = doErode(image, kernel);
image = doDeliate(image, kernel);
} else if ( selected == OPEN_CLOSE.CLOSE ) {
image = doDeliate(image, kernel);
image = doErode(image, kernel);
}
return image;
}
private RenderedImage doDeliate(RenderedImage image, KernelJAI kernel) {
ParameterBlock dilateParams = new ParameterBlock();
dilateParams.addSource( image );
dilateParams.add( kernel );
return JAI.create("dilate", dilateParams, null);
}
private RenderedImage doErode(RenderedImage image, KernelJAI kernel) {
ParameterBlock erodeParams = new ParameterBlock();
erodeParams.addSource( image );
erodeParams.add( kernel );
return JAI.create("erode", erodeParams, null);
}
private RenderedImage toGrayScale(RenderedImage imageToBinarize) {
ParameterBlock bandCombineParams = new ParameterBlock();
bandCombineParams.addSource(imageToBinarize);
bandCombineParams.add( new double[][]{ {0.2,0.3,0.4,0.5} } );
return JAI.create("bandcombine", bandCombineParams, null);
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test7.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doOpenOrClose(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 10:43下午 八月 30, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - bandMerge 把兩張圖的顏色放在一張圖
public class JAITester {
private PlanarImage doBandMerge() {
ParameterBlockJAI mergeParams = new ParameterBlockJAI("bandmerge");
mergeParams.setSource( pixelToFile(new File("build/test1.txt"), toGrayScale( JAI.create("fileload", "test1.jpg") ) ), 0);
mergeParams.setSource( pixelToFile(new File("build/test2.txt"), toGrayScale( JAI.create("fileload", "test2.jpg") ) ), 1);
mergeParams.setSource( pixelToFile(new File("build/test3.txt"), toGrayScale( JAI.create("fileload", "test3.jpg") ) ), 2);
PlanarImage result = JAI.create( "bandmerge", mergeParams, null);
pixelToFile( new File("build/bandmergeresult.txt"), result );
return result;
}
private PlanarImage toGrayScale(PlanarImage imageToBinarize) {
ParameterBlock bandCombineParams = new ParameterBlock();
bandCombineParams.addSource(imageToBinarize);
bandCombineParams.add( new double[][]{ {0.2,0.3,0.4,0.5} } );
return JAI.create("bandcombine", bandCombineParams, null);
}
private RenderedImage pixelToFile(File outputFile, RenderedImage img) {
StringBuffer sb = new StringBuffer();
int w = img.getWidth();
int h = img.getHeight();
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
sb.append("[" + x + ", " + y + "]:[");
double[] pixel = img.getData().getPixel(x, y, (double[]) null);
for (int i = 0; i < pixel.length; i++) {
if (i == pixel.length - 1) {
sb.append(pixel[i]);
} else {
sb.append(pixel[i] + ", ");
}
}
sb.append("]");
}
sb.append("\n");
}
try {
FileWriter f = new FileWriter(outputFile);
f.write(sb.substring(0));
f.flush();
f.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(JAITester.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JAITester.class.getName()).log(Level.SEVERE, null, ex);
}
return img;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doBandMerge())), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 08:03下午 八月 30, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - bandSelect 指定需要的band資料
public class JAITester {
/** Present selected band */
private int[] bandSelect = new int[]{2, 2, 2, 2};
private PlanarImage doBandSelect(PlanarImage imageToBandSelect) {
pixelToFile( new File("build/before.txt"), imageToBandSelect );
imageToBandSelect = JAI.create("bandselect", imageToBandSelect, bandSelect);
pixelToFile( new File("build/after.txt"), imageToBandSelect );
return imageToBandSelect;
}
private void pixelToFile(File outputFile, RenderedImage img) {
StringBuffer sb = new StringBuffer();
int w = img.getWidth();
int h = img.getHeight();
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
sb.append("[" + x + ", " + y + "]:[");
double[] pixel = img.getData().getPixel(x, y, (double[]) null);
for (int i = 0; i < pixel.length; i++) {
if (i == pixel.length - 1) {
sb.append(pixel[i]);
} else {
sb.append(pixel[i] + ", ");
}
}
sb.append("]");
}
sb.append("\n");
}
try {
FileWriter f = new FileWriter(outputFile);
f.write(sb.substring(0));
f.flush();
f.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(JAITester.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JAITester.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doBandSelect(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 03:35下午 八月 30, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - binarize 二值化
java.lang.IllegalArgumentException: Binarize source image must be single-banded., 我猜這一個 band 就是灰階的圖.
public class JAITester {
private double threshold = 128;
private PlanarImage doBinarize(PlanarImage imageToBinarize) {
// convert to grayscale
ParameterBlock bandCombineParams = new ParameterBlock();
bandCombineParams.addSource(imageToBinarize);
bandCombineParams.add( new double[][]{ {0.2,0.3,0.4,0.5} } );
imageToBinarize = JAI.create("bandcombine", bandCombineParams, null);
// binarize
ParameterBlock binarizeParams = new ParameterBlock();
binarizeParams.addSource( imageToBinarize );
binarizeParams.add( threshold );
return JAI.create("binarize", binarizeParams);
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doBinarize(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 10:44上午 八月 30, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - 色階分佈 Histogram
| Name | Class Type | Default Value |
|---|---|---|
| roi | javax.media.jai.ROI | null |
| xPeriod | java.lang.Integer | 1 |
| yPeriod | java.lang.Integer | 1 |
| numBins | int[] | {256} |
| lowValue | double[] | {0.0} |
| highValue | double[] | {256.0} |
public class JAITester {
private PlanarImage doHistogram(PlanarImage imageToHistogram) {
ParameterBlock pb = new ParameterBlock();
pb.addSource( imageToHistogram );
pb.add( null ); // region-of-interest (ROI)
pb.add( 1 ); // xPeriod
pb.add( 1 ); // yPeriod
// numBins : must have an array length of 1
pb.add( new int[]{256} );
// lowValue : must have an array length of 1
pb.add( new double[]{0} );
// highValue : must have an array length of 1
pb.add( new double[]{256} );
imageToHistogram = JAI.create( "histogram", pb );
Histogram h = (Histogram) imageToHistogram.getProperty("histogram");
int[][] bins = h.getBins();
for ( int i = 0; i < bins.length; i++ ) {
for ( int j = 0; j < bins[i].length; j++ ) {
System.out.println("(" + i + "," + j + ")=" + bins[i][j]);
}
}
return imageToHistogram;
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test2.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doHistogram(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 02:29上午 八月 27, 2008 by shooeugenesea in Java Image | 迴響[0]
JAI - BandCombine 處理
double[][] matrix = new double[destBands][sourceBands + 1];sourceBands 是來源圖的 band, destBands 目地圖的 band. 這個 band 可以透過 RenderedImage 呼叫
RenderedImage.getSampleModel().getNumBands(); 取得.
// s = source pixel
// d = destination pixel
for(int i = 0; i < destBands; i++) {
d[i] = matrix[i][sourceBands];
for(int j = 0; j < sourceBands; j++) {
d[i] += matrix[i][j]*s[j];
}
}
這個公式其實蠻單純的,
{
{0.5,0.6,0.7,0.1}
}
這是候原圖的像素 [0,0] 的值為 [101.0, 81.0, 48.0]
s[] = {101, 81, 48}
destBands = 1
sourceBands = 3
計算過程就是
matrix = {0.5,0.6,0.7,0.1}
i = 0 => d[0] = matrix[0][3] = 0.1
j = 0 => d[0] = d[0] + matrix[0][0] * s[0] = 0.1 + 0.5 * 101 = 50.6
j = 1 => d[0] = d[0] + matrix[0][1] * s[1] = 50.6 + 0.6 * 81 = 99.2
j = 2 => d[0] = d[0] + matrix[0][2] * s[2] = 99.2 + 0.7 * 48 = 132.8 (final)
所以 [0,0] 在 BandCombine 之後就變成 [132.8], 然後在讀取 SampleModel 的 Data 時發現好像有四捨五入就變 133.
{
{0.5,0.6,0.7,0.1},
{0.2,0.3,0.4,0.8}
};
上面已經算出第一個陣列值的結果是 132.8, 接下來再算第二個陣列.
matrix = {0.2,0.3,0.4,0.8}
i = 0 => d[0] = matrix[0][3] = 0.1
j = 0 => d[0] = d[0] + matrix[0][0] * s[0] = 0.8 + 0.2 * 101 = 21.0
j = 1 => d[0] = d[0] + matrix[0][1] * s[1] = 21.0 + 0.3 * 81 = 45.3
j = 2 => d[0] = d[0] + matrix[0][2] * s[2] = 45.6 + 0.4 * 48 = 64.8
最後第二組陣列計算結果會讓像素 [0,0] 的值變成 [132.8, 64.8], 然後 RenderedImage.getSampleModel().getData() 的結果可看到像素 [0,0] = [133,65]
public class JAITester {
private PlanarImage doHistogram(PlanarImage imageToHistogram) {
int numBands = imageToHistogram.getSampleModel().getNumBands();
double[][] matrix = new double[][]{
{0.5,0.6,0.7,0.1},
{0.2,0.3,0.4,0.8}
};
pixelToFile(new File("build/beforeBandCombine.txt"), imageToHistogram);
ParameterBlock pb = new ParameterBlock();
pb.addSource(imageToHistogram);
pb.add(matrix);
imageToHistogram = JAI.create("bandcombine", pb, null);
pixelToFile(new File("build/afterBandCombine.txt"), imageToHistogram);
return imageToHistogram;
}
private void pixelToFile(File outputFile, RenderedImage img) {
StringBuffer sb = new StringBuffer();
int w = img.getWidth();
int h = img.getHeight();
for ( int x = 0; x < w; x++ ) {
for ( int y = 0; y < h; y++ ) {
sb.append("[" + x + ", " + y + "]:[");
double[] pixel = img.getData().getPixel(x, y, (double[])null);
for ( int i = 0; i < pixel.length; i++ ) {
if ( i == pixel.length - 1 ) {
sb.append( pixel[i] );
} else {
sb.append( pixel[i] + ", " );
}
}
sb.append("]");
}
sb.append("\n");
}
try {
FileWriter f = new FileWriter(outputFile);
f.write(sb.substring(0));
f.flush();
f.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(JAITester.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JAITester.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* http://forums.java.net/jive/message.jspa?messageID=221209
* this gets rid of exception for not using native acceleration */
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
private static final String IMG_FILE_1 = "test1.jpg";
public static void main(String[] args) {
JAITester test = new JAITester();
test.test();
}
private void test() {
PlanarImage input = JAI.create("fileload", IMG_FILE_1);
JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");
jPanelMain.add(new JScrollPane(new DisplayJAI(doHistogram(input))), "grow");
show(jPanelMain);
}
private void show(JComponent comp) {
JFrame f = new JFrame();
f.setLocation(50, 50);
f.setPreferredSize(new Dimension(800, 600));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setVisible(true);
}
}
Posted at 02:06下午 八月 24, 2008 by shooeugenesea in Java Image | 迴響[0]