This is not possible in the GD library, but with imagemagick.
Color changes and their location in images can be found through Edge Detection .
There is a good cover for php on this blog .
There is no general script I could give you, since you find the correct algorithm 4, the original image is an iterative process.
first you try to filter out the dark green colors of the tree from the original image, then you can run edge detection to get the edges of the circles in the grayscale image.
Once you are there, you need some kind of circle definition. google found this interesting thread
Hope this helps you in the right direction.
source share