Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By camickr

Thread: reading pixel values from images

  1. #1
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Post reading pixel values from images

    hi im doing project on extracting a character from images. for that purpose i want to read pixel values from images and store in the 1d array and this array im using for testing purpose. and in using only grayscale images. can anyone provide me the java code to read pixel vlues from images and store it in array.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: reading pixel values from images

    Look at the BufferedImage getRGB() method.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: reading pixel values from images

    can anyone provide me the java code
    No, this is a forum, not a code mill. If you want someone to write your programs for you, hire a programmer.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Post Re: reading pixel values from images

    i have written code to read pixel values from images

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;

    public class ShowImage extends Panel {
    BufferedImage image;

    public ShowImage() {
    try {
    System.out.println("Enter image name\n");
    BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
    String imageName=bf.readLine();
    File input = new File("ab.jpg");
    image = ImageIO.read(input);
    } catch (IOException ie) {
    System.out.println("Error:"+ie.getMessage());
    }

    }

    public void paint(Graphics g) {
    g.drawImage( image, 100,100, null);
    }

    static public void main(String args[]) throws Exception {
    JFrame frame = new JFrame("image");
    Panel panel = new ShowImage();
    frame.getContentPane().add(panel);
    frame.setSize(500, 500);
    frame.setVisible(true);
    }
    }


    im not geting any error but geting negative values can u tel me the solution for this

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: reading pixel values from images

    geting negative values
    Please show the values you are getting.
    What statement generates the negative values?

  6. #6
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: reading pixel values from images

    im extremally sorry i posted wrong code

    this is the correct code
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import java.lang.Exception;
    
    import java.awt.Graphics;
    import java.awt.image.PixelGrabber;
    import java.awt.image.*;
    import java.util.*;
    
    
      public class ShowImage3 extends Panel {
          BufferedImage  image;
        public ShowImage3() {
          try{
          System.out.println("Enter image name\n");
          BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
           String imageName=bf.readLine();
          File input = new File("ab.jpg");
              image = ImageIO.read(input);
    
     int w = image.getWidth(null);
    int h = image.getHeight(null);
    
    
    int pixels[] = new int[w*h];
          PixelGrabber pg = new PixelGrabber(image,0,0,w,h,pixels,0,w);
      
    pg.grabPixels();
    
    int[] data = (int[]) pg.getPixels();
          
    
    
    for(int i=0;i<w*h;i++)
    {
    System.out.println(""+data[i]);
    }
     
    
    
    
    
          }
        
     catch (Exception ie) {
            System.out.println("Error:"+ie.getMessage());
          }  
    }
    
        public void paint(Graphics g) {
          g.drawImage( image, 100,100, null);
        }
      
        static public void main(String args[]) throws Exception {
          JFrame frame = new JFrame("image");
          Panel panel = new ShowImage3();
          frame.getContentPane().add(panel);
          frame.setSize(500, 500);
          frame.setVisible(true);
        }
      }

    and output which im geting is


    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    Last edited by Norm; 12-22-2011 at 06:31 PM. Reason: added code tags

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: reading pixel values from images

    What color do those values represent?
    You should look at the values in hexadecimal not in base 10.
    -1 is xFFFFFFFF
    Last edited by Norm; 12-22-2011 at 06:40 PM.

  8. #8
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: reading pixel values from images

    color of pixel value is black

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: reading pixel values from images

    What values do you expect to print out for black?

  10. #10
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: reading pixel values from images

    1 if black pixel is present else print zero

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: reading pixel values from images

    1 if black pixel is present else print zero
    Why do you expect the RGB value for a black pixel to be 1?
    There are 24 bits in the RGB value.
    Print out the bits for the Color.black object to see the value of the bits are for black.

  12. #12
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: reading pixel values from images

    which part of code i should modify to get proper output

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: reading pixel values from images

    What output is "proper" output?
    The code works for me.

  14. #14
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: reading pixel values from images

    what output ur geting same -1 ya something else

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: reading pixel values from images

    The output will depend on the color of the image. You would get -1 for white.

    I get this (in hexadecimal) for a solid colored image: ffff9966 ffff9966 ffff9966 ffff9966 ffff9966 ....
    If you want to see the color(sort of orange), use the Color class's constructor to create this color.

  16. #16
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: reading pixel values from images

    if im using only grayscale images means it contains only black pixel
    what should be the output
    what does -1 value represnt

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: reading pixel values from images

    -1 is 0xFFFFFFFF
    You can use the Color class's constructor to create a color object and display it to see what color it is.
    For example: new Color(0xFF, 0X15, 0xFF)
    The Integer toHexString() method is useful for printing out the RGB values in a more understandable form.
    Last edited by Norm; 12-22-2011 at 07:24 PM.

  18. #18
    utkarsh 1 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: reading pixel values from images

    thanks a lot for ur help

  19. #19
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: reading pixel values from images

    Don't use AWT components in a Swing application. Swing component start with "J".
    DarrylBurke likes this.

Similar Threads

  1. Replies: 8
    Last Post: 05-18-2011, 03:01 PM
  2. How to read the pixel values of a jpg binary image
    By carindia in forum New To Java
    Replies: 2
    Last Post: 09-17-2010, 06:19 PM
  3. problem with pixel values of an image
    By SreerajSarma in forum New To Java
    Replies: 6
    Last Post: 03-06-2010, 03:47 PM
  4. compare two images pixel by pixel
    By java_bond in forum Advanced Java
    Replies: 6
    Last Post: 03-02-2010, 11:27 AM
  5. Replies: 14
    Last Post: 04-10-2009, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •