def testsift(image1, image2, maxd=1000, distthresh=0.4): im1 = imread(image1) print im1[:, :] im2 = imread(image2) print im2.shape kdt = [[], [], []] d1 = [[], [], []] d2 = [[], [], []] C = 3 for channel in range(C): if C == 1: _, d1[channel] = sift.descriptors(im1, maxd) else: _, d1[channel] = sift.descriptors(im1[:, :, channel], maxd) print channel, d1[channel].shape if C == 1: _, d2[channel] = sift.descriptors(im2, maxd) else: _, d2[channel] = sift.descriptors(im2[:, :, channel], maxd) print channel, d2[channel].shape kdt[channel] = KDTree(d1[channel]) for desc in d2[channel]: dist, index = kdt[channel].query(desc) if distthresh >= dist: print dist, index
def imagedesc(filename, imgsize, maxd, crop, datamode): if datamode == 1: im = imread(filename, flatten=True) if crop > 0: hpix = int(im.shape[0] * crop) wpix = int(im.shape[1] * crop) im = im[hpix : im.shape[0] - hpix, wpix : im.shape[1] - wpix] im = imresize(im, float(imgsize) / max(im.shape)) # im = (exposure.equalize_hist(im)*256).astype(np.float32) _, d = sift.descriptors(im, maxd) return d if datamode == 2: im = imread(filename) if crop > 0: hpix = int(im.shape[0] * crop) wpix = int(im.shape[1] * crop) im = im[hpix : im.shape[0] - hpix, wpix : im.shape[1] - wpix, :] im = imresize(im, float(imgsize) / max(im.shape)) d = [None, None, None] for channel in range(3): # im = (exposure.equalize_hist(im)*256).astype(np.float32) _, d[channel] = sift.descriptors(im[:, :, channel], maxd) return d
def imagedescriptors(image_filename, object_id): ndimage = imread(path_images + image_filename) if len(ndimage.shape) == 2: print "Warning: image %s is an grayscale image" % image_filename return None size = max(ndimage[:, :, 0].shape) ndimage = imresize(ndimage, 360.0 / size) print ndimage.shape result = [] for channel in range(3): print object_id, image_filename keypoints, descriptors = sift.descriptors(ndimage[:, :, channel], max_descriptors) nd = descriptors.shape[0] descriptors = descriptors.reshape(nd, 128) result.append(descriptors) return result
import sift import numpy as np from scipy.misc import imread,imshow,imresize import matplotlib from time import time im=imread("test2.jpg") #im = matplotlib.colors.rgb_to_hsv(im) R=im[:,:,0].astype(np.float32) G=im[:,:,1].astype(np.float32) B=im[:,:,2].astype(np.float32) for channel in [R,G,B]: t = time() keypoints, descriptors = sift.descriptors(channel, 30) print time() - t print descriptors.shape for point in keypoints: channel[point[1],point[0]] = 0 channel[point[1],point[0]+1] = 255 imshow(channel)