From: jimgardener on
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[i] = cm.getBlue(pixels[i]) + cm.getGreen(pixels[i]) +
cm.getRed(pixels[i]);
result[i] /= 3.0;
}

}catch(Exception e){
e.printStackTrace();
}
}
}


thanks
jim
From: Knute Johnson on
jimgardener wrote:
> 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[i] = cm.getBlue(pixels[i]) + cm.getGreen(pixels[i]) +
> cm.getRed(pixels[i]);
> result[i] /= 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.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access