def _bovw_hog(clusters=None): if clusters is None: clusters = pickle.load(open('hog_8_2_clusters.pkl')) clusters = np.asfarray(clusters) hog = imfeat.HOGLatent(8, 2) return imfeat.BoVW(lambda x: hog.make_bow_mask(x, clusters), clusters.shape[0], 3)
def test_ainterface(self): """Simple test of the basic feature interface""" features = [ imfeat.ObjectBank(), imfeat.GIST(), imfeat.HOGLatent(2), imfeat.Autocorrelogram(), imfeat.GradientHistogram(), imfeat.Histogram('gray'), imfeat.RHOG(gray=False), imfeat.RHOG(gray=True), imfeat.Moments('rgb', 2), imfeat.Histogram('rgb'), imfeat.SpatialHistogram(mode='rgb', num_rows=2, num_cols=2), imfeat.TinyImage() ] feat_sz = {} for image_fn in glob.glob('test_images/*'): if image_fn in ['test_images/test3.gif']: continue for feat_num, feature in enumerate(features): prev_f = None for load_func in [ cv.LoadImage, cv.LoadImageM, cv2.imread, Image.open ]: f = feature( imfeat.resize_image(load_func(image_fn), 100, 100)) self.assertEqual(feat_sz.setdefault(feat_num, f.size), f.size) if prev_f is None: prev_f = f if load_func != Image.open: # Skip PIL as the jpeg loading produces different data np.testing.assert_equal(prev_f, f)
def test_save_lena(self): feat = imfeat.HOGLatent(8) image_input = imfeat.convert_image( cv2.imread('test_images/mosaic_001_01.jpg'), feat.MODES) #lena.jpg num_eq = 0 num_neq = 0 try: os.makedirs('out/latent/') except OSError: pass for x in range(260, 261): #image_input = imfeat.resize_image(image_input, x, x) for sz in range(1, 6): print(sz) sz = 2**sz num_blocks = (np.floor( np.asfarray(image_input.shape[:2]) / float(sz) + .5) - 2) print(num_blocks) if any(num_blocks <= 0): continue #effective_size = (np.floor(np.asfarray(image_input.shape[:2]) / float(sz) + .5)) * sz feat = imfeat.HOGLatent(sz) im = image_input.copy() out = feat.make_feature_mask(im) print('Dims[%d]' % out.shape[2]) for i in range(out.shape[2]): out_s = out[:, :, i] print(np.min(out_s)) print(np.max(out_s)) print('sz[%s]M[%s] m[%s]' % (sz, np.max(out_s), np.min(out_s))) out_s = np.array( 255 * (out_s - np.min(out_s)) / (np.max(out_s) - np.min(out_s) + .000000001), dtype=np.uint8) cv2.imwrite('out/latent/lena-hog-%.3d-%.3d.png' % (sz, i), out_s) y, x = np.random.randint(0, im.shape[0]), np.random.randint( 0, im.shape[1]) im[y, x, :] += 100 out2 = feat.make_feature_mask(im) if out[y, x, :].tolist() == out2[y, x, :].tolist(): num_eq += 1 print('-----------%s' % str((num_eq, num_neq))) else: num_neq += 1
def _feature_hog_loc(self, image): feature_mask = imfeat.HOGLatent(self.sbin).compute_dense_2d(image) features = [] norm = np.asfarray(feature_mask.shape[:2]) for y in range(feature_mask.shape[0]): for x in range(feature_mask.shape[1]): yx = np.array([y, x]) / norm * self.scale features.append(np.hstack([feature_mask[y, x, :], yx])) return np.asfarray(features)
def cluster_points_local(self, **kw): row_cols = hadoopy_hbase.scanner(self.hb, self.images_table, columns=[self.image_column], **kw) feature_func = imfeat.HOGLatent(16) num_clusters = 100 features = [] for row, columns in row_cols: image = imfeat.image_fromstring(columns[self.image_column]) features.append(feature_func.compute_dense(image)) features = np.vstack(features) clusters = sp.cluster.vq.kmeans(features, num_clusters)[0] print(clusters.shape) json.dump(clusters.tolist(), open('clusters.js', 'w'))
def test_aahog_latent(self): print('Hog Latent') feature = imfeat.HOGLatent(2) for feat_out, image in self._run_all_images(feature): print(feat_out) print(len(feat_out)) print('Hog Latent') image = cv2.imread('test_images/lena.ppm') out = feature(image) self.assertEqual(len(out), 254 * 254 * 31) load_from_umiacs('fixtures/lena_feat.pkl.gz', 'ab4580a8322e18b144c39867aeefa05b') with gzip.GzipFile('fixtures/lena_feat.pkl.gz') as fp: f = pickle.load(fp) np.testing.assert_almost_equal(out, f)
color_key = kontort.make_color_key(colors[:, ::-1], class_names) try: os.makedirs('%s/view' % out_root) except OSError: pass cv2.imwrite('%s/view/key.png' % out_root, color_key) def convert_color(image): image = imfeat.convert_image(image, [('opencv', 'lab', cv.IPL_DEPTH_8U)]) image = np.asarray(cv.GetMat(image)).copy() return image HOG = imfeat.HOGLatent(4) LBP = imfeat.LBP() GRAD = imfeat.GradientHistogram() def convert_labels_to_integrals(label_mask, num_vals): masks = [] print(label_mask.shape) for x in range(num_vals): m = np.asfarray(label_mask == x) m = cv2.integral(m) masks.append(m) return np.ascontiguousarray(np.swapaxes(np.swapaxes(np.array(masks), 0, 2), 0, 1))
def _cluster_bovw(self, images, points_per_image=100, num_clusters=256): hog = imfeat.HOGLatent(8, 2) clusters = np.asfarray( imfeat.BoVW.cluster(images, hog.compute_dense, num_clusters, points_per_image)) pickle.dump(clusters, open('hog_8_2_clusters.pkl', 'w'), -1)
def _meta_hog_gradient(): return imfeat.MetaFeature(imfeat.HOGLatent(), imfeat.GradientHistogram())
def _hog(): return imfeat.HOGLatent()
def __init__(self, clusters=None, levels=3, *args, **kw): super(HOGBoVW, self).__init__() self.hog = imfeat.HOGLatent(*args, **kw) self.levels = levels self.clusters = clusters
import imfeat import cv2 import numpy as np import os PATCH_SIZE = 40 CELLS = 8 SBIN = 4 CELL_SKIP = int(os.environ.get('CELL_SKIP', '8')) compute = imfeat.HOGLatent(sbin=SBIN, blocks=1) def compute_patch(image): if not (image.shape[0] == image.shape[1] == PATCH_SIZE): raise ValueError('Bad patch shape[%s]' % str(image.shape)) f = compute(image, ravel=False) return np.ascontiguousarray(f[1:-1, 1:-1, :].ravel()) def _image_patch_features_base(image, inner_func, scales=6, normalize_box=False): orig_height, orig_width = image.shape[:2] for scale in [2**x for x in range(scales)]: if scale > 1: height, width = np.array(image.shape[:2]) / 2 image = image[:height * 2, :width * 2, :] if min(width, height) < 1: return image = cv2.resize(image, (width, height))