Results 1 to 6 of 6

Thread: Counting Pixels

  1. #1
    shaungoater is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default Counting Pixels

    I am using a buffered image and wish to count the pixels in the image to determine the most prominent colors. I have done some research and think i need to use the ColorModel class. If anyone could tell me how to implement this
    and use the ColorModel class it would be greatly appreciated.
    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    the most prominent colors
    By this do you mean the colors that occur with the highest frequency, colors with particular qualities, or specific/target colors?

  3. #3
    shaungoater is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default

    I would like to obtain a count of the number of red blue and green colours

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    
    public class ColorCount {
        public static void main(String[] args) throws IOException {
            String path = pathToImage
            BufferedImage image = ImageIO.read(new File(path));
            int redCount = 0, greenCount = 0, blueCount = 0;
            int red   = Color.red.getRGB();
            int green = Color.green.getRGB();
            int blue  = Color.blue.getRGB();
            System.out.printf("red = %d  green = %d  blue = %d%n",
                               red, green, blue);
            int w = image.getWidth();
            int h = image.getHeight();
            for(int y = 0; y < h; y++) {
                for(int x = 0; x < w; x++) {
                    int pixel = image.getRGB(x, y);
                    if(pixel == red)   redCount++;
                    if(pixel == green) greenCount++;
                    if(pixel == blue)  blueCount++;
                }
            }
            System.out.printf("redCount = %d  greenCount = %d  blueCount = %d%n",
                               redCount, greenCount, blueCount);
        }
    }

  5. #5
    shaungoater is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default Thanks

    Thanks for the excellent help your code was very useful. Could you please explain what the following code does especially the role of "d" and "n".

    ("red = %d green = %d blue = %d%n",
    red, green, blue);

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    Formatter class was introduced in j2se 1.5
    d: format the value to an integer
    n: new line.

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 03:03 PM
  2. Modified Pixels
    By monkey04 in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 09:15 AM
  3. Blurring pixels
    By tim in forum New To Java
    Replies: 0
    Last Post: 01-01-2008, 03:06 PM
  4. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 06:45 PM
  5. Deleting certain image pixels..
    By Brightside in forum New To Java
    Replies: 1
    Last Post: 05-22-2007, 09:21 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
  •