def SIFT(qImg, tImg, rect): # MA: test that keypoints are inside the region of interest rect # x1 = rect[0]; # y1 = rect[1]; # x2 = x1 + rect[2]; # y2 = y1 + rect[3]; x1 = rect.x1 y1 = rect.y1 x2 = rect.x2 y2 = rect.y2 use_opencv_sift = True if use_opencv_sift: # compute SIFT with OpenCV sift = cv2.SIFT() # find the keypoints and descriptors with SIFT list_kp1, des1 = sift.detectAndCompute(qImg, None) list_kp2, des2 = sift.detectAndCompute(tImg, None) kp1 = convert_keypoints_opencv_vlfeat(list_kp1) kp2 = convert_keypoints_opencv_vlfeat(list_kp2) else: # compute SIFT with vlfeat import vlfeat I = np.array(qImg, dtype=np.float32) _kp1, _des1 = vlfeat.vl_sift(data=I) des1 = np.transpose(np.array(_des1, copy=True)) kp1 = np.transpose(np.array(_kp1, copy=True)) I = np.array(tImg, dtype=np.float32) _kp2, _des2 = vlfeat.vl_sift(data=I) des2 = np.transpose(np.array(_des2, copy=True)) kp2 = np.transpose(np.array(_kp2, copy=True)) if kp1.shape[0] > 0 and kp2.shape[0] > 0: boolidx1 = np.logical_and(kp1[:, 0] >= x1, kp1[:, 0] <= x2) boolidx2 = np.logical_and(kp1[:, 1] >= y1, kp1[:, 1] <= y2) boolidx = np.logical_and(boolidx1, boolidx2) des1 = des1[boolidx, :] kp1 = kp1[boolidx, :] matches = match_and_ratio_test(des1, des2) else: matches = [] return kp1, kp2, matches, des1, des2
def SIFT(qImg, tImg, rect): # MA: test that keypoints are inside the region of interest rect # x1 = rect[0]; # y1 = rect[1]; # x2 = x1 + rect[2]; # y2 = y1 + rect[3]; x1 = rect.x1; y1 = rect.y1; x2 = rect.x2; y2 = rect.y2; use_opencv_sift = True; if use_opencv_sift: # compute SIFT with OpenCV sift = cv2.SIFT() # find the keypoints and descriptors with SIFT list_kp1, des1 = sift.detectAndCompute(qImg,None) list_kp2, des2 = sift.detectAndCompute(tImg,None) kp1 = convert_keypoints_opencv_vlfeat(list_kp1); kp2 = convert_keypoints_opencv_vlfeat(list_kp2); else: # compute SIFT with vlfeat import vlfeat; I = np.array(qImg, dtype=np.float32); _kp1, _des1 = vlfeat.vl_sift(data=I); des1 = np.transpose(np.array(_des1, copy=True)); kp1 = np.transpose(np.array(_kp1, copy=True)); I = np.array(tImg, dtype=np.float32); _kp2, _des2 = vlfeat.vl_sift(data=I); des2 = np.transpose(np.array(_des2, copy=True)); kp2 = np.transpose(np.array(_kp2, copy=True)); if kp1.shape[0] > 0 and kp2.shape[0] > 0: boolidx1 = np.logical_and(kp1[:, 0] >= x1, kp1[:, 0] <= x2); boolidx2 = np.logical_and(kp1[:, 1] >= y1, kp1[:, 1] <= y2); boolidx = np.logical_and(boolidx1, boolidx2); des1 = des1[boolidx, :]; kp1 = kp1[boolidx, :] matches = match_and_ratio_test(des1, des2); else: matches = []; return kp1, kp2, matches, des1, des2;
def siftify(store, dataset, patch_shape, frame): """SIFT descriptor for every patch in _store_. Compute descriptor at frame _frame_. """ totals = store.shape[0] print "Generating ", totals, " SIFT descriptors for", store, "at scale", frame[2] for i in xrange(totals): patch = store[i].reshape(patch_shape) [_, d] = vl_sift(patch.T, frames=frame) dataset[i] = np.asarray(d.ravel(), dtype=np.float64)
def Extract(self, img_name): img = cv2.imread(img_name, cv2.IMREAD_GRAYSCALE) img = img.astype(np.float32) / 255 height = img.shape[0] width = img.shape[1] # print img peak = self._config.Get('sift_peak_threshold') edge = self._config.Get('sift_edge_threshold') loc, des = vlfeat.vl_sift(img, peak_thresh=peak, edge_thresh=edge) #loc, des = vlfeat.vl_sift(img) loc = np.round(loc[0:2, :].T) loc[:, 0] -= width / 2 loc[:, 1] -= height / 2 #bigger = max(width, height) #loc /= bigger des = des.T #self.SaveFeature(img_name, loc, des) print 'Extract SIFT %s total %d points' % (img_name, loc.shape[0]) return [loc, des.astype(np.float32)]
def extractFeaturesPerClass(images, labels, peak_thresh, edge_thresh): """ Extracts SIFT descriptors from the L{images}. This method uses the vlfeat library from U{http://www.vlfeat.org/}. @type images: An iterator of images from which SIFT descriptors are to be computed. @param images: [Image.Image] @param peak_thresh: The peak threshold used during SIFT feature extraction. @type peak_thresh: float @param edge_thresh: The edge threshold used during SIFT feature extraction. @type edge_thresh: float @return: A tuple. The first item contains a list of 2D arrays which in turn hold the SIFT descriptors for every image. The second items contains total amount of descriptors. @rtype: ([C{numpy.ndarray}], int) """ if not isinstance(images, collections.Iterable): images = [images] numDescriptors = 0 imgCount = 0 descPerClass = dict() for lbl, imgPath in zip(labels, images): # Convert to gray level image img = Image.open(imgPath).convert('L') img = numpy.array(img, dtype=numpy.float32, order='F') _, perImgDesc = vlfeat.vl_sift(img, octaves=5, levels=3, peak_thresh=peak_thresh, edge_thresh=edge_thresh, verbose=0) numDescriptors += numpy.size(perImgDesc, 1) imgCount += 1 recognosco.logger.debug("Extracted %i features from image #%i. Total features: %i", numpy.size(perImgDesc, 1), imgCount, numDescriptors) if lbl not in descPerClass: descPerClass[lbl] = [[0], numpy.empty((0, 128), numpy.uint8)] descPerClass[lbl][1] = numpy.vstack((descPerClass[lbl][1], perImgDesc.transpose())) descPerClass[lbl][0].append(numpy.size(descPerClass[lbl][1], 0)) return (descPerClass, numDescriptors)
def extractFeatures(images, peak_thresh, edge_thresh): """ Extracts SIFT descriptors from the L{images}. This method uses the vlfeat library from U{http://www.vlfeat.org/}. @type images: An iterator of images from which SIFT descriptors are to be computed. @param images: [Image.Image] @param peak_thresh: The peak threshold used during SIFT feature extraction. @type peak_thresh: float @param edge_thresh: The edge threshold used during SIFT feature extraction. @type edge_thresh: float @return: A tuple. The first item contains a list of 2D arrays which in turn hold the SIFT descriptors for every image. The second items contains total amount of descriptors. @rtype: ([C{numpy.ndarray}], int) """ if not isinstance(images, collections.Iterable): images = [images] numDescriptors = 0 desc = [] for imgPath in images: # Convert to gray level image convImg = Image.open(imgPath).convert('L') convImg = numpy.array(convImg, dtype=numpy.float32, order='F') _, perImgDesc = vlfeat.vl_sift(data=convImg, octaves=5, levels=3, peak_thresh=peak_thresh, edge_thresh=edge_thresh, verbose=0) numDescriptors += numpy.size(perImgDesc, 1) recognosco.logger.debug("Extracted %i features from %s. Total: %i", numpy.size(perImgDesc, 1), imgPath, numDescriptors) desc.append(perImgDesc.transpose()) return (desc, numDescriptors)
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) pylab.show()
def extractSIFT(gray_img): # No need for conversion here, since the input frame should be grayscale # gray= np.float32(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)) f,d = vlfeat.vl_sift(gray_img,peak_thresh=0.1) return (f,d)
# -------------------------------------------------------------------- # 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') print 'sift_basic_2' # h3 = vl_plotsiftdescriptor(d(:,sel),f(:,sel)) ; # set(h3,'color','k','linewidth',2) ; # h4 = vl_plotsiftdescriptor(d(:,sel),f(:,sel)) ; # set(h4,'color','g','linewidth',1) ; # h1 = vl_plotframe(f(:,sel)) ; set(h1,'color','k','linewidth',3) ; # h2 = vl_plotframe(f(:,sel)) ; set(h2,'color','y','linewidth',2) ; # # vl_demo_print('sift_basic_3') ;
from vlfeat.test.vl_test_pattern import vl_test_pattern if __name__ == '__main__': # create pattern I = vl_test_pattern(1) ; # create frames (spatial sampling) ur = numpy.arange(0, I.shape[1], 5) vr = numpy.arange(0, I.shape[0], 5) [u,v] = numpy.meshgrid(ur, vr) f = numpy.array([u.ravel(), v.ravel()]) K = f.shape[1] f = numpy.vstack([f, 2 * numpy.ones([1, K]), 0 * numpy.ones([1, K])]) # convert frame to good format f = numpy.array(f, order='F') # compute sift f, d = vlfeat.vl_sift(numpy.array(I, 'f', order='F'), frames=f, orientations=True) # display results pylab.gray() pylab.imshow(I) vl_plotframe(f, color='k', linewidth=3) vl_plotframe(f, color='y', linewidth=2) pylab.show() print 'sift_or'
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) pylab.show()
def generate_desc(self, img_data, kpts): ''' cannot use opencv functions, use vl functions to do this for us ''' drop, self.desc = vl.vl_sift(img_data, kpts, orientations = False) #why cannot change to true for orientation????
def extractSIFT(gray_img): # No need for conversion here, since the input frame should be grayscale # gray= np.float32(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)) f, d = vlfeat.vl_sift(gray_img, peak_thresh=0.1) return (f, d)