5 Replies - 1520 Views - Last Post: 10 April 2012 - 11:26 AM Rate Topic: -----

#1 ashishdonvir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-April 12

Need Faster way to get RGB value for each Pixel of a Buffered Image

Posted 10 April 2012 - 05:11 AM

What is the to get RGB value of each pixel of a Buffered Image?

Right now i am getting RGb value using two for loops as shown in code below but it took too much long time to get rgb values as loop runs 479999 times for my image and it can even rise when i will use 16 bit image.

SO i need some faster way to get pixel values

here is my code what i tried

 BufferedImage bi=ImageIO.read(new File("C:\\images\\Sunset.jpg"));    

      {
     int countloop=0;  

           for (int x = 0; x <bi.getWidth(); x++) {

                    for (int y = 0; y < bi.getHeight(); y++) {

                         Color c = new Color(bi.getRGB(x, y));

                          System.out.println("red=="+c.getRed()+" green=="+c.getGreen()+"    blue=="+c.getBlue()+"  countloop="+countloop++);                                                                                                                              
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Need Faster way to get RGB value for each Pixel of a Buffered Image

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9040
  • View blog
  • Posts: 33,540
  • Joined: 27-December 08

Re: Need Faster way to get RGB value for each Pixel of a Buffered Image

Posted 10 April 2012 - 06:48 AM

You can use the getRGB() method that returns an int[], but it won't change the number of pixels you have to evaluate.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 996
  • Posts: 2,212
  • Joined: 05-April 11

Re: Need Faster way to get RGB value for each Pixel of a Buffered Image

Posted 10 April 2012 - 09:51 AM

I believe it is actually the System.out.println's that are taking most of your time :)
Was This Post Helpful? 1
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 766
  • View blog
  • Posts: 2,916
  • Joined: 12-May 09

Re: Need Faster way to get RGB value for each Pixel of a Buffered Image

Posted 10 April 2012 - 10:11 AM

I was thinking about suggesting building up a StringBuilder object and then calling a print on it once you've finished. I've been out of Java for so long I kind of forget if that works. Definitely worth a try since it's an easy change.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,176
  • Joined: 06-March 08

Re: Need Faster way to get RGB value for each Pixel of a Buffered Image

Posted 10 April 2012 - 10:50 AM

You are calling getWidth() and getHeight() so many times for nothing just call them once and store their values in a local variable

Color c = new Color()

no need to create A new Color object for every pixel, if your only purpose is to display the value simply save getRGB() in a int and print out of it. For each pixel, you create a new Color object andf you access 3 of its method

You can always use that method

http://docs.oracle.c...html#getRGB(int, int, int, int, int[], int, int)
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,176
  • Joined: 06-March 08

Re: Need Faster way to get RGB value for each Pixel of a Buffered Image

Posted 10 April 2012 - 11:26 AM

Yup xclite :^:

the execution of a stament like:
System.out.println("red==" + c.getRed()+ " green==" + c.getGreen() + " blue==" + c.getBlue()+" countloop=" + countloop++);

Implies the creation of a new StringBuilder() (ONE for each processed pixel) and the creation of a String object (when doing stringBuilder.toString() to pass to println())
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1