getting pixel data from image as single value

Discussion in 'Java' started by jimgardener, Jul 8, 2008.

  1. jimgardener

    jimgardener Guest

    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
    jimgardener, Jul 8, 2008
    #1
    1. Advertising

  2. 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 = 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.

    --

    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
    Knute Johnson, Jul 8, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rfractal30
    Replies:
    5
    Views:
    6,906
    rfractal30
    Mar 6, 2005
  2. cppaddict
    Replies:
    5
    Views:
    393
    Thomas Lempa
    May 24, 2004
  3. André Wagner

    Fastest pixel by pixel operation

    André Wagner, Mar 17, 2008, in forum: Java
    Replies:
    12
    Views:
    1,025
    Larry A Barowski
    Mar 19, 2008
  4. jimgardener
    Replies:
    3
    Views:
    322
  5. Neraph

    getting pixel colors from an image?

    Neraph, Feb 14, 2006, in forum: Javascript
    Replies:
    3
    Views:
    160
    Thomas 'PointedEars' Lahn
    Feb 14, 2006
Loading...

Share This Page