J
jimgardener
hi
i need to get value of each pixel in an image as a single value.The
image may be color or greyscale .I tried as follows.I don't know if i
am doing it right,and i want to know if it can be done in more compact
manner.If anyone can advise/help pls do
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class PixelDataDemo{
public static void main(String[] args){
PixelGrabber pg;
BufferedImage img;
try{
img=ImageIO.read(newFile("F:\\mygallery\
\myimage.png"));
int ht=img.getHeight() ;
int wd=img.getWidth();
int[] pixels = new int[wd * ht];
pg = new PixelGrabber(img, 0, 0, wd, ht, pixels, 0, wd);
pg.grabPixels();
double[] result =new double[wd*ht];
ColorModel cm = pg.getColorModel();
for (int i=0; i<result.length; i++){
result = cm.getBlue(pixels) + cm.getGreen(pixels) +
cm.getRed(pixels);
result /= 3.0;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
thanks
jim
i need to get value of each pixel in an image as a single value.The
image may be color or greyscale .I tried as follows.I don't know if i
am doing it right,and i want to know if it can be done in more compact
manner.If anyone can advise/help pls do
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class PixelDataDemo{
public static void main(String[] args){
PixelGrabber pg;
BufferedImage img;
try{
img=ImageIO.read(newFile("F:\\mygallery\
\myimage.png"));
int ht=img.getHeight() ;
int wd=img.getWidth();
int[] pixels = new int[wd * ht];
pg = new PixelGrabber(img, 0, 0, wd, ht, pixels, 0, wd);
pg.grabPixels();
double[] result =new double[wd*ht];
ColorModel cm = pg.getColorModel();
for (int i=0; i<result.length; i++){
result = cm.getBlue(pixels) + cm.getGreen(pixels) +
cm.getRed(pixels);
result /= 3.0;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
thanks
jim