DARPA Shredder Challenge - Part Deux
2011-Nov-3 22:28 As promised, here is a bit of code that follows my preliminary line of thinking on Puzzle1:
from SimpleCV import *
from optparse import OptionParser
import random
cmdOptParser = OptionParser()
cmdOptParser.add_option("-i","--input",dest="inputFilename",help="Filename of the image to operate on")
cmdOptParser.add_option("-o","--output",dest="outputFilename",help="Filename of the image to save on")
(options,args) = cmdOptParser.parse_args()
puzzleImg = Image(options.inputFilename)
# The source images for the challenges are flippin huge. So let's up the recursion limit to support the search for many blobs
sys.setrecursionlimit(10000)
# Kick things off by creating a binarized() version of the source image
binarizedSrc = puzzleImg.binarize()
# Now lets find the blobs. Lets set some mins to make sure we don't worry about the itty bitty scraps
binBlobs = binarizedSrc.findBlobs(-1,2000)
# We have a set of identified blobs, so we can do some basic analysis. Let's just stupidly try to match up blobs. This won't really work for us because we are not trying to find duplicates etc, we need matching tiles. But anyway.
blobBestMatches = {}
for i,blob in enumerate(binBlobs):
blobMatchSet = {}
for c,blobCmp in enumerate(binBlobs,1):
blobMatch = blob.match(blobCmp)
if (blobMatch > 0): #blobMatch at 0 means it is itself
blobMatchSet[c] = blobMatch
blobBestMatches[i] = min(blobMatchSet,key=blobMatchSet.get) #this clever little bit identifies the index of the dict that contains the lowest possible match value.
for k,v in blobBestMatches.iteritems():
randomColor = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
print randomColor
binBlobs[k].draw(randomColor,-1,-1,binarizedSrc.dl())
binBlobs[v].draw(randomColor,-1,-1,binarizedSrc.dl())
binarizedSrc.save(options.outputFilename)
Forgive me if the code formatting creates issues for you. The script is dirt simple, and should be easy to follow. It is only a proof of concept that (usefully) gets as far as identifying the distinct blobs. From there, I've asked it to perform a simple match and identify the pairs by coloring each of the matches the same. Running the script produces something similar to this:
Doesn't that look a lot easier now?
This last bit is not at all useful in solving the puzzle because we are not attempting to identify duplicates in the image. Rather we need to match each shred to a "fit match". I have not yet figured out the math to express the "fit match", but at least I'm a step closer in this marathon.


Reader Comments