Reputation: 1437
Let's say I have a JPG image that's completely white except for a green filled in square that's placed randomly. The square has equal side lengths of between 20 and 30 pixels.
I'd like to write a Java program to find the exact center of this square. I know there must be a way to do this extremely fast. Any ideas?
Upvotes: 0
Views: 45
Reputation: 1437
Thanks to everyone who helped. Your comments were useful.
I found that the most efficient way to do this was to just use a quasi-brute force method. I walk the pixel grid with a step of 30 pixels in both x and y direction. The images I'm searching ar around 2k X 2k, so this is pretty quick. Once I find one pixel, I just create a neighborhood around it that's 30 pixes in each direction. This way I can find exactly where the square is.
Two more benefits. I did find that many pixels were almost the correct color due to roundoff error from the producer. I was also able to see that the squares were exactly 30 pixels on each side. If they aren't, I know there's a problem with the process that producces it.
In actual practice, this code is far faster than I would have imagined - no need to try and optimize.
Upvotes: 2