def run(self): while not self._stop.isSet(): task = self.q.get() if task != None: obj, image = task rect = cv.BoundingRect(obj.cont) siftimage = siftfastpy.Image(rect[2], rect[3]) cv.CvtColor(image, self.gray, cv.CV_BGR2GRAY) cv.SetZero(self.mask) cv.FillPoly(self.mask, [obj.cont], cv.Scalar(255)) cv.And(self.gray, self.mask, self.gray) gnp = np.asarray(cv.GetSubRect(self.gray, rect)) siftimage.SetData(gnp) t0 = time.time() # compute keypoints and time how long it takes frames,desc = siftfastpy.GetKeypoints(siftimage) self.stats.append((rect[2]*rect[3], time.time() - t0)) # compute feature vector tmp = np.concatenate((frames[:,2:4], desc), axis=1).astype('float64') # search in the flann tree for the feature vectors n = 2 thr = 1.5 result, dists = self.flann.nn_index(tmp, n, checks=32) # ismatch contains the indices in the testset for which a match is found ismatch = dists[:,1] > thr * dists[:,0] # meta contains the index to object-ID mapping obj.ids = [] for i, res in enumerate(result): if ismatch[i]: obj.ids.append(self.meta[res[0]][0]) # obj.ids = [self.meta[res][0] for i, res in enumerate(result) if ismatch[i]] # transfer keypoints back to full frame coordinates frames[:,0] += rect[0] frames[:,1] += rect[1] obj.frames = frames obj.desc = desc
def run(self): starttime = time.time() gray = cv.CreateMat(480, 640, cv.CV_8UC1) im = cv.LoadImage(self.image) rect = cv.BoundingRect(self.obj.cont) siftimage = siftfastpy.Image(rect[2], rect[3]) cv.CvtColor(im, gray, cv.CV_BGR2GRAY) gnp = np.asarray(cv.GetSubRect(gray, rect)) siftimage.SetData(gnp) print 'initialization in: %fs' % (time.time() - starttime) frames, desc = siftfastpy.GetKeypoints(siftimage) self.obj.frames = frames self.obj.desc = desc print '%d keypoints found in %fs' % (frames.shape[0], time.time() - starttime)
def get_sift_keypoints(img): """Get sift keypoints from image Parameters ---------- img: array-like Input image of shape ``(M, N)`` from which we get sift keypoints Returns ------- frames: numpy.ndarray each row has [col, row, orientation, scale] in this order desc: numpy.ndarray descriptors of each frame """ if type(img) is not np.ndarray: img = np.array(img) if len(img.shape) != 2: raise ValueError('image should be 2d array: {}'.format(img.shape)) siftimg = siftfastpy.Image(img.shape[1], img.shape[0]) siftimg.SetData(img) frames, desc = siftfastpy.GetKeypoints(siftimg) return frames, desc
karton['image'].reverse() karton['depth'].reverse() karton['features'] = [] karton['frames'] = [] karton['ratios'] = [[], [], []] # compute feature vectors for all images for i, image in enumerate(karton['image']): siftimage = siftfastpy.Image(image.shape[1], image.shape[0]) gray = cv.CreateMat(image.shape[0], image.shape[1], cv.CV_8UC1) cv.CvtColor(image, gray, cv.CV_BGR2GRAY) siftimage.SetData(np.asarray(gray)) # compute keypoints frames, desc = siftfastpy.GetKeypoints(siftimage) # compute feature vector depth = np.zeros((frames.shape[0], 1)) for j in range(frames.shape[0]): depth[j] = karton['depth'][i][math.floor(frames[j, 1]), math.floor(frames[j, 0])] tmp = np.concatenate((frames[:, 2:4], desc, depth), axis=1).astype('float64') karton['features'].append(tmp) karton['frames'].append(frames) flann = pyflann.FLANN() for i in range(len(karton['features']) - 1):