def extract_keypoints(path, scale_factor=1.0): cap = cv.VideoCapture(path) detector, matcher = init_feature('surf') raw_result = [] first_frame = True frame_idx = 0 kp1 = None kp2 = None des1 = None des2 = None key_points1 = None key_points2 = None while cap.isOpened(): ret, frame = cap.read() if not ret: break kp, des = detector.detectAndCompute(frame, None) if first_frame: first_frame = False kp1 = kp des1 = des key_points1 = [{'pt': p.pt, 'color': generate_a_random_hex_color()} for p in kp] raw_result.append({'frame_idx': frame_idx, 'key_points': key_points1}) else: kp2 = kp des2 = des raw_matches = matcher.knnMatch(des1, des2, k=2) src_pts, dst_pts, kp_pairs = filter_matches(kp1, kp2, raw_matches) key_points2 = get_key_points2(key_points1, src_pts, dst_pts) raw_result.append({'frame_idx': frame_idx, 'key_points': key_points2}) kp1 = kp2 des1 = des2 key_points1 = key_points2 frame_idx += 1 print(frame_idx) cap.release() result = [] for f in raw_result: key_points = [] raw_key_points = f['key_points'] for kp in raw_key_points: key_points.append({'pt': (int(round(kp['pt'][0] * scale_factor)), int(round(kp['pt'][1] * scale_factor))), 'color': kp['color']}) result.append({'frame_idx': f['frame_idx'], 'key_points': key_points}) return result
def main(): import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'brisk-flann') try: fn1, fn2 = args except: fn1 = 'data/aero1.jpg' fn2 = 'data/aero3.jpg' img1 = cv.imread(cv.samples.findFile(fn1), cv.IMREAD_GRAYSCALE) img2 = cv.imread(cv.samples.findFile(fn2), cv.IMREAD_GRAYSCALE) detector, matcher = init_feature(feature_name) if img1 is None: print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: print('unknown feature:', feature_name) sys.exit(1) print('using', feature_name) pool = ThreadPool(processes=cv.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) def match_and_draw(win): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors=desc2, k=2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv.findHomography(p1, p2, cv.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None print('%d matches found, not enough for homography estimation' % len(p1)) explore_match(win, img1, img2, kp_pairs, None, H) match_and_draw('affine find_obj') cv.waitKey() print('Done')
def main(): import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'brisk-flann') try: fn1, fn2 = args except: fn1 = 'aero1.jpg' fn2 = 'aero3.jpg' img1 = cv.imread(cv.samples.findFile(fn1), cv.IMREAD_GRAYSCALE) img2 = cv.imread(cv.samples.findFile(fn2), cv.IMREAD_GRAYSCALE) detector, matcher = init_feature(feature_name) if img1 is None: print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: print('unknown feature:', feature_name) sys.exit(1) print('using', feature_name) pool=ThreadPool(processes = cv.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) def match_and_draw(win): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv.findHomography(p1, p2, cv.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None print('%d matches found, not enough for homography estimation' % len(p1)) explore_match(win, img1, img2, kp_pairs, None, H) match_and_draw('affine find_obj') cv.waitKey() print('Done')
def add_data(self, num, inter=49): temp_array = pickle.load( open("./feature/keypoints_database_" + self.alg + ".p", "rb")) # save training data import glob, sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', self.alg + '-flann') detector, matcher = init_feature(feature_name) if detector is not None: print 'using', feature_name else: print 'unknown feature:', feature_name sys.exit(1) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) print 'start' icc = 0 errors = [] k = 0 for imagePath in glob.glob("./collect_bi/*.*"): k += 1 if k > num: imageName = imagePath[imagePath.rfind("/") + 1:imagePath.rfind(".")] image = cv2.imread(imagePath) print imageName try: kp, desc = affine_detect(detector, image, pool=pool) temp = pickle_keypoints(kp, desc, imageName, imagePath) temp_array.append(temp) icc += 1 print icc, imageName except: errors.append(imageName) time.sleep(0.1) if k - num > inter: break # time.sleep(1) else: time.sleep(1) pickle.dump( temp_array, open("./feature/keypoints_database_" + self.alg + ".p", "wb")) print 'error!!!' for i in errors: print i print 'successful finish'
def matching_image(fn1='aero1.jpg', fn2='aero3.jpg', feature='sift'): import sys feature_name = feature detector, matcher = init_feature(feature_name) try: img1 = cv.imread(cv.samples.findFile(fn1), cv.IMREAD_GRAYSCALE) img2 = cv.imread(cv.samples.findFile(fn2), cv.IMREAD_GRAYSCALE) except: img1 = fn1 img2 = fn2 if img1 is None: print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: print('unknown feature:', feature_name) sys.exit(1) print('using', feature_name) pool=ThreadPool(processes = cv.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) def match_and_draw(win): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv.findHomography(p1, p2, cv.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None print('%d matches found, not enough for homography estimation' % len(p1)) explore_match(win, img1, img2, kp_pairs, None, H) return blending(img1, img2, H) final = match_and_draw('affine find_obj') cv.imwrite('{}_panorama.jpg'.format(feature), final) cv.waitKey() print('Done')
def extract_descriptor_process(image_array, thumbnail_size, feature_type="SIFT", descriptor_type="SIFT"): #Converte para cinza se tiver mais que 3 canais (RGB) img_gray = image_array if len(image_array.shape) >= 3: img_gray = cv2.cvtColor(image_array, cv2.COLOR_RGB2GRAY) img_gray = numpy.array(img_gray, numpy.uint8) if descriptor_type == "DAISY": try: daisy_descs = [] descs = daisy(img_gray, step=15, radius=15, rings=14, histograms=6, orientations=8) for row in range(descs.shape[0]): for col in range(descs.shape[1]): daisy_descs.append(descs[row][col]) #print len(daisy_descs) return None, daisy_descs except: print "Error while extracting Daisy descriptor" return None, None if descriptor_type == "HOG": descs = hog(img_gray, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1)) if len(descs) < 512: return None, None hog_descs = [] hog_descs.append(descs[0:648]) return None, hog_descs if feature_type == "ASIFT" or descriptor_type == "ASIFT": detector, matcher = init_feature('sift-flann') kp, descs = affine_detect(detector, img_gray) return kp, descs #Normalize #img_gray = img_gray.astype(numpy.float32) #img_gray = (img_gray - img_gray.mean())/img_gray.std() #img_gray = cv2.normalize(img_gray, img_gray,0, 255, cv2.NORM_MINMAX) #img_gray = img_gray.astype(numpy.uint8) featureDetector = cv2.FeatureDetector_create(feature_type) descriptorDetector = cv2.DescriptorExtractor_create(descriptor_type) keypoints = featureDetector.detect(img_gray) keypoints, descriptors = descriptorDetector.compute(img_gray, keypoints) return keypoints, descriptors
def detect_image(img1, img2): import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'brisk-flann') detector, matcher = init_feature(feature_name) if img1 is None: print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: print('unknown feature:', feature_name) sys.exit(1) print('using', feature_name) img1 = cv.GaussianBlur(img1, (5, 5), sigmaX=5) pool = ThreadPool(processes=cv.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) def match_and_draw(win): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors=desc2, k=2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv.findHomography(p1, p2, cv.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None print('%d matches found, not enough for homography estimation' % len(p1)) img, corners = explore_match(win, img1, img2, kp_pairs, None, H) return img, corners, kp_pairs return match_and_draw('affine find_obj')
def doFeatureMatch(img1, img2, way, win): import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) method = way + '-flann' feature_name = opts.get('--feature', 'surf-flann') detector, matcher = init_feature(feature_name) if img1 is None: print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: print('unknown feature:', feature_name) sys.exit(1) print('using', feature_name) pool = ThreadPool(processes=cv.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors=desc2, k=2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv.findHomography(p1, p2, cv.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None print('%d matches found, not enough for homography estimation' % len(p1)) explore_match(win, img1, img2, kp_pairs, None, H) cv.waitKey() cv.destroyAllWindows()
def main(args): with h5py.File(args['ah'], "r") as a: kps = a['keypoints'][()] kp1 = [cv2.KeyPoint(kp[0], kp[1], kp[2], kp[3]) for kp in kps] desc1 = a['descriptors'][()] with h5py.File(args['bh'], "r") as b: kps = b['keypoints'][()] kp2 = [cv2.KeyPoint(kp[0], kp[1], kp[2], kp[3]) for kp in kps] desc2 = b['descriptors'][()] detector, matcher = init_feature("sift") with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors=desc2, k=2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None print('%d matches found, not enough for homography estimation' % len(p1)) img1 = cv2.imread(args['a'], 0) img2 = cv2.imread(args['b'], 0) explore_match("LIFT Match", img1, img2, kp_pairs, None, H) cv2.waitKey(0) cv2.destroyAllWindows() pass
def start(feature_name, fn1, fn2): #print(__doc__) import sys, getopt ''' opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'brisk-flann') try: fn1, fn2 = args except: fn1 = '../data/aero1.jpg' fn2 = '../data/aero3.jpg' ''' img1 = cv2.imread(fn1, 0) img2 = cv2.imread(fn2, 0) detector, matcher = init_feature(feature_name) if img1 is None: print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: print('unknown feature:', feature_name) sys.exit(1) print('using', feature_name) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) #print (kp1[0]) print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) def match_and_draw(win): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors=desc2, k=2) #2 p1, p2, kp_pairs, desc_pairs = filter_matches(kp1, kp2, raw_matches, desc1, desc2) if len(p1) >= 4: H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] desc_pairs = [dp for dp, flag in zip(desc_pairs, status) if flag] #for kk in kp_pairs: #print (kk) return desc_pairs else: H, status = None, None print('%d matches found, not enough for homography estimation' % len(p1)) vis = explore_match(win, img1, img2, kp_pairs, None, H) desc_pairs = match_and_draw('affine find_obj') #cv2.waitKey() cv2.destroyAllWindows() return desc_pairs
with a large circle in a blank image virtualdisks = np.zeros(img2.shape) max_threshold = 200 virtualradius = 60 eraserradius = 13 while np.max(img1) > 200: cy, cx = np.argmax(img1, axis=0), np.argmax(img1, axis=1) rr, cc = circle(cy, cx, virtualradius) virtualdisks[rr, cc] = 255 re, ce = circle(cy, cx, eraserradius) img1[re, ce] = 0 # now match using asift (from OpenCV samples) detector, matcher = init_feature('orb') pool=ThreadPool(processes = cv2.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, virtualdisks, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) def match_and_draw(win): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) print '%d / %d inliers/matched' % (np.sum(status), len(status)) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None print '%d matches found, not enough for homography estimation' % len(p1)
descrs.extend(d) print return keypoints, np.array(descrs) if __name__ == '__main__': ''' import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'sift-flann') try: fn1, fn2 = args except: fn1 = 'data/aero1.jpg' fn2 = 'data/aero3.jpg' ''' detector, matcher = init_feature('sift') img1 = cv2.imread('usb.png', cv2.CV_LOAD_IMAGE_GRAYSCALE) #cap = video.create_capture('tree.avi') cap = cv2.VideoCapture('output.avi') pool=ThreadPool(processes = cv2.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) ''' detector, matcher = init_feature(feature_name) if detector != None: print 'using', feature_name else: print 'unknown feature:', feature_name sys.exit(1)
def search(self, fn1): if self.keypoints_database == None: start_time = datetime.now() print 'loading training file', datetime.now() self.keypoints_database = pickle.load( open("./feature/keypoints_database_" + self.alg + ".p", "rb")) print 'load complete cost:', datetime.now() - start_time import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) # feature_name = opts.get('--feature', 'sift-flann') feature_name = opts.get('--feature', self.alg + '-flann') fn2 = './img/LV路易威登.jpg' img1 = cv2.imread(fn1, 0) imgt = cv2.imread(fn2, 0) # img2 = cv2.imread(img1) detector, matcher = init_feature(feature_name) # print detector, '123123' if detector is not None: print 'using', feature_name else: print 'unknown feature:', feature_name sys.exit(1) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) try: kp1, desc1 = affine_detect(detector, img1, pool=pool) except: return '0' results = [] def match_and_draw(win, kpa, desca3): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors=desca3, k=2) #2 p1, p2, kp_pairs = filter_matches(kp1, kpa, raw_matches) if len(p1) > 4: H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) print '%d / %d inliers/matched' % (np.sum(status), len(status)) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] return win, kp_pairs, H, (np.sum(status)) else: return win, kp_pairs, None, -1 start_m = datetime.now() for keypoints_data in self.keypoints_database: kp3, desc3, name, path = unpickle_keypoints(keypoints_data) try: win, kp_pairs, H, num = match_and_draw('affine find_obj', kp3, desc3) result = { 'win': win, 'name': name[0], 'path': path[0], 'kp_pairs': kp_pairs, 'num': num, 'H': H } results.append(result) except: print name, 'error!' print 'match finish: ', datetime.now() - start_m results.sort(reverse=True, key=lambda x: x['num']) results = self.get_result(results) for i in results: print i return results[0]['name']
def detect(img1): detector, matcher = init_feature("sift-flann") pool = ThreadPool(processes=cv2.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) return desc1
if __name__ == '__main__': print __doc__ import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'sift-flann') try: fn1, fn2 = args except: fn1 = 'C:/opencv-2.4.9/sources/samples/python2/data/aero1.jpg'#入力画像 fn2 = 'C:/opencv-2.4.9/sources/samples/python2/data/aero3.jpg' img1 = cv2.imread(fn1, 0) img2 = cv2.imread(fn2, 0) detector, matcher = init_feature(feature_name) if detector != None: print 'using', feature_name else: print 'unknown feature:', feature_name sys.exit(1) pool=ThreadPool(processes = cv2.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2)) def match_and_draw(win): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches)
for i, (k, d) in enumerate(ires): print('affine sampling: %d / %d\r' % (i+1, len(params)), end='') keypoints.extend(k) descrs.extend(d) return keypoints, np.array(descrs) if __name__ == '__main__': if len(sys.argv) < 4: print ("Usage %s <image1> <image2> <keypoint_detector>" % sys.argv[0]) exit() img1 = cv2.imread(sys.argv[1], 0) # queryImage img2 = cv2.imread(sys.argv[2], 0) # trainImage detectstr = sys.argv[3] detector, matcher = init_feature(detectstr) print('using', detectstr) cpucount = cv2.getNumberOfCPUs() / 2 pool=ThreadPool(processes = cpucount) pool = None print("No. of CPUs: %d" % cpucount) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, matchMask = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(matchMask), len(matchMask)))
def run(self): # init big image fro stitching ret, frame = self.cam.read() frame=cv2.resize(frame,(320,240)) h,w,d=frame.shape big_image = np.zeros((h*12,w*3,3), np.uint8) starty=h*11 startx=w total_transl_x=0 total_transl_y=0 frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) self.prev_gray=frame_gray self.prev_frame=frame detector, matcher = init_feature('sift-flann') pool=ThreadPool(processes = cv2.getNumberOfCPUs()) while True: for i in range(skip_frames): ret, frame = self.cam.read() ret, frame = self.cam.read() frame=cv2.resize(frame,(320,240)) frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) vis = frame.copy() img0, img1 = self.prev_gray, frame_gray kp1, desc1 = affine_detect(detector, img0[10:h-50,10:w-10], pool=pool) kp2, desc2 = affine_detect(detector, img1[10:h-50,10:w-10], pool=pool) print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2)) with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) print '%d / %d inliers/matched' % (np.sum(status), len(status)) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] warp = cv2.warpPerspective(img0[10:h-50,10:w-10], H, (w, h*2)) cv2.imshow("warped",warp) else: H, status = None, None print '%d matches found, not enough for homography estimation' % len(p1) vis = explore_match('affine find_obj', img0, img1, kp_pairs, None, H) # stitching----------------------- translation = np.zeros((3,1)) #3,1 if len(p1)>4 and len(p2)>4: # temp1=[] # # temp2=[] # for i in range(len(kp1)): # print kp1[i].pt+ (0,) # temp1.append(kp1[i].pt+ (0,)) # # for i in range(len(kp2)): # # temp2.append(kp2[i].pt) # points1.astype(np.uint8) # points1 = np.array(temp1) # print points1 # # points2 = np.array(temp2) # Hr=cv2.estimateRigidTransform(points1, points1,False) translation[:,0] = H[:,2] #Hr[:,2] # rotation = np.zeros((3,3)) # rotation[:,0] = H[:,0] # rotation[:,1] = H[:,1] # rotation[:,2] = np.cross(H[0:3,0],H[0:3,1]) # print "x translation:",translation[0] # print "y translation:",translation[1] draw_str(vis, (20, 40), 'x-axis translation: %.1f' % translation[0]) draw_str(vis, (20, 60), 'y-axis translation: %.1f' % translation[1]) if translation[0]<60 and translation[1]<60: #check for bad H total_transl_x+=int(translation[0]) total_transl_y+=int(translation[1]) draw_str(vis, (20, 80), 'tot x-axis translation: %.1f' % total_transl_x) draw_str(vis, (20, 100), 'tot y-axis translation: %.1f' % total_transl_y) #h,w,d=frame.shape frame_over=self.prev_frame[10:h-50,10:w-10].copy() overlay = cv2.warpPerspective(frame_over, H, (w, h)) frame_h,frame_w,d=frame_over.shape cv2.imshow('overlay',overlay) #vis = cv2.addWeighted(vis, 0.5, overlay, 0.5, 0.0) big_image[starty-int(total_transl_y):starty-int(total_transl_y)+frame_h,startx-int(total_transl_x):startx-int(total_transl_x)+frame_w]=overlay[0:frame_h,0:frame_w].copy() #small_image=big_image.copy() big_h,big_w,d=big_image.shape small_image=cv2.resize(big_image,(big_w/4,big_h/4)) cv2.imshow('stitching', small_image) #cv2.imwrite("result.jpg",big_image); self.frame_idx += 1 self.prev_gray = frame_gray self.prev_frame=frame ch = 0xFF & cv2.waitKey(5) if ch == 27: break
if __name__ == '__main__': print __doc__ import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'sift-flann') try: fn1, fn2 = args except: fn1 = 'data/aero1.jpg' fn2 = 'data/aero3.jpg' img1 = cv2.imread(fn1, 0) img2 = cv2.imread(fn2, 0) detector, matcher = init_feature(feature_name) if img1 is None: print 'Failed to load fn1:', fn1 sys.exit(1) if img2 is None: print 'Failed to load fn2:', fn2 sys.exit(1) if detector is None: print 'unknown feature:', feature_name sys.exit(1) print 'using', feature_name
def detect_image(img1, img2): import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'brisk-flann') detector, matcher = init_feature(feature_name) if img1 is None: print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: print('unknown feature:', feature_name) sys.exit(1) print('using', feature_name) #img1 = cv.GaussianBlur(img1, (5, 5), sigmaX = 5) for i in np.arange(3): img1 = cv2.pyrDown(img1) pool = ThreadPool(processes=cv.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) def match_and_draw(win): with Timer('matching'): raw_matches = matcher.knnMatch(desc1, trainDescriptors=desc2, k=2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) #p1, p2, kp_pairs = filter_matches_std(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv.findHomography(p1, p2, cv.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: #regular sift '''# Initiate SIFT detector sift = cv2.xfeatures2d.SIFT_create() # find the keypoints and descriptors with SIFT kp1, des1 = sift.detectAndCompute(img1, None) kp2, des2 = sift.detectAndCompute(img2, None) # FLANN parameters FLANN_INDEX_KDTREE = 0 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict(checks=50) # or pass empty dictionary flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(des1, des2, k=2) # Need to draw only good matches, so create a mask matchesMask = [[0, 0] for i in range(len(matches))] # ratio test as per Lowe's paper for i, (m, n) in enumerate(matches): if m.distance < 0.7 * n.distance: matchesMask[i] = [1, 0] draw_params = dict(matchColor=(0, 255, 0), singlePointColor=(255, 0, 0), matchesMask=matchesMask, flags=2) img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, None, **draw_params) cv2.imshow('matched',img3)''' return sift_detect(img1, img2) img, corners = explore_match(win, img1, img2, kp_pairs, None, H) return img, corners, kp_pairs return match_and_draw('affine find_obj')