def featureExtractOneFile(loc,clusterData,doRandom=True,randomPixno=1000): size=(128,128) im=(resize(rgb2grey(imread(loc)),size)*255) #resize rp=extractPatch(im) print "patch extraction done" #for each pixel that has been converted into a 36 pixel patch, we are first going to find out the nearest codebook patch. Then, #we will create an one hot vector. Finally we will sum up the vectors belonging to the same quadrant and concatenate them. patchDict={} quadrant=0 #this is too time consuming, we are choosing 5000 pixels randomly #randomPixelIndices=sorted(range((128-(PS/2))*(128-(PS/2))), key=os.urandom)[0:5000] #print len(randomPixelIndices) randomPixelIndices=range((128-(PS/2))*(128-(PS/2))) if doRandom: random.shuffle(randomPixelIndices) print "shuffling done" else: randomPixno=len(randomPixelIndices) #for i,patch in enumerate(rp): for i in randomPixelIndices[:randomPixno]: patch=rp[i] divFactor=128-(PS/2) x=i/divFactor y=i%divFactor if x<65 and y<65: quadrant=1 elif x>65 and y<65: quadrant=2 elif x>65 and y>65: quadrant=3 else: quadrant=4 if patch.shape[0]*patch.shape[1]==36: patch=standardizePatch(patch) if quadrant not in patchDict: patchDict[quadrant]=nearestOneHot(patch,clusterData) else: patchDict[quadrant]=np.sum((patchDict[quadrant],nearestOneHot(patch,clusterData)),axis=0) #pprint(patchDict) return np.hstack((patchDict[1],patchDict[2],patchDict[3],patchDict[4]))
def extractPatch1(img): return filter(lambda y: y.shape[0]*y.shape[1]==PS*PS, [standardizePatch(img[x/64:x/64+PS,x%64:x%64+PS]) for x in range(64*64) \ if ndimage.variance(img[x/64:x/64+PS,x%64:x%64+PS]) > VR*ndimage.variance(img)])