Skip to content
Advertisement

Get average color on bufferedimage and bufferedimage portion as fast as possible

I am trying to find image in an image. I do this for desktop automation. At this moment, I’m trying to be fast, not precise. As such, I have decided to match similar image solely based on the same average color.

If I pick several icons on my desktop, for example:

several desktop icons

And I will search for the last one (I’m still wondering what this file is):

some file icon

You can clearly see what is most likely to be the match:

image displaying average colors in regions

In different situations, this may not work. However when image size is given, it should be pretty reliable and lightning fast.

I can get a screenshot as BufferedImage object:

JavaScript

Of course, the image to search image will be loaded from template saved in a file:

JavaScript

I explained what all I know so that we can focus on the only problem:

  • Q: How can I get average color on buffered image? How can I get such average color on sub-rectangle of that image?

Speed wins here. In this exceptional case, I consider it more valuable than code readability.

Advertisement

Answer

I think that no matter what you do, you are going to have an O(wh) operation, where w is your width and h is your height.

Therefore, I’m going to post this (naive) solution to fulfil the first part of your question as I do not believe there is a faster solution.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement