def test_downscaling(data_dir, sizes, rectify=False): images_left = post_processing.load_images_cv(data_dir + 'left/', convert_to_grayscale=True) images_right = post_processing.load_images_cv(data_dir + 'right/', convert_to_grayscale=True) thetas = post_processing.load_poses(data_dir).theta good_indices = abs(thetas) < math.radians(20) good_thetas = thetas[good_indices] * math.degrees(1) plt.plot(good_thetas, label='GT') if rectify: info_left, info_right = load_camera_infos() stitched_images = np.array([ image_processing.rectify_stitch_stereo_image( image_left, image_right, info_left, info_right)[0] for image_left, image_right in zip(images_left, images_right) ]) fov = image_processing.rectify_stitch_stereo_image( images_left[0], images_right[0], info_left, info_right)[1] else: stitched_images = np.array([ image_processing.stitch_stereo_image(image_left, image_right) for image_left, image_right in zip(images_left, images_right) ]) fov = 175.2 rms_results = {'pixel': {}, 'subpixel': {}} for size in sizes: resized_images = patch_normalise_images( resize_images(stitched_images, 1. / size)) offsets = np.array([ image_processing.xcorr_match_images(resized_images[0], image)[0] for image in resized_images ]) offsets_deg = -offsets[good_indices] * fov / resized_images[0].shape[1] plt.plot(offsets_deg, label=str(size)) rms = np.sqrt(((offsets_deg - good_thetas)**2).mean()) rms_results['pixel'][str(size)] = rms print('%d: RMS = %f' % (size, rms)) if size != 1: offsets_subpixel = np.array([ image_processing.xcorr_match_images(resized_images[0], image, size)[0] for image in resized_images ]) offsets_subpixel_deg = -offsets_subpixel[ good_indices] * fov / resized_images[0].shape[1] / size plt.plot(offsets_subpixel_deg, '--', label=str(size) + '_sub') rms = np.sqrt(((offsets_subpixel_deg - good_thetas)**2).mean()) rms_results['subpixel'][str(size)] = rms print('%d_sub: RMS = %f' % (size, rms)) plt.legend() df = pd.DataFrame(rms_results) df = df.iloc[np.argsort([int(s) for s in list(df.index)])] df.plot(kind='bar')
def test_depth(data_dir, size, rectify=False): images_left = post_processing.load_images_cv(data_dir + 'left/', convert_to_grayscale=True) images_right = post_processing.load_images_cv(data_dir + 'right/', convert_to_grayscale=True) thetas = post_processing.load_poses(data_dir).theta depth_left = np.load(data_dir + 'left/000000_disp.npy').squeeze() depth_right = np.load(data_dir + 'right/000000_disp.npy').squeeze() good_indices = abs(thetas) < math.radians(20) good_thetas = thetas[good_indices] * math.degrees(1) plt.plot(good_thetas, label='GT') if rectify: info_left, info_right = load_camera_infos() stitched_images = np.array([ image_processing.rectify_stitch_stereo_image( image_left, image_right, info_left, info_right)[0] for image_left, image_right in zip(images_left, images_right) ]) depth = image_processing.rectify_stitch_stereo_image( depth_left, depth_right, info_left, info_right)[0] fov = image_processing.rectify_stitch_stereo_image( images_left[0], images_right[0], info_left, info_right)[1] else: stitched_images = np.array([ image_processing.stitch_stereo_image(image_left, image_right) for image_left, image_right in zip(images_left, images_right) ]) depth = image_processing.stitch_stereo_image(depth_left, depth_right) fov = 175.2 rms_results = {'depth': 0, 'no depth': 0} # for size in sizes: resized_images = patch_normalise_images( resize_images(stitched_images, 1. / size)) depth = cv2.resize(depth, resized_images[0].shape[::-1], interpolation=cv2.INTER_CUBIC) offsets = np.array([ image_processing.xcorr_match_images(resized_images[0], image)[0] for image in resized_images ]) offsets_deg = -offsets[good_indices] * fov / resized_images[0].shape[1] plt.plot(offsets_deg, label=str(size)) rms = np.sqrt(((offsets_deg - good_thetas)**2).mean()) rms_results['no depth'] = rms print('%d: RMS = %f' % (size, rms)) # if size != 1: offsets_depth = np.array([ image_processing.xcorr_match_images(depth * resized_images[0], image)[0] for image in resized_images ]) offsets_depth_deg = -offsets_depth[good_indices] * fov / resized_images[ 0].shape[1] plt.plot(offsets_depth_deg, '--', label=str(size) + ' depth') rms = np.sqrt(((offsets_depth_deg - good_thetas)**2).mean()) rms_results['depth'] = rms print('%d depth: RMS = %f' % (size, rms)) plt.legend() df = pd.DataFrame(rms_results, index=[size]) # df = df.iloc[np.argsort([int(s) for s in list(df.index)])] df.plot(kind='bar')
def test_full_path_offsets(data_dir): # if os.path.exists(test_dir + 'offset_data.npy'): # offsets_data = np.load(test_dir + 'offset_data.npy', allow_pickle=True)[()] # else: # offsets_data = {} info_left, info_right = load_camera_infos() images_left = post_processing.load_images_cv(data_dir + 'left/', convert_to_grayscale=True) images_right = post_processing.load_images_cv(data_dir + 'right/', convert_to_grayscale=True) size = (44, 115) stitched_images = np.array([ resize_images([ image_processing.rectify_stitch_stereo_image( image_left, image_right, info_left, info_right)[0] ], size)[0] for image_left, image_right in zip(images_left, images_right) ]) stitched_images_fov = image_processing.rectify_stitch_stereo_image( images_left[0], images_right[0], info_left, info_right)[1] stitched_images_fov = cv2.resize(stitched_images_fov.reshape( -1, 1), (1, stitched_images[0].shape[1])).flatten() stitched_images_fov -= stitched_images_fov[(stitched_images_fov.size - 1) / 2] stitched_images_original = np.array([ resize_images( [image_processing.stitch_stereo_image(image_left, image_right)], size)[0] for image_left, image_right in zip(images_left, images_right) ]) thetas = post_processing.load_poses(data_dir).theta offsets = np.array([ image_processing.xcorr_match_images(stitched_images[0], image)[0] for image in stitched_images ]) offsets = [ stitched_images_fov[offset + (stitched_images_fov.size - 1) / 2] for offset in offsets ] # offsets_2 = np.array([image_processing.xcorr_match_images(stitched_images[0], image, 10)[0] for image in stitched_images]) offsets_original = np.array([ image_processing.xcorr_match_images(stitched_images_original[0], image)[0] for image in stitched_images_original ]) n = 90 debug_image1 = image_processing.xcorr_match_images_debug( stitched_images[0], stitched_images[n])[2] debug_image2 = image_processing.xcorr_match_images_debug( stitched_images_original[0], stitched_images_original[n])[2] debug_image = cv2.resize(np.hstack((debug_image1, debug_image2)), None, fx=4, fy=4, interpolation=cv2.INTER_NEAREST) # cv2.imshow('a', 0.5+0.5*small_images[5]) cv2.imshow('a', debug_image) cv2.waitKey() # plt.plot(thetas * math.degrees(1)) plt.plot(offsets) # plt.plot(-offsets_2 * stitched_images_fov / stitched_images[0].shape[1] / 10.) # plt.plot(-offsets_left * 175.2 / stitched_images[0].shape[1]) # plt.plot(-offsets_right * 175.2 / stitched_images[0].shape[1]) plt.plot(-offsets_original * 175.2 / stitched_images[0].shape[1])
def test_full_path_offsets(ref_dir, test_dir, sizes_to_test): if os.path.exists(test_dir + 'offset_data.npy'): offsets_data = np.load(test_dir + 'offset_data.npy', allow_pickle=True)[()] else: offsets_data = {} test_keyframes = post_processing.get_image_keyframes(post_processing.load_images_cv(test_dir + 'norm/'), post_processing.load_images_cv(test_dir)) test_images = post_processing.load_images_cv(test_dir + 'norm/') test_keyframe_correspondances = np.array([np.argmin([np.sum(np.abs(keyframe-test_image)) for test_image in test_images]) for keyframe in test_keyframes]) correspondances_full = post_processing.get_full_correspondances(test_keyframe_correspondances, test_images.shape[0]) ref_full = post_processing.get_sorted_files_by_ending(ref_dir+'full/', '.png') test_full = post_processing.get_sorted_files_by_ending(test_dir+'full/', '.png') for size in sizes_to_test: if str(size) in offsets_data: continue offsets_data[str(size)] = [] if size != 1: offsets_data[str(size)+'_sub'] = [] print('calculating size %d' % size) for i, c in enumerate(correspondances_full): if i % 10 == 0: print('%d/%d' % (i, len(correspondances_full))) ref_image = cv2.imread(ref_full[c], cv2.IMREAD_GRAYSCALE) test_image = cv2.imread(test_full[i], cv2.IMREAD_GRAYSCALE) if size != 1: ref_image, test_image = resize_images([ref_image, test_image], 1./size) # ref_image, test_image = patch_normalise_images(resize_images([ref_image, test_image], 1./size), (9,9)) ref_image = np.pad(ref_image, ((0,),(int(test_image.shape[1]/2),)), mode='constant', constant_values=0) corr = image_processing.normxcorr2_subpixel(ref_image, test_image, size) offset = np.argmax(corr) - (len(corr)-1)/2 if size == 1: offsets_data[str(size)].append(offset) else: offset_not_sub = size * np.argmax(corr[::size]) - (len(corr)-1)/2 offsets_data[str(size)+'_sub'].append(offset) offsets_data[str(size)].append(offset_not_sub) np.save(test_dir+'offset_data.npy', offsets_data) plt.figure() for size in sizes_to_test: plt.plot(offsets_data[str(size)], label=str(size)) if size != 1: plt.plot(offsets_data[str(size)+'_sub'], label=str(size)+'_sub') plt.legend() pixel_accuracy_rms = collections.OrderedDict() subpixel_accuracy_rms = collections.OrderedDict() for size in sizes_to_test: if size != 1: not_sub_rms = math.sqrt(((np.array(offsets_data[str(size)]) - np.array(offsets_data['1']))**2).mean()) sub_rms = math.sqrt(((np.array(offsets_data[str(size)+'_sub']) - np.array(offsets_data['1']))**2).mean()) print('RMS [%d]:\tdiscrete: %.3f\tsubpixel: %.3f' % (size, not_sub_rms, sub_rms)) im_size = '[%dx%d]' % (round(925./size), round(360./size)) pixel_accuracy_rms[im_size] = not_sub_rms subpixel_accuracy_rms[im_size] = sub_rms # plt.figure() mod_offset = 1. / size * ((np.array(offsets_data['1'])) % size) mod_offset_05 = 1. / size * ((np.array(offsets_data['1']) + size/2.) % size) - 0.5 sub_err = np.float64(offsets_data[str(size)+'_sub']) - np.array(offsets_data['1']) px_err = np.float64(offsets_data[str(size)]) - np.array(offsets_data['1']) bad_err = abs(sub_err / px_err) bad_err[px_err == 0] = 0 # plt.plot(mod_offset) # plt.scatter(mod_offset_05, sub_err, alpha=0.2) # plt.scatter(mod_offset_05, px_err) # plt.plot(bad_err, '.') # plt.plot(offsets_data[str(size)] - np.array(offsets_data['1'])) n = np.argmax(bad_err) print(n) bad_ref = np.float64(cv2.imread(ref_full[correspondances_full[n]], cv2.IMREAD_GRAYSCALE)) bad_test = np.float64(cv2.imread(test_full[n], cv2.IMREAD_GRAYSCALE)) off, corr, img = image_processing.xcorr_match_images_debug(bad_ref, bad_test, 1) bad_ref, bad_test = patch_normalise_images(resize_images([bad_ref, bad_test], 1./size), (9,9)) off2, corr2, img2 = image_processing.xcorr_match_images_debug(bad_ref, bad_test, 1) print(off, off2) print(offsets_data[str(size)+'_sub'][n], offsets_data['1'][n]) # cv2.imshow('full',cv2.resize(img,None,fx=1.0,fy=0.5,interpolation=cv2.INTER_NEAREST)) cv2.imshow('sub',cv2.resize(img2,None,fx=4,fy=4,interpolation=cv2.INTER_NEAREST)) cv2.waitKey() combined = collections.OrderedDict((('pixel accuracy', pixel_accuracy_rms), ('subpixel accuracy', subpixel_accuracy_rms))) df = pd.DataFrame(combined) df = df.iloc[np.argsort([int(s[1:s.index('x')]) for s in list(df.index)])[::-1]] df.plot(kind='bar') plt.xticks(rotation=0) plt.title('Correlation error vs full-res image') plt.ylabel('RMS error (px)')