oakmachine / projects / Quandry

September 2015

I'm messing with scikit-image and trying to solve jigsaw puzzles.

The current pipeline (but see the code on github for a more up-to-date picture):

  1. find an elevation map with filters.sobel
  2. generate thresholded markers
  3. segment the image with morphology.watershed
  4. "close" the image with scipy.ndimage.binary_fill_holes
  5. detect the edges of the segmented image via the "marching squares" algorithm (measure.find_contours)
  6. estimate the piece's centroid
  7. detect 90 degree angles along the shape's outline by selecting slices of the outline, composing two 90 deg angles above and below the slice and running computing Hausdorff distances for the segment and the angles -- slices that actually are corners will have a low Hausdorff score
  8. identify the "true corners" of each piece by composing reasonably shaped rectangles out of four corner candidate points and then computing the area of the resulting rectangle -- largest area wins
  9. arrange the corners in clockwise rotation with the first in the upper left
  10. arrange the sides in N-E-S-W fashion
  11. compute the distances between the true corners along the path defined by each side
  12. classify each side as 'in,' 'out' or 'flat'

pipeline output

The matching sides of two puzzle pieces should have the same length along the path. Matching sides should also have low Hausdorff and Frechet scores. Color-matching and other more advanced techniques could be applied later for more filtering.. I'm learning a lot just getting the side lengths.

I tried various corner-detecting routines provided by skimage: fast, foerstner, kitchen_rosenfeld, shi_tomasi and subpix, but I found harris worked the best while generating the fewest false positives. I later moved away from the harris technique since I already had the piece's outline as an ordered sequence. I found "template matching" to be more robust -- my templates are two 90deg angles that are composed around a test segment. I then determine the similarity between the segment and each angle. In the example below, the segment (in red) is not very similar to either angle and so it's Hausdorff score would be high:

corner template matching

I also found that the marching squares routine was significantly more robust when the sobel/watershed techniques were applied first to generate a segmented image. This region-based segmentation technique came from this skimage article

Technical References

I've built a few small imaging rigs to take photographs with consistent lighting and camera positioning. I'm using a beaglebone black and an adafruit JPEG TTL camera with my own LED right ring. I need to try taking photos on pieces of paper that have sharper contrast with the pieces -- some of the white colors in the pieces get lost in the edge detection step.

I shelved this one -- it was super tough! I revisited it in 2024 after seeing a much more successful puzzle-solving robot: July 2024 notes.