def bow_train(featfunc, opts, canon_opts, params): descs = [] files = dataset.training_files(opts['num_train_images']) for img_file, depth_file in print_progress(files): img, segmask = canonize(img_file, depth_file, canon_opts, params) if featfunc == 'daisy': h = daisy(img_as_float(img), **opts['feature_opts']) if featfunc == 'jet': h = jet(img_as_float(img), **opts['feature_opts']) segmask = sp.misc.imresize(segmask, h.shape[:2]) for i in range(h.shape[0]): for j in range(h.shape[1]): if segmask[i,j] != 0: descs.append(h[i,j,:].flatten()) descs = [descs[i] for i in range(0, len(descs), opts['feature_step'])] descs = np.vstack(tuple(descs)) print '# K-means clustering of %i features of dimensionality %i'%(descs.shape[0], descs.shape[1]) kmeans = KMeans(opts['num_clusters'], n_jobs=options.num_threads) kmeans.fit(descs) return kmeans
def feature_extraction(img_file, depth_file, opts, params): img, segmask = canonize(img_file, depth_file, opts['canonization'], params) features = np.array([], dtype=float) if 'region_properties' in opts: opts_ = opts['region_properties'] if 'grid' in opts_: grid = opts_['grid'] segmask[segmask!=0] = 1 cellNum = 0 cellHeight = segmask.shape[0]/grid[0] cellWidth = segmask.shape[1]/grid[1] for i in range(grid[0]): for j in range(grid[1]): cellNum += 1 t = cellHeight*i b = cellHeight*(i+1) l = cellWidth*j r = cellWidth*(j+1) segmask[t:b, l:r] = segmask[t:b, l:r] * cellNum props = regionprops(segmask.astype(int), properties=opts_['properties'], intensity_image=img) featureList = [] for i in range(len(props)): for p in opts_['properties']: featureList.append(np.array(props[i][p]).flatten()) features = np.append(features, np.hstack(tuple(featureList))) if 'raw_pixels' in opts: img_ = sp.misc.imresize(img, opts['raw_pixels']['img_scale']) features = np.append(features, img_.flatten()) if 'daisy_bow' in opts: f = bow(img, segmask, daisy, opts['daisy_bow'], params['daisy_bow_kmeans']) features = np.append(features, f) if 'jet_bow' in opts: f = bow(img, segmask, jet, opts['jet_bow'], params['jet_bow_kmeans']) features = np.append(features, f) return features
def output_img(filename, outdir, output_filename, params): img_file, depth_file = filename img, _ = canonize(img_file, depth_file, options.canonization, params) sp.misc.imsave(os.path.join(outdir, output_filename), img)