import Image import numpy import pylab import vlfeat from vlfeat.plotop.vl_plotframe import vl_plotframe if __name__ == "__main__": """ VL_DEMO_SIFT_EDGE Demo: SIFT: edge treshold """ I = numpy.zeros([100, 500]) for i in 10 * (1 + numpy.arange(9)): d = numpy.round(i / 3.0) I[50 - d - 1 : 50 + d - 1, i * 5 - 1] = 1 I = numpy.array(I, "f", order="F") I = 2 * numpy.pi * 8 ** 2 * vlfeat.vl_imsmooth(I, 8) I = 255 * I I = numpy.array(I, "f", order="F") print "sift_edge_0" ter = [3.5, 5, 7.5, 10] for te in ter: f, d = vlfeat.vl_sift(I, peak_thresh=0.0, edge_thresh=te) pylab.figure() pylab.gray() pylab.imshow(I) vl_plotframe(f, color="k", linewidth=3) vl_plotframe(f, color="y", linewidth=2)
def vl_phow(im, verbose=True, fast=True, sizes=[4, 6, 8, 10], step=2, color='rgb', floatdescriptors=False, magnif=6, windowsize=1.5, contrastthreshold=0.005): opts = Options(verbose, fast, sizes, step, color, floatdescriptors, magnif, windowsize, contrastthreshold) dsiftOpts = DSiftOptions(opts) # make sure image is float, otherwise segfault im = array(im, 'float32') # Extract the features imageSize = shape(im) if im.ndim == 3: if imageSize[2] != 3: # "IndexError: tuple index out of range" if both if's are checked at the same time raise ValueError("Image data in unknown format/shape") if opts.color == 'gray': numChannels = 1 if (im.ndim == 2): im = vl_rgb2gray(im) else: numChannels = 3 if (im.ndim == 2): im = dstack([im, im, im]) if opts.color == 'rgb': pass elif opts.color == 'opponent': # from https://github.com/vlfeat/vlfeat/blob/master/toolbox/sift/vl_phow.m # Note that the mean differs from the standard definition of opponent # space and is the regular intesity (for compatibility with # the contrast thresholding). # Note also that the mean is added pack to the other two # components with a small multipliers for monochromatic # regions. mu = 0.3 * im[:, :, 0] + 0.59 * im[:, :, 1] + 0.11 * im[:, :, 2] alpha = 0.01 im = dstack([ mu, (im[:, :, 0] - im[:, :, 1]) / sqrt(2) + alpha * mu, (im[:, :, 0] + im[:, :, 1] - 2 * im[:, :, 2]) / sqrt(6) + alpha * mu ]) else: raise ValueError('Color option ' + str(opts.color) + ' not recognized') if opts.verbose: print('{0}: color space: {1}'.format('vl_phow', opts.color)) print('{0}: image size: {1} x {2}'.format('vl_phow', imageSize[0], imageSize[1])) print('{0}: sizes: [{1}]'.format('vl_phow', opts.sizes)) frames_all = [] descrs_all = [] for size_of_spatial_bins in opts.sizes: # from https://github.com/vlfeat/vlfeat/blob/master/toolbox/sift/vl_phow.m # Recall from VL_DSIFT() that the first descriptor for scale SIZE has # center located at XC = XMIN + 3/2 SIZE (the Y coordinate is # similar). It is convenient to align the descriptors at different # scales so that they have the same geometric centers. For the # maximum size we pick XMIN = 1 and we get centers starting from # XC = 1 + 3/2 MAX(OPTS.SIZES). For any other scale we pick XMIN so # that XMIN + 3/2 SIZE = 1 + 3/2 MAX(OPTS.SIZES). # In pracrice, the offset must be integer ('bounds'), so the # alignment works properly only if all OPTS.SZES are even or odd. off = floor(3.0 / 2 * (max(opts.sizes) - size_of_spatial_bins)) + 1 # smooth the image to the appropriate scale based on the size # of the SIFT bins sigma = size_of_spatial_bins / float(opts.magnif) ims = vl_imsmooth(im, sigma) # extract dense SIFT features from all channels frames = [] descrs = [] for k in range(numChannels): size_of_spatial_bins = int(size_of_spatial_bins) # vl_dsift does not accept numpy.int64 or similar f_temp, d_temp = vl_dsift(data=ims[:, :, k], step=dsiftOpts.step, size=size_of_spatial_bins, fast=dsiftOpts.fast, verbose=dsiftOpts.verbose, norm=dsiftOpts.norm, bounds=[off, off, maxint, maxint]) frames.append(f_temp) descrs.append(d_temp) frames = array(frames) descrs = array(descrs) d_new_shape = [descrs.shape[0] * descrs.shape[1], descrs.shape[2]] descrs = descrs.reshape(d_new_shape) # remove low contrast descriptors # note that for color descriptors the V component is # thresholded if (opts.color == 'gray') | (opts.color == 'opponent'): contrast = frames[0][2, :] elif opts.color == 'rgb': contrast = mean( [frames[0][2, :], frames[1][2, :], frames[2][2, :]], 0) else: raise ValueError('Color option ' + str(opts.color) + ' not recognized') descrs[:, contrast < opts.contrastthreshold] = 0 # save only x,y, and the scale frames_temp = array(frames[0][0:3, :]) padding = array(size_of_spatial_bins * ones(frames[0][0].shape)) frames_all.append(vstack([frames_temp, padding])) descrs_all.append(array(descrs)) frames_all = hstack(frames_all) descrs_all = hstack(descrs_all) return frames_all, descrs_all
if __name__ == '__main__': """ VL_DEMO_SIFT_PEAK Demo: SIFT: peak treshold """ tmp = uniform(0, 1, (100, 500)) I = numpy.zeros([100, 500]) I.ravel()[pylab.find(tmp.ravel()<=0.005)] = 1 I = (numpy.ones([100,1]) * numpy.r_[0:1:500j]) * I I[:, 0] = 0 I[:, -1] = 0 I[0, :] = 0 I[-1, :] = 0 I = numpy.array(I, 'f', order='F') I = 2 * numpy.pi * 4**2 * vlfeat.vl_imsmooth(I, 4) I = 255 * I print 'sift_peak_0' I = numpy.array(I, 'f', order='F') tpr = [0, 10, 20, 30] for tp in tpr: f, d = vlfeat.vl_sift(I, peak_thresh=tp, edge_thresh=10000) pylab.figure() pylab.gray() pylab.imshow(I) vl_plotframe(f, color='k', linewidth=3) vl_plotframe(f, color='y', linewidth=2)
def dsift(im, verbose=True, fast=True, sizes=[4, 6, 8, 10], step=2, color='rgb', floatdescriptors=False, magnif=6, windowsize=1.5, contrastthreshold=0.005): opts = Options(verbose, fast, sizes, step, color, floatdescriptors, magnif, windowsize, contrastthreshold) dsiftOpts = DSiftOptions(opts) # make sure image is float, otherwise segfault im = array(im, 'float32') # Extract the features imageSize = shape(im) if im.ndim == 3: if imageSize[2] != 3: # "IndexError: tuple index out of range" if both if's are checked at the same time raise ValueError("Image data in unknown format/shape") if opts.color == 'gray': numChannels = 1 if (im.ndim == 2): im = vl_rgb2gray(im) else: numChannels = 3 if (im.ndim == 2): im = dstack([im, im, im]) if opts.color == 'rgb': pass elif opts.color == 'opponent': # from https://github.com/vlfeat/vlfeat/blob/master/toolbox/sift/vl_phow.m # Note that the mean differs from the standard definition of opponent # space and is the regular intesity (for compatibility with # the contrast thresholding). # Note also that the mean is added pack to the other two # components with a small multipliers for monochromatic # regions. mu = 0.3 * im[:, :, 0] + 0.59 * im[:, :, 1] + 0.11 * im[:, :, 2] alpha = 0.01 im = dstack([mu, (im[:, :, 0] - im[:, :, 1]) / sqrt(2) + alpha * mu, (im[:, :, 0] + im[:, :, 1] - 2 * im[:, :, 2]) / sqrt(6) + alpha * mu]) else: raise ValueError('Color option ' + str(opts.color) + ' not recognized') if opts.verbose: great='great' #print('{0}: color space: {1}'.format('vl_phow', opts.color)) #print('{0}: image size: {1} x {2}'.format('vl_phow', imageSize[0], imageSize[1])) #print('{0}: sizes: [{1}]'.format('vl_phow', opts.sizes)) frames_all = [] descrs_all = [] for size_of_spatial_bins in opts.sizes: # from https://github.com/vlfeat/vlfeat/blob/master/toolbox/sift/vl_phow.m # Recall from VL_DSIFT() that the first descriptor for scale SIZE has # center located at XC = XMIN + 3/2 SIZE (the Y coordinate is # similar). It is convenient to align the descriptors at different # scales so that they have the same geometric centers. For the # maximum size we pick XMIN = 1 and we get centers starting from # XC = 1 + 3/2 MAX(OPTS.SIZES). For any other scale we pick XMIN so # that XMIN + 3/2 SIZE = 1 + 3/2 MAX(OPTS.SIZES). # In pracrice, the offset must be integer ('bounds'), so the # alignment works properly only if all OPTS.SZES are even or odd. off = floor(3.0 / 2 * (max(opts.sizes) - size_of_spatial_bins)) + 1 # smooth the image to the appropriate scale based on the size # of the SIFT bins sigma = size_of_spatial_bins / float(opts.magnif) ims = vl_imsmooth(im, sigma) # extract dense SIFT features from all channels frames = [] descrs = [] for k in range(numChannels): size_of_spatial_bins = int(size_of_spatial_bins) # vl_dsift does not accept numpy.int64 or similar f_temp, d_temp = vl_dsift(data=ims[:, :, k], step=dsiftOpts.step, size=size_of_spatial_bins, fast=dsiftOpts.fast, verbose=dsiftOpts.verbose, norm=dsiftOpts.norm, bounds=[off, off, maxint, maxint]) frames.append(f_temp) descrs.append(d_temp) frames = array(frames) descrs = array(descrs) d_new_shape = [descrs.shape[0] * descrs.shape[1], descrs.shape[2]] descrs = descrs.reshape(d_new_shape) # remove low contrast descriptors # note that for color descriptors the V component is # thresholded if (opts.color == 'gray') | (opts.color == 'opponent'): contrast = frames[0][2, :] elif opts.color == 'rgb': contrast = mean([frames[0][2, :], frames[1][2, :], frames[2][2, :]], 0) else: raise ValueError('Color option ' + str(opts.color) + ' not recognized') descrs[:, contrast < opts.contrastthreshold] = 0 # save only x,y, and the scale frames_temp = array(frames[0][0:3, :]) padding = array(size_of_spatial_bins * ones(frames[0][0].shape)) frames_all.append(vstack([frames_temp, padding])) descrs_all.append(array(descrs)) frames_all = hstack(frames_all) descrs_all = hstack(descrs_all) return frames_all, descrs_all
def test_all(self): img = numpy.array(Image.open('roofs1.jpg')) # Test rgb2gray img_gray = rgb2gray(img) self.assertEqual(tuple(img_gray.shape), (478, 640)) self.assertTrue( numpy.allclose(img_gray[:4, :4], numpy.array([[0.8973, 0.8973, 0.8973, 0.9052], [0.8973, 0.8973, 0.8973, 0.9052], [0.8973, 0.8973, 0.9021, 0.9061], [0.9013, 0.9013, 0.9061, 0.9100]]), atol=1e-4)) if os.path.exists("img_gray.txt"): expected = numpy.loadtxt("img_gray.txt", delimiter='\t') self.assertTrue(numpy.allclose(img_gray, expected, atol=1e-4)) # Test vl_imsmooth binsize = 8 magnif = 3 img_smooth = vlfeat.vl_imsmooth(img_gray, math.sqrt((binsize / magnif)**2 - 0.25), verbose=True) self.assertEqual(tuple(img_smooth.shape), (478, 640)) self.assertTrue( numpy.allclose(img_smooth[:4, :4], numpy.array([[0.8998, 0.9013, 0.9034, 0.9057], [0.9000, 0.9015, 0.9035, 0.9057], [0.9002, 0.9017, 0.9036, 0.9057], [0.9005, 0.9020, 0.9038, 0.9058]]), atol=1e-4)) if os.path.exists("img_smooth.txt"): expected = numpy.loadtxt("img_smooth.txt", delimiter='\t') self.assertTrue(numpy.allclose(img_smooth, expected, atol=1e-4)) # Test vl_dsift frames, descrs = vlfeat.vl_dsift(img_smooth, size=binsize, verbose=True) frames = numpy.add(frames.transpose(), 1) descrs = descrs.transpose() self.assertEqual(tuple(frames.shape), (2, 279664)) self.assertEqual(tuple(descrs.shape), (128, 279664)) self.assertTrue( numpy.allclose(frames[:, :4], numpy.array([[13, 13, 13, 13], [13, 14, 15, 16]]))) self.assertTrue( numpy.allclose( descrs[:, 0], numpy.array([ 134, 35, 0, 0, 0, 0, 0, 5, 109, 9, 1, 0, 0, 0, 0, 61, 7, 2, 32, 21, 0, 0, 1, 28, 2, 13, 111, 9, 0, 0, 0, 2, 33, 134, 131, 0, 0, 0, 0, 0, 30, 92, 134, 0, 0, 0, 0, 19, 11, 42, 134, 0, 0, 0, 1, 31, 6, 20, 124, 3, 0, 0, 0, 7, 5, 134, 134, 0, 0, 0, 0, 0, 1, 94, 134, 0, 0, 0, 0, 0, 0, 34, 134, 1, 0, 0, 0, 0, 0, 4, 134, 13, 0, 2, 2, 0, 27, 53, 15, 0, 0, 0, 0, 1, 11, 48, 27, 2, 0, 0, 0, 0, 0, 5, 28, 16, 1, 0, 0, 0, 0, 2, 13, 16, 4, 5, 4, 0 ]))) if os.path.exists("dsift_frames.txt"): expected = numpy.loadtxt("dsift_frames.txt", delimiter='\t') self.assertTrue(numpy.allclose(frames, expected)) if os.path.exists("dsift_descrs.txt"): expected = numpy.loadtxt("dsift_descrs.txt", delimiter='\t') self.assertTrue( numpy.linalg.norm(expected - descrs) < 28) # rounding errors? # Test vl_kmeans centers, assigns = vlfeat.vl_kmeans(numpy.array( [[1], [2], [3], [10], [11], [12]], dtype='f'), 2, ret_quantize=True, verbose=True) self.assertTrue(numpy.allclose(centers, numpy.array([[2], [11]]))) self.assertTrue( numpy.allclose(assigns, numpy.array([0, 0, 0, 1, 1, 1]))) centers, assigns = vlfeat.vl_kmeans(numpy.array( [[1, 0], [2, 0], [3, 0], [10, 1], [11, 1], [12, 1]], dtype='f'), 2, ret_quantize=True) self.assertTrue(numpy.allclose(centers, numpy.array([[11, 1], [2, 0]]))) # order swapped? self.assertTrue( numpy.allclose(assigns, numpy.array([1, 1, 1, 0, 0, 0]))) # Test vl_gmm if os.path.exists("gmm_data.txt"): data = numpy.loadtxt("gmm_data.txt", delimiter='\t').transpose() else: data = numpy.random.rand(5000, 2) means, covariances, priors, ll, posteriors = vlfeat.vl_gmm( data, 30, verbose=True, ret_loglikelihood=True, ret_posterior=True) self.assertEqual(tuple(means.shape), (30, 2)) self.assertEqual(tuple(covariances.shape), (30, 2)) self.assertEqual(tuple(priors.shape), (30, )) self.assertEqual(tuple(posteriors.shape), (5000, 30)) if os.path.exists("gmm_means.txt"): expected = numpy.loadtxt("gmm_means.txt", delimiter='\t').transpose() self.assertTrue(numpy.allclose(means, expected, atol=1e-4)) if os.path.exists("gmm_covariances.txt"): expected = numpy.loadtxt("gmm_covariances.txt", delimiter='\t').transpose() self.assertTrue(numpy.allclose(covariances, expected, atol=1e-4)) if os.path.exists("gmm_priors.txt"): expected = numpy.loadtxt("gmm_priors.txt", delimiter='\t').transpose() self.assertTrue(numpy.allclose(priors, expected, atol=1e-4)) if os.path.exists("gmm_posteriors.txt"): expected = numpy.loadtxt("gmm_posteriors.txt", delimiter='\t').transpose() self.assertTrue(numpy.allclose(posteriors, expected, atol=1e-3)) # Test vl_fisher if os.path.exists("fisher_data.txt"): data = numpy.loadtxt("fisher_data.txt", delimiter='\t').transpose() else: data = numpy.random.rand(1000, 2) encoding = vlfeat.vl_fisher(data, means, covariances, priors, verbose=True) self.assertEqual(tuple(encoding.shape), (120, )) if os.path.exists("fisher_encoding.txt"): expected = numpy.loadtxt("fisher_encoding.txt", delimiter='\t').transpose() self.assertTrue(numpy.allclose(encoding, expected, atol=1e-4))