Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Amanoo

Thread: Gemiddelde kleur van een BufferedImage berekenen

  1. #1
    Amanoo is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Gemiddelde kleur van een BufferedImage berekenen

    Hi. I am currently working on a program which analyses parts of my screen and calculates the average colorvalue of each chunk (a block of approximately 150*150 pixels). By this I mean that if, for example, half the chunk contains the colour green, and the rest is blue, that the result would be something like teal. Yellow+red would make orange, etc. In practice it will use my display for this analysis, so it will have to deal with a lot of very different colours in practice. Each chunk is loaded into a BufferedImage. Currently my code takes the colour (the get-method I use for this returns this in the form of an int value) of each pixel in the BufferedImage. I add up all of these values and then I divide them by the amount of analysed pixels. You can see the code down here, width and height are information about the size of each chunk of which the average colour value has to be calculated. This allows me to vary the exact size, but 150x150 is a good indication.
    The problem is that my code isn't fast enough. On one of my CPU cores it can be used about 50 times per second.This is way too slow for my purposes. Around 900 times per second is practically a minimum. If really necessary, my code can afford to lose a bit of accuracy. All the analysed chunks are located on the border of my screen, so I tried to create different threads that keep checking one chunk each, instead of one linear code which checks all chunks one by one. While I like the idea behind this approach, it didn't make the code much faster either.
    This is the only way I managed to think of to calculate these values. I downloaded the JavaCL library, but I can't say I have figured out anything about this library. A completely different approach to the problem is perfectly allowable, but I would need help with that. I was thinking about using OpenGL or OpenCL, for all I care the code may be allowed to make my GPU bloody hot, but something entirely different from that is good as well. I tried resizing the BufferedImage to a 1x1 resolution (without using OpenCL or OpenGL), but this proved to be way too inaccurate, and I haven't even measured if it was any faster yet.
    All help is welcome, because I am running out of ideas.

    This is the code which needs to be made faster:
    Java Code:
    public Color getAverageColor(BufferedImage screencapture) {
    	int i = 0;
    	for (int x = 0; x < width; x++) {
    		for (int y = 0; y < height; y++) {
    			i += screencapture.getRGB(x, y);
    		}
    	}
    	return new Color(i / width * height);
    }
    As I said, is necessary entirely rewriting this is not a problem, as long as it is faster.
    Last edited by Amanoo; 05-03-2012 at 11:01 AM.

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

    Default Re: Gemiddelde kleur van een BufferedImage berekenen

    Jos, where are you?

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

  3. #3
    Amanoo is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Gemiddelde kleur van een BufferedImage berekenen

    I'm a moron. For some reason I started thinking this was a Dutch forum, so I wrote in Dutch...
    I guess I don't need to tell you that I can be very scatterminded.
    Anyway, I just translated the whole bloody text.

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

    Default Re: Gemiddelde kleur van een BufferedImage berekenen

    To be honest, I wouldn't expect your present algorithm to at all produce an 'average' color for the range. With any decent number of pixels, I would expect integer overflow; apart from that, I would expect you need to separately average the r/g/b components, not attempt to average their int representation.

    Of course, adding steps for segregating the r/g/b and separately averaging, then recombining the 3 averages would make it slower still -- but correct.

    Lets see if anyone has a better idea.

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

  5. #5
    Amanoo is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Gemiddelde kleur van een BufferedImage berekenen

    Currently I am having very nonsensical measurements... Sometimes my original code seems to be more than fast enough, even with your r/g/b tip implemented, and sometimes it seems way too slow. It depends on how I measure. I think I should just test my code in practice as soon as I get the hardware I need for it.
    DarrylBurke likes this.

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

    Default Re: Gemiddelde kleur van een BufferedImage berekenen

    That would probably be better, yes.

    db

    edit And using the form of getRGB(...) that returns an array, and then iterating over the array, should be measurably faster that your present method call for each pixel.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Gemiddelde kleur van een BufferedImage berekenen

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

Similar Threads

  1. BufferedImage to ImageIcon
    By han003 in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 11:13 AM
  2. bufferedImage
    By ranadav in forum New To Java
    Replies: 3
    Last Post: 06-05-2010, 03:22 PM
  3. BufferedImage rgb
    By Bill87 in forum New To Java
    Replies: 2
    Last Post: 02-25-2010, 06:31 PM
  4. Using BufferedImage
    By timkd127 in forum New To Java
    Replies: 5
    Last Post: 12-19-2009, 09:17 PM
  5. BufferedImage through FTP
    By dudejonne in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 05:36 PM

Posting Permissions

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