def process_image(imagename, resultname='temp.sift', dense=False): """ process an image and save the results in a .key ascii file""" print "working on ", imagename if dense == False: if imagename[-3:] != 'pgm': #create a pgm file, image is resized, if it is too big. # sift returns an error if more than 8000 features are found size = (MAXSIZE, MAXSIZE) im = Image.open(imagename).convert('L') im.thumbnail(size, Image.ANTIALIAS) im.save('tmp.pgm') imagename = 'tmp.pgm' #check if linux or windows if os.name == "posix": cmmd = "./sift <" + imagename + ">" + resultname print cmmd else: cmmd = "siftWin32 <" + imagename + ">" + resultname os.system(cmmd) if os.path.getsize(resultname) == 0: raise IOError("extracting SIFT features failed " + resultname) else: import vlfeat # defines how dense the grid is size = (150, 150) step = 10 im = Image.open(imagename).resize(size, Image.ANTIALIAS) im_array = numpy.asarray(im) if im_array.ndim == 3: im_gray = vlfeat.vl_rgb2gray(im_array) elif im_array.ndim == 2: im_gray = im_array else: raise IOError("Not enough dims found in image " + resultname) locs, int_descriptors = vlfeat.vl_dsift(im_gray, step=step, verbose=VERBOSE) nfeatures = int_descriptors.shape[1] padding = numpy.zeros((2, nfeatures)) locs = numpy.vstack((locs, padding)) header = ' '.join([str(nfeatures), str(128)]) temp = int_descriptors.astype('float') # convert descriptors to float descriptors = temp[:] with open(resultname, 'wb') as f: cPickle.dump([locs.T, descriptors.T], f, protocol=cPickle.HIGHEST_PROTOCOL) print "features saved in", resultname if WRITE_VERBOSE: with open(resultname, 'w') as f: f.write(header) f.write("\n") for i in range(nfeatures): f.write(' '.join(map(str, locs[:, i]))) f.write("\n") f.write(' '.join(map(str, descriptors[:, i]))) f.write("\n")
def extract_feature_vector(self): img = imread(self.image.get_path()) if self.memory == False else self.image imgResized = resize(img, (300,250)) grayScaleImg = vl_rgb2gray(imgResized).astype('uint8') histEqualizedImage = equalizeHist(grayScaleImg) sizeOfSpatialBins = 8 step = 10 fast = False #if set to True it uses a flat window rather than a Gaussian window verbose = False norm = False bounds = -1 frames ,descriptors = vl_dsift(histEqualizedImage,step,bounds,sizeOfSpatialBins,fast,verbose,norm) return descriptors.transpose().astype('float32')
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
def process_image(imagename, resultname='temp.sift', dense=False): """ process an image and save the results in a .key ascii file""" print "working on ", imagename if dense == False: if imagename[-3:] != 'pgm': #create a pgm file, image is resized, if it is too big. # sift returns an error if more than 8000 features are found size = (MAXSIZE, MAXSIZE) im = Image.open(imagename).convert('L') im.thumbnail(size, Image.ANTIALIAS) im.save('tmp.pgm') imagename = 'tmp.pgm' #check if linux or windows if os.name == "posix": cmmd = "./sift <" + imagename + ">" + resultname print cmmd else: cmmd = "siftWin32 <" + imagename + ">" + resultname os.system(cmmd) if os.path.getsize(resultname) == 0: raise IOError("extracting SIFT features failed " + resultname) #print 'processed', imagename else: import vlfeat # like in pinto2008 why is vision hard size = (150, 150) window_size = 8 step = 10 im = Image.open(imagename).resize(size, Image.ANTIALIAS) im_array = numpy.asarray(im) if im_array.ndim == 3: im_gray = vlfeat.vl_rgb2gray(im_array) elif im_array.ndim == 2: im_gray = im_array else: raise IOError("Not enough dims found in image " + resultname) #locs,int_descriptors = vlfeat.vl_dsift(im_gray,size=window_size,verbose=VERBOSE) locs, int_descriptors = vlfeat.vl_dsift(im_gray, step=step, verbose=VERBOSE) nfeatures = int_descriptors.shape[1] padding = numpy.zeros((2, nfeatures)) locs = numpy.vstack((locs, padding)) header = ' '.join([str(nfeatures), str(128)]) temp = int_descriptors.astype('float') # convert descriptors to float descriptors = temp[:] with open(resultname, 'wb') as f: cPickle.dump([locs.T, descriptors.T], f, protocol=cPickle.HIGHEST_PROTOCOL) print "features saved in", resultname if WRITE_VERBOSE: with open(resultname, 'w') as f: f.write(header) f.write("\n") for i in range(nfeatures): f.write(' '.join(map(str, locs[:, i]))) f.write("\n") f.write(' '.join(map(str, descriptors[:, i]))) f.write("\n")
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
import Image import numpy import pylab import vlfeat from vlfeat.plotop.vl_plotframe import vl_plotframe from numpy.random import shuffle if __name__ == '__main__': # VL_DEMO_SIFT_BASIC Demo: SIFT: basic functionality # -------------------------------------------------------------------- # Load a figure and convert the to required format # -------------------------------------------------------------------- I = Image.open('../../../data/a.jpg') I = vlfeat.vl_rgb2gray(numpy.array(I)) I = numpy.array(I, 'f', order='F') # 'F' = column-major order! pylab.gray() pylab.imshow(I) print 'sift_basic_1' # -------------------------------------------------------------------- # Run SIFT # -------------------------------------------------------------------- f, d = vlfeat.vl_sift(I) sel = numpy.arange(f.shape[1]) shuffle(sel) vl_plotframe(f[:, sel[:50]], color='k', linewidth=3) vl_plotframe(f[:, sel[:50]], color='y')
def process_image(imagename, resultname='temp.sift', dense=False): """ process an image and save the results in a .key ascii file""" #print "working on ", imagename if dense == False: if imagename[-3:] != 'pgm': #create a pgm file, image is resized, if it is too big. # sift returns an error if more than 8000 features are found size = (MAXSIZE, MAXSIZE) im = Image.open(imagename).convert('L') im.thumbnail(size, Image.ANTIALIAS) im.save('tmp.pgm') imagename = 'tmp.pgm' #check if linux or windows if os.name == "posix": cmmd = "./sift < " + imagename + " > " + resultname else: cmmd = "siftWin32 < " + imagename + " > " + resultname # run extraction command returnvalue = subprocess.call(cmmd, shell=True) if returnvalue == 127: os.remove(resultname) # removing empty resultfile created by output redirection raise IOError("SIFT executable not found") if returnvalue == 2: os.remove(resultname) # removing empty resultfile created by output redirection raise IOError("image " + imagename + " not found") if os.path.getsize(resultname) == 0: raise IOError("extracting SIFT features failed " + resultname) else: import vlfeat # defines how dense the grid is size = (150, 150) step = 10 im = Image.open(imagename).resize(size, Image.ANTIALIAS) im_array = numpy.asarray(im) if im_array.ndim == 3: im_gray = vlfeat.vl_rgb2gray(im_array) elif im_array.ndim == 2: im_gray = im_array else: raise IOError("Not enough dims found in image " + resultname) locs, int_descriptors = vlfeat.vl_dsift(im_gray, step=step, verbose=VERBOSE) nfeatures = int_descriptors.shape[1] padding = numpy.zeros((2, nfeatures)) locs = numpy.vstack((locs, padding)) header = ' '.join([str(nfeatures), str(128)]) temp = int_descriptors.astype('float') # convert descriptors to float descriptors = temp[:] with open(resultname, 'wb') as f: cPickle.dump([locs.T, descriptors.T], f, protocol=cPickle.HIGHEST_PROTOCOL) print "features saved in", resultname if WRITE_VERBOSE: with open(resultname, 'w') as f: f.write(header) f.write("\n") for i in range(nfeatures): f.write(' '.join(map(str, locs[:, i]))) f.write("\n") f.write(' '.join(map(str, descriptors[:, i]))) f.write("\n")
def process_image(imagename, resultname="temp.sift", dense=False): """ process an image and save the results in a .key ascii file""" print "working on ", imagename if dense == False: if imagename[-3:] != "pgm": # create a pgm file, image is resized, if it is too big. # sift returns an error if more than 8000 features are found size = (MAXSIZE, MAXSIZE) im = Image.open(imagename).convert("L") im.thumbnail(size, Image.ANTIALIAS) im.save("tmp.pgm") imagename = "tmp.pgm" # check if linux or windows if os.name == "posix": cmmd = "./sift < " + imagename + " > " + resultname else: cmmd = "siftWin32 < " + imagename + " > " + resultname # run extraction command returnvalue = subprocess.call(cmmd, shell=True) if returnvalue == 127: os.remove(resultname) # removing empty resultfile created by output redirection raise IOError("SIFT executable not found") if returnvalue == 2: os.remove(resultname) # removing empty resultfile created by output redirection raise IOError("image " + imagename + " not found") if os.path.getsize(resultname) == 0: raise IOError("extracting SIFT features failed " + resultname) else: import vlfeat # defines how dense the grid is size = (150, 150) step = 10 im = Image.open(imagename).resize(size, Image.ANTIALIAS) im_array = numpy.asarray(im) if im_array.ndim == 3: im_gray = vlfeat.vl_rgb2gray(im_array) elif im_array.ndim == 2: im_gray = im_array else: raise IOError("Not enough dims found in image " + resultname) locs, int_descriptors = vlfeat.vl_dsift(im_gray, step=step, verbose=VERBOSE) nfeatures = int_descriptors.shape[1] padding = numpy.zeros((2, nfeatures)) locs = numpy.vstack((locs, padding)) header = " ".join([str(nfeatures), str(128)]) temp = int_descriptors.astype("float") # convert descriptors to float descriptors = temp[:] with open(resultname, "wb") as f: cPickle.dump([locs.T, descriptors.T], f, protocol=cPickle.HIGHEST_PROTOCOL) print "features saved in", resultname if WRITE_VERBOSE: with open(resultname, "w") as f: f.write(header) f.write("\n") for i in range(nfeatures): f.write(" ".join(map(str, locs[:, i]))) f.write("\n") f.write(" ".join(map(str, descriptors[:, i]))) f.write("\n")