def epipolar_rectify(imL,imR,show_matches=True): descriptor_extractor = ORB(n_keypoints=2000) descriptor_extractor.detect_and_extract(imL) keypoints1 = descriptor_extractor.keypoints descriptors1 = descriptor_extractor.descriptors descriptor_extractor.detect_and_extract(imR) keypoints2 = descriptor_extractor.keypoints descriptors2 = descriptor_extractor.descriptors matches12 = match_descriptors(descriptors1, descriptors2,metric='hamming', cross_check=True) pts1=keypoints1[matches12[:,0],:] pts2=keypoints2[matches12[:,1],:] F, mask = cv2.findFundamentalMat(pts1,pts2,cv2.FM_RANSAC) pts1 = pts1[mask.ravel()==1] pts2 = pts2[mask.ravel()==1] res,H1,H2=cv2.stereoRectifyUncalibrated(pts1,pts2,F,imL.shape,10) if show_matches: fig, ax = plt.subplots(nrows=1, ncols=1) plot_matches(ax, imL, imR, keypoints1, keypoints2, matches12) return H1,H2
def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) # Read features. locations_1, _, descriptors_1, _, _ = feature_io.ReadFromFile( cmd_args.features_1_path) num_features_1 = locations_1.shape[0] tf.logging.info("Loaded image 1's %d features" % num_features_1) locations_2, _, descriptors_2, _, _ = feature_io.ReadFromFile( cmd_args.features_2_path) num_features_2 = locations_2.shape[0] tf.logging.info("Loaded image 2's %d features" % num_features_2) # Find nearest-neighbor matches using a KD tree. d1_tree = cKDTree(descriptors_1) _, indices = d1_tree.query( descriptors_2, distance_upper_bound=_DISTANCE_THRESHOLD) # Select feature locations for putative matches. locations_2_to_use = np.array([ locations_2[i,] for i in range(num_features_2) if indices[i] != num_features_1 ]) locations_1_to_use = np.array([ locations_1[indices[i],] for i in range(num_features_2) if indices[i] != num_features_1 ]) # Perform geometric verification using RANSAC. _, inliers = ransac( (locations_1_to_use, locations_2_to_use), AffineTransform, min_samples=3, residual_threshold=20, max_trials=1000) tf.logging.info('Found %d inliers' % sum(inliers)) # Visualize correspondences, and save to file. _, ax = plt.subplots() img_1 = mpimg.imread(cmd_args.image_1_path) img_2 = mpimg.imread(cmd_args.image_2_path) inlier_idxs = np.nonzero(inliers)[0] plot_matches( ax, img_1, img_2, locations_1_to_use, locations_2_to_use, np.column_stack((inlier_idxs, inlier_idxs)), matches_color='b') ax.axis('off') ax.set_title('DELF correspondences') plt.savefig(cmd_args.output_image)
def plot_matches(src, dst, src_keypoints, dst_keypoints, matches_src_dst): fig, ax = plt.subplots(nrows=3, ncols=1) plt.gray() plot_matches(ax[0], src, dst, src_keypoints, dst_keypoints, matches_src_dst) ax[0].axis('off') plt.show()
def getDisplacement(Image0, Image1): Image0Gray = rgb2gray(Image0) Image1Gray = rgb2gray(Image1) descriptor_extractor = ORB(n_keypoints=200) descriptor_extractor.detect_and_extract(Image0Gray) keypoints1 = descriptor_extractor.keypoints descriptors1 = descriptor_extractor.descriptors descriptor_extractor.detect_and_extract(Image1Gray) keypoints2 = descriptor_extractor.keypoints descriptors2 = descriptor_extractor.descriptors matches12 = match_descriptors(descriptors1, descriptors2, cross_check=True) # Sort the matches based on distance. Least distance # is better distances12 = [] for match in matches12: distance = hamming(descriptors1[match[0]], descriptors2[match[1]]) distances12.append(distance) indices = np.range(len(matches12)) indices = [index for (_, index) in sorted(zip(distances12, indices))] matches12 = matches12[indices] # collect displacement from the first 10 matches dxList = [] dyList = [] for mat in matches12[:10]: # Get the matching keypoints for each of the images img1_idx = mat[0] img2_idx = mat[1] # x - columns # y - rows (x1, y1) = keypoints1[img1_idx] (x2, y2) = keypoints2[img2_idx] dxList.append(abs(x1 - x2)) dyList.append(abs(y1 - y2)) dxMedian = np.median(np.asarray(dxList, dtype=np.double)) dyMedian = np.median(np.asarray(dyList, dtype=np.double)) plot_matches(Image0, Image1, descriptors1, descriptors2, matches12[:10]) return dxMedian, dyMedian
def main(): p = Pool(5) listing = os.listdir('patterns') zipped_patterns = p.map(load_pattenrs, listing) zipped_patterns = [ent for sublist in zipped_patterns for ent in sublist] listing = os.listdir('scenes') zipped_scenes = p.map(load_scenes, listing) zipped_scenes = [ent for sublist in zipped_scenes for ent in sublist] zipped_patterns.sort(key=lambda x: x[3]) p_img, patterns, p_key, tmp = zip(*zipped_patterns) zipped_scenes.sort(key=lambda x: x[3]) s_img, scenes, s_key, tmp = zip(*zipped_scenes) set_names(len(zipped_patterns) - 1) k = 0 for j in scenes: arg_list = [] for a, b, c, d in zipped_patterns: arg_list.append([b, d, j]) zipped_matches = p.map(f_wrap, arg_list) zipped_matches = [ent for sublist in zipped_matches for ent in sublist] zipped_matches.sort(key=lambda x: x[1]) matches, tmp, m_array = zip(*zipped_matches) best_match = max(matches) proc = 1.0 id = 0 result = 'Karta to: ' for i in range(len(patterns)): el = abs((patterns[i].size/j.size) - 1) if matches[i] == best_match: result += cards[i] + " " if matches[i] == best_match and el < proc: proc = el id = i fig, ax = plt.subplots() plt.gray() plot_matches(ax, p_img[id], s_img[k], p_key[id], s_key[k], m_array[id]) ax.axis('off') plt.show() #print 'Karta to: ', cards[id] print result k += 1
keypoints3 = corner_peaks(corner_harris(img3), min_distance=5) extractor = BRIEF() extractor.extract(img1, keypoints1) keypoints1 = keypoints1[extractor.mask] descriptors1 = extractor.descriptors extractor.extract(img2, keypoints2) keypoints2 = keypoints2[extractor.mask] descriptors2 = extractor.descriptors extractor.extract(img3, keypoints3) keypoints3 = keypoints3[extractor.mask] descriptors3 = extractor.descriptors matches12 = match_descriptors(descriptors1, descriptors2, cross_check=True) matches13 = match_descriptors(descriptors1, descriptors3, cross_check=True) fig, ax = plt.subplots(nrows=2, ncols=1) plt.gray() plot_matches(ax[0], img1, img2, keypoints1, keypoints2, matches12) ax[0].axis('off') plot_matches(ax[1], img1, img3, keypoints1, keypoints3, matches13) ax[1].axis('off') plt.show()
# robustly estimate affine transform model with RANSAC model_robust, inliers = ransac((src, dst), AffineTransform, min_samples=3, residual_threshold=2, max_trials=100) outliers = inliers == False # compare "true" and estimated transform parameters print(tform.scale, tform.translation, tform.rotation) print(model.scale, model.translation, model.rotation) print(model_robust.scale, model_robust.translation, model_robust.rotation) # visualize correspondence fig, ax = plt.subplots(nrows=2, ncols=1) plt.gray() inlier_idxs = np.nonzero(inliers)[0] plot_matches(ax[0], img_orig_gray, img_warped_gray, src, dst, np.column_stack((inlier_idxs, inlier_idxs)), matches_color='b') ax[0].axis('off') ax[0].set_title('Correct correspondences') outlier_idxs = np.nonzero(outliers)[0] plot_matches(ax[1], img_orig_gray, img_warped_gray, src, dst, np.column_stack((outlier_idxs, outlier_idxs)), matches_color='r') ax[1].axis('off') ax[1].set_title('Faulty correspondences') plt.show()
img2 = rgb2gray(skimage.io.imread(imgPath + "banana/banana3.jpeg", plugin="pil")) descriptor_extractor = ORB(n_keypoints=200) descriptor_extractor.detect_and_extract(img1) keypoints1 = descriptor_extractor.keypoints descriptors1 = descriptor_extractor.descriptors descriptor_extractor.detect_and_extract(img2) keypoints2 = descriptor_extractor.keypoints descriptors2 = descriptor_extractor.descriptors # descriptor_extractor.detect_and_extract(img3) # keypoints3 = descriptor_extractor.keypoints # descriptors3 = descriptor_extractor.descriptors matches12 = match_descriptors(descriptors1, descriptors2, cross_check=True) # matches13 = match_descriptors(descriptors1, descriptors3, cross_check=True) fig, ax = plt.subplots(nrows=1, ncols=1) plt.gray() plot_matches(ax, img1, img2, keypoints1, keypoints2, matches12) ax.axis("off") # plot_matches(ax[1], img1, img3, keypoints1, keypoints3, matches13) # ax[1].axis('off') plt.show()
# Apply the ORB algorithm to our images descriptor_extractor.detect_and_extract(schroedinger) keypoints1 = descriptor_extractor.keypoints descriptors1 = descriptor_extractor.descriptors descriptor_extractor.detect_and_extract(schroedinger_rotate) keypoints2 = descriptor_extractor.keypoints descriptors2 = descriptor_extractor.descriptors descriptor_extractor.detect_and_extract(schroedinger_warped) keypoints3 = descriptor_extractor.keypoints descriptors3 = descriptor_extractor.descriptors # See which descriptors match across the images matches12 = match_descriptors(descriptors1, descriptors2, cross_check=True) matches13 = match_descriptors(descriptors1, descriptors3, cross_check=True) fig, ax = plt.subplots(nrows=2, ncols=1) plot_matches(ax[0], schroedinger, schroedinger_warped, keypoints1, keypoints2, matches12) ax[0].axis('off') plot_matches(ax[1], schroedinger, schroedinger_warped, keypoints1, keypoints3, matches13) ax[1].axis('off') plt.show() plt.gray()
def plot(img1, img2, keyp1, keyp2, matches): plot_matches(plt, img1, img2, keyp1, keyp2, matches) plt.show()
inlier_keypoints_left = keypoints_left[matches[inliers, 0]] inlier_keypoints_right = keypoints_right[matches[inliers, 1]] print("Number of matches:", matches.shape[0]) print("Number of inliers:", inliers.sum()) # Compare estimated sparse disparities to the dense ground-truth disparities. disp = inlier_keypoints_left[:, 1] - inlier_keypoints_right[:, 1] disp_coords = np.round(inlier_keypoints_left).astype(np.int64) disp_idxs = np.ravel_multi_index(disp_coords.T, groundtruth_disp.shape) disp_error = np.abs(groundtruth_disp.ravel()[disp_idxs] - disp) disp_error = disp_error[np.isfinite(disp_error)] # Visualize the results. fig, ax = plt.subplots(nrows=2, ncols=1) plt.gray() plot_matches(ax[0], img_left, img_right, keypoints_left, keypoints_right, matches[inliers], only_matches=True) ax[0].axis("off") ax[0].set_title("Inlier correspondences") ax[1].hist(disp_error) ax[1].set_title("Histogram of disparity errors") plt.show()