Luke Taylor
Luke Taylor

Reputation: 9571

Lists of (x, y) coordinates into multiple lists of connected pixels

I have a Python script that uses PIL and the HSV color space to detect all the red objects in an image. in action

It can output a list of red pixels in the image that looks like [(x,y),(x,y),(x,y), etc.]

Using this list, I can find the center of all red pixels with [sum(list(x))/len(list(x)) for x in zip(*list)]

What I'd like to do is find the respective centers of all red objects (Both the center of the trash can AND the center of the folder). To do this, I want an efficient way to divide the list into multiple lists, one for each solid object. Then, I can take these lists and exclude the ones with fewer than 20 pixels to account for disconnected specks of color.

How can I best separate lists of (x, y) coordinates into multiple lists of connected pixels?

NOTE: I do not have scipy, OpenCV, or scikit-image available. I have PIL and Numpy

Upvotes: 1

Views: 166

Answers (1)

Luke Taylor
Luke Taylor

Reputation: 9571

I ended up just using breadth-first search to flood-fill the area. In this case, depth-first search would've worked just as well, though.

Upvotes: 1

Related Questions