getting pixel data from image as single value

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
 
K

Knute Johnson

jimgardener said:
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


See BufferedImage.getRGB() in the docs. The only disadvantage I can see
is that it converts the pixel data to the default color model,
TYPE_INT_ARGB, but in most cases I doubt that will be an issue for you.
Even for grayscale images.

PixelGrabber is designed for asynchronous acquisition of pixel data such
as would occur if the image came slowly from the net. The image
observer/producer scheme, while it works really well, is more
complicated than you need in most cases.

For the three most commonly used methods of image loading, see;

http://rabbitbrush.frazmtn.com

and the 'How to load images' examples.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top