Close

Connected Components - Python

A project log for PiMoCap

Turning an RPI into a MoCap Camera. Functional *and* Delicious...

bit-meddlerBit Meddler 07/04/2016 at 11:240 Comments

Motion Capture is generally just a series of compression problems, through using the Strobes and the reflective markers, we are compressing the 'description' of the subjects body pose from all the pixels their light may fall onto, to just the pixels seeing the markers, and compressing the 'description' down to just these markers (from this angle).

The image that hits the sensor of a mocap camera will be 95+% black, or below a certain threshold, the only bits we're interested in are the regions of light on the image. once we can hone-in the bright regions (the blobs), we're then only interested in their center, which could be computed from the x,y extents = (n-x)/2, (m-y)/2. (NOTE: x,y is the upper left of a bounding box, n,m are the bottom right. In screen space (0,0) is upper left of the screen (1920,1280) would be bottom right)

Anyway, how do we ID the regions of interest in the image? 'Connected components' (see wiki) offers a method to scan an image and tag connected regions. We're not interested in tagging the regions, just maintaining a bounding box of them, and maybe collecting other data. So the Algo presents itself:

regions = []
first_possible_region = 0
current_region = 0
for rows:
  current_region = first_possible_region
  while pixels in col
    traverse dark pixels till a line of bright pixels found
    if line found:
      merges = 0
      while current_region < len(regions):
        if line touches current_region:
          if merges == 0:
            merge line into region
            merges = 1
          else:
            merge current & last regions
            drop current region
            current_region -= 1
        else:
          // line ends before current_region
          make new region out of this line
          insert it into regions before current_region
          break
        current_region += 1
      if merges == 0:
        
  after scanning all pixels in the row, tidy up the region list
  move any regions not touching current row down the list
  preserve order of list (regions will be from x to n)
  move first_possible_region index so impossible regions are not tested
// whole image scanned now
for every region:
  compute center

return centers
Which I'll try to bang out in python and test with my bench-marking images / ground-truth data

Discussions