DCT of buffered image using JTransform - Java -
i'm trying dct of bufferedimage using jtransform. when visualise transform looks http://tinypic.com/r/2vcxhzo/8
in order use jtransform need convert bufferedimage 2d double array. i've tried 2 different methods change bufferedimage double array
public double[][] convertto2darray(bufferedimage image) { final byte[] pixels = ((databufferbyte) image.getraster() .getdatabuffer()).getdata(); final int width = image.getwidth(); final int height = image.getheight(); double[][] result = new double[height][width]; final boolean hasalphachannel = image.getalpharaster() != null; if (hasalphachannel) { final int pixellength = 4; (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixellength) { int argb = 0; argb += (((int) pixels[pixel] & 0xff) << 24); // alpha argb += ((int) pixels[pixel + 1] & 0xff); // blue argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red result[row][col] = argb; col++; if (col == width) { col = 0; row++; } } } else { final int pixellength = 3; (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixellength) { int argb = 0; argb += -16777216; // 255 alpha argb += ((int) pixels[pixel] & 0xff); // blue argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red result[row][col] = argb; col++; if (col == width) { col = 0; row++; } } } return result; }
i've tried
private double[][] bufferedimagetoarray(bufferedimage image) { int h = image.getheight(); int w = image.getwidth(); int[][] array = new int[h][w]; double[][] result; (int count = 0; count < h; count++) { (int loop = 0; loop < w; loop++) { int gray = image.getrgb(loop, count) & 0xff; // add values array array[count][loop] = gray; } } result = todoublearray(array); return result; }
i've implemented transform
public double[][] applydct(double[][] image) { doubledct_2d transform = new doubledct_2d(image.length, image[0].length); transform.forward(image, true); return image; }
i tried using opencv's dct transform gives same output shown in link.
ty (i kept blue channel simplicity). shows energy compaction in upper left corner of result image.
import java.awt.graphicsconfiguration; import java.awt.graphicsdevice; import java.awt.graphicsenvironment; import java.awt.image; import java.awt.transparency; import java.awt.image.bufferedimage; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; public class testdct { public static void main(string[] args) { imageicon icon = new imageicon(args[0]); image image = icon.getimage(); int w = image.getwidth(null); int h = image.getheight(null); graphicsdevice gs = graphicsenvironment.getlocalgraphicsenvironment().getscreendevices()[0]; graphicsconfiguration gc = gs.getdefaultconfiguration(); bufferedimage img = gc.createcompatibleimage(w, h, transparency.opaque); img.getgraphics().drawimage(image, 0, 0, null); int[] rgb1 = new int[w*h]; img.getraster().getdataelements(0, 0, w, h, rgb1); double[] array = new double[w*h]; (int i=0; i<w*h; i++) array[i] = (double) (rgb1[i] & 0xff); org.jtransforms.dct.doubledct_2d tr = new org.jtransforms.dct.doubledct_2d(w, h); tr.forward(array, true); (int i=0; i<w*h; i++) { // grey levels int val= math.min((int) (array[i]+128), 255); rgb1[i] = (val <<16) | (val << 8) | val; } img.getraster().setdataelements(0, 0, w, h, rgb1); icon = new imageicon(img); jframe frame = new jframe("fft"); frame.setbounds(20, 30, w, h); frame.add(new jlabel(icon)); frame.setvisible(true); }
}
Comments
Post a Comment