def extract(self, impath): """ Extract a ScSPM descriptor for an image. This method will return an ScSPM descriptor for an image. learn_dictionary() needs to be run (once) before this method can be used. Arguments: impath: str, the path to an image Returns: a ScSPM descriptor (array) for the image. This array either has self.dsize*sum(self.levels**2) elements, or self.compress_dim if not None. """ if self.dic is None: raise ValueError('No dictionary has been learned!') # Get and resize image img = pch.imread_resize(impath, self.maxdim) # Extract SIFT patches patches, cx, cy = sw.DSIFT_patches(img, self.psize, self.pstride) # Get OMP codes scpatch = np.transpose( omp(np.asfortranarray(patches.T, np.float64), self.dic, self.active, eps=np.spacing(1), numThreads=1).todense()) # Pyramid pooling and normalisation fea = pch.pyramid_pooling(scpatch, cx, cy, img.shape, self.levels) fea = fea / math.sqrt((fea**2).sum() + 1e-10) if self.compress_dim is not None: return np.dot(fea, self.rmat) else: return fea
def extract (self, impath): """ Extract a ScSPM descriptor for an image. This method will return an ScSPM descriptor for an image. learn_dictionary() needs to be run (once) before this method can be used. Arguments: impath: str, the path to an image Returns: a ScSPM descriptor (array) for the image. This array either has self.dsize*sum(self.levels**2) elements, or self.compress_dim if not None. """ if self.dic is None: raise ValueError('No dictionary has been learned!') # Get and resize image img = pch.imread_resize(impath, self.maxdim) # Extract SIFT patches patches, cx, cy = sw.DSIFT_patches(img, self.psize, self.pstride) # Get OMP codes scpatch = np.transpose(omp(np.asfortranarray(patches.T, np.float64), self.dic, self.active, eps=np.spacing(1), numThreads=1).todense()) # Pyramid pooling and normalisation fea = pch.pyramid_pooling(scpatch, cx, cy, img.shape, self.levels) fea = fea / math.sqrt((fea**2).sum() + 1e-10) if self.compress_dim is not None: return np.dot(fea, self.rmat) else: return fea
def test_pyramid_pooling (self): """ Test the imgpatches.pyramid_pooling function. """ pyr = patch.pyramid_pooling(self.tpatch, self.tx, self.ty, self.timg.shape, (1,2), patch.p_max) self.assertTrue((self.tpyr == pyr).all())