def reconstruct(self,img): """ This is a "just for fun" method as a sanity check for the BOF codeook. The method takes in an image, extracts each codebook code, and replaces the image at the position with the code. """ retVal = cv.CreateImage((img.width,img.height), cv.IPL_DEPTH_8U, 1) data = self._getPatches(img) p = spsd.cdist(data,self.mCodebook) foo = p.shape[0] codes = np.argmin(p,axis=1) count = 0 wsteps = img.width/self.mPatchSize[0] hsteps = img.height/self.mPatchSize[1] w=self.mPatchSize[0] h=self.mPatchSize[1] length = w*h retVal = Image(retVal) for widx in range(wsteps): for hidx in range(hsteps): x = (widx*self.mPatchSize[0]) y = (hidx*self.mPatchSize[1]) p = codes[count] temp = Image(self.mCodebook[p,:].reshape(self.mPatchSize[0],self.mPatchSize[1])) retVal.blit(temp,pos=(x,y)) count = count + 1 return retVal
def reconstruct(self, img): """ This is a "just for fun" method as a sanity check for the BOF codeook. The method takes in an image, extracts each codebook code, and replaces the image at the position with the code. """ retVal = cv.CreateImage((img.width, img.height), cv.IPL_DEPTH_8U, 1) data = self._getPatches(img) p = spsd.cdist(data, self.mCodebook) foo = p.shape[0] codes = np.argmin(p, axis=1) count = 0 wsteps = img.width / self.mPatchSize[0] hsteps = img.height / self.mPatchSize[1] w = self.mPatchSize[0] h = self.mPatchSize[1] length = w * h retVal = Image(retVal) for widx in range(wsteps): for hidx in range(hsteps): x = (widx * self.mPatchSize[0]) y = (hidx * self.mPatchSize[1]) p = codes[count] temp = Image(self.mCodebook[p, :].reshape( self.mPatchSize[0], self.mPatchSize[1])) retVal.blit(temp, pos=(x, y)) count = count + 1 return retVal
def _codebook2Img(self, cb, patchsize, count, patch_arrangement, spacersz): """ cb = the codebook patchsize = the patch size (ususally 11x11) count = total codes patch_arrangement = how are the patches grided in the image (eg 128 = (8x16) 256=(16x16) ) spacersz = the number of pixels between patches """ w = (patchsize[0]*patch_arrangement[0])+((patch_arrangement[0]+1)*spacersz) h = (patchsize[1]*patch_arrangement[1])+((patch_arrangement[1]+1)*spacersz) bm = np.zeros((h, w), np.uint8) img = Image(bm) count = 0 for widx in range(patch_arrangement[0]): for hidx in range(patch_arrangement[1]): x = (widx*patchsize[0])+((widx+1)*spacersz) y = (hidx*patchsize[1])+((hidx+1)*spacersz) temp = Image(cb[count,:].reshape(patchsize[0],patchsize[1])) img.blit(temp,pos=(x,y)) count = count + 1 return img