Given a png image with an unknown number of green colored (rgba=0, 65535, 0, 65535) regions

1- How can I find and put the border points of only the separate green boxes to an array like:

[0] = []point{...} // the first box's points
[1] = []point{...} // the second box's points
[...] = []point{...} // 
[7] = []point{...} // the last box's points

2- Which algorithm(s) should I use?

I tried canny edge detector and my own custom ones (like flood-fill) but the canny algorithm finds all the regions not only the green ones.

  • $\begingroup$ Are these guaranteed to be the exact color you've shown above, or might they vary a bit from region to region? $\endgroup$ Nov 7, 2017 at 3:21
  • $\begingroup$ @user1118321 Let’s assume they’re exact for now. $\endgroup$ Nov 7, 2017 at 8:53

1 Answer 1


Here is a brute force algorithm. This requires some temporary storage (another image, an array, etc.) to record the visited pixels.

  1. Start at the first pixel and continue iterating over the image until you find a green pixel. Mark each read pixel as "visited".
  2. Extract the set of green neighbor pixels and record them. Also tag them as "visited". The idea is to find all the green pixels connected to the one found in [1].
  3. If any pixels in the source image are left, go back to [1].

This will give you a set of contiguous green pixel regions where each pixel appears 1 or 0 times.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.