Results 1 to 13 of 13
  1. #1
    mahesh_chavan is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default How to get individual pixel colors ?

    I want to know alph, red, blue, and green values of pixels in buffered image. Following displays the picture correctly, But all red blue and green values are always zero. Can anybody guide me how to get actual values :
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    
    public class BuffIt {
      public static void main (String args[]) {
        // Get Image
        ImageIcon icon = new ImageIcon(args[0]);
        Image image = icon.getImage();
        int totval = 0;
        int picval;
            int rd, bl, gr;
        picval = 0;
            int i,j,w,h, clr;
    
    
        // Create empty BufferedImage, sized to Image
        BufferedImage buffImage =
          new BufferedImage(
              image.getWidth(null),
              image.getHeight(null),
              BufferedImage.TYPE_INT_ARGB);
            w = image.getWidth(null);
            h = image.getHeight(null);
            System.out.println("width : " + Integer.toString(w));
            System.out.println("height : " + Integer.toString(h));
    
            for (j=0;j<h;j++) {
            for (i=0;i<w;i++) {
            clr = buffImage.getRGB(i,j);
        rd   = (clr & 0x00ff0000) >> 16;
          gr = (clr & 0x0000ff00) >> 8;
          bl  =  clr & 0x000000ff;
            picval = ( picval + rd + gr + bl);
            if ( rd > 0 || gr > 0 || bl > 0 ) {
            System.out.println("red : " + Integer.toString(rd));
            System.out.println("green : " + Integer.toString(gr));
            System.out.println("blue : " + Integer.toString(bl));
            System.exit(0);
            }
        }
            }
            System.out.println("picval : " + Integer.toString(picval));
            totval = (totval + picval ) & 0xffffffff;
            System.out.println("totval: " + Integer.toString(totval));
    
    
    
        // Draw Image into BufferedImage
        Graphics g = buffImage.getGraphics();
        g.drawImage(image, 0, 0, null);
    
        // Show success
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(buffImage));
        frame.getContentPane().add(label);
        frame.pack();
        frame.show();
      }
    }

    Please help
    Last edited by Fubarable; 06-16-2012 at 07:59 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to get individual pixel colors ?

    Look at the order that you do things:

    • When do you create the BufferedImage?
    • When do you extract the numeric data from the BufferedImage?
    • When do you draw to the BufferedImage?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: How to get individual pixel colors ?

    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to get individual pixel colors ?

    Posted today and he never replied to my post. I must thank him for allowing me to waste my time on him.

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

    Default Re: How to get individual pixel colors ?

    Last edited by DarrylBurke; 06-17-2012 at 09:28 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    mahesh_chavan is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to get individual pixel colors ?

    Sorry Fuberble, I dd not reply to your querry
    First ImageIcon icon = new ImageIcon(args[0]);
    Then I create buff image BufferedImage buffImage =
    new BufferedImage(
    image.getWidth(null),
    image.getHeight(null),
    BufferedImage.TYPE_INT_ARGB);
    w = image.getWidth(null);
    After that I rey to read pixel data.
    Then I draw image.
    Actually, drawing of image has nothing to do wuth getRGB.
    In my other app i capture image from webcam without showing it ( no gui) and save it in jpg format.
    When buff image is created using Image all data is lready there
    I shall see other sources mentioned above

  7. #7
    mahesh_chavan is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to get individual pixel colors ?

    Dear DarryBurke,
    I am sorry to post the same question on diff. forums. But the reason is that I am till not getting the answer. The old code you have mentioned is just for display. For getting pixels values I have adopted code from RoseIndia. Despite my best efforts, I am unable to get the values and I need them to write a geiger counter program.
    Please show me the way how to do it.

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

    Default Re: How to get individual pixel colors ?

    Quote Originally Posted by mahesh_chavan View Post
    ... I have adopted code from RoseIndia.
    Don't. I'm yet to see any halfway decent code that came from that site.

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

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: How to get individual pixel colors ?

    With changes as per Fubarable post#2 I get:
    width : 55
    height : 68
    clr=ffb8b8b0, j=1, i=40
    red : 184, green : 184, blue : 176
    clr=ff706868, j=1, i=41
    red : 112, green : 104, blue : 104
    clr=ff202428, j=2, i=40
    red : 32, green : 36, blue : 40
    clr=ff201018, j=2, i=41
    red : 32, green : 16, blue : 24
    clr=ff383030, j=2, i=42
    red : 56, green : 48, blue : 48
    clr=ff505850, j=3, i=39
    red : 80, green : 88, blue : 80
    clr=ff181010, j=3, i=40
    red : 24, green : 16, blue : 16
    clr=ff201818, j=3, i=41
    red : 32, green : 24, blue : 24
    clr=ff282028, j=3, i=42
    red : 40, green : 32, blue : 40
    clr=ff181010, j=4, i=39
    ...
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    mahesh_chavan is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to get individual pixel colors ?

    When instead of
    BufferedImage buffImage =
    new BufferedImage(
    image.getWidth(null),
    image.getHeight(null),
    BufferedImage.TYPE_INT_ARGB);
    I used
    File file= new File(args[0]);
    buffImage = ImageIO.read(file);
    It worked and gave me correct values of red, green blue !

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: How to get individual pixel colors ?

    The important thing was to have some image with colors in the buffImage variable. Your earlier code didn't.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    mahesh_chavan is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to get individual pixel colors ?

    Dear Norm.
    Thanks you.
    I did not understand how the program works for you.
    First two lines of my program create image by accepting file name on command line.
    ImageIcon icon = new ImageIcon(args[0]);
    Image image = icon.getImage();
    That is why the image is displayed, but with BufferImage it still gives me all zeros ifI do not use
    File file= new File(args[0]);
    buffImage = ImageIO.read(file);
    I have tried by first displaying values and then using getRGB but it did not help.
    Will you please show me the entire code which shows you correct pixel values ?
    Thanking you once again.
    -Mahesh Chavan

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: How to get individual pixel colors ?

    I followed the suggestions made in post#2 by moving lines 50-51 to after line 22.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Scanning Image Pixel by Pixel
    By the_transltr in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2012, 04:01 PM
  2. Controlling individual threads
    By youngstorm in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-03-2010, 10:37 PM
  3. * vs. individual package name?
    By XmisterIS in forum New To Java
    Replies: 3
    Last Post: 09-01-2010, 12:19 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. Calculating Individual Numbers
    By TheKnight in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 12:51 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
  •