def align_images(self, translation_only=False):
        """
        compute homographies between all images to a common coordinate system
        :param translation_only: see estimte_rigid_transform
        """
        # Extract feature point locations and descriptors.
        points_and_descriptors = []
        for file in self.files:
            image = sol4_utils.read_image(file, 1)
            self.h, self.w = image.shape
            pyramid, _ = sol4_utils.build_gaussian_pyramid(image, 3, 7)
            points_and_descriptors.append(find_features(pyramid))

        # Compute homographies between successive pairs of images.
        Hs = []
        for i in range(len(points_and_descriptors) - 1):
            points1, points2 = points_and_descriptors[i][0], \
                               points_and_descriptors[i + 1][0]
            desc1, desc2 = points_and_descriptors[i][1], \
                           points_and_descriptors[i + 1][1]

            # Find matching feature points.
            ind1, ind2 = match_features(desc1, desc2, .7)
            points1, points2 = points1[ind1, :], points2[ind2, :]

            # Compute homography using RANSAC.
            H12, inliers = ransac_homography(points1, points2, 100, 6,
                                             translation_only)

            # Uncomment for debugging: display inliers and outliers among matching points.
            # In the submitted code this function should be commented out!
            # display_matches(self.images[i], self.images[i+1], points1 , points2, inliers)

            Hs.append(H12)

        # Compute composite homographies from the central coordinate system.
        accumulated_homographies = accumulate_homographies(
            Hs, (len(Hs) - 1) // 2)
        self.homographies = np.stack(accumulated_homographies)
        self.frames_for_panoramas = filter_homographies_with_translation(
            self.homographies, minimum_right_translation=5)
        self.homographies = self.homographies[self.frames_for_panoramas]
Exemple #2
0
def test_disp_points():
    from sol4 import map_coord_2_level, sample_descriptor, get_windows_coords
    from scipy.ndimage import map_coordinates
    im1 = read_image('external/oxford1.jpg', 1)
    pyr1, f = build_gaussian_pyramid(im1, 3, 3)
    pos1, desc1 = sol4.find_features(pyr1)
    ind = np.random.choice(np.arange(pos1.shape[0]), 15)
    pos2 = map_coord_2_level(pos1[ind])
    plt.figure()
    plt.subplot(1, 2, 1)
    plt.imshow(pyr1[0], cmap=plt.cm.gray)
    plt.scatter(pos1[ind][:, 1], pos1[ind][:, 0])
    plt.subplot(1, 2, 2)
    plt.imshow(pyr1[2], cmap=plt.cm.gray)
    plt.scatter(pos2[:, 1], pos2[:, 0])

    plt.figure()
    for i in range(1, 16):
        plt.subplot(3, 5, i)
        plt.imshow(desc1[:, :, ind[i - 1]], cmap=plt.cm.gray)
        plt.title('X:{0}, Y:{1}'.format(pos2[i - 1, 1], pos2[i - 1, 0]))
    plt.show(block=True)
Exemple #3
0
def test_harris_detector():
    im = read_image('external/oxford2.jpg', 1)
    plt.figure()
    res = spread_out_corners(im, 7, 7, 15)
    plt.imshow(im, cmap=plt.cm.gray)
    plt.scatter(res[:, 0], res[:, 1])
    # plt.show(block=True)
    for pth in [
            'external/oxford2.jpg', 'external/office1.jpg',
            'external/office2.jpg'
    ]:
        plt.figure()
        im = read_image(pth, 1)
        res = sol4.harris_corner_detector(im)
        plt.imshow(im, cmap=plt.cm.gray)
        plt.scatter(res[:, 0], res[:, 1])

        pyr, f = build_gaussian_pyramid(im, 3, 3)
        plt.figure()
        im = pyr[2]
        res = sol4.harris_corner_detector(im)
        plt.imshow(im, cmap=plt.cm.gray)
        plt.scatter(res[:, 0], res[:, 1])
    plt.show(block=True)
Exemple #4
0
def test_desc():
    from sol4 import map_coord_2_level, sample_descriptor, get_windows_coords
    from scipy.ndimage import map_coordinates
    im1 = read_image('external/oxford1.jpg', 1)
    pyr1, f = build_gaussian_pyramid(im1, 3, 3)
    pos1, desc1 = sol4.find_features(pyr1)
    ind = np.random.choice(np.arange(pos1.shape[0]), 15)
    pos2 = map_coord_2_level(pos1[ind])
    desc2 = sample_descriptor(pyr1[2], pos2, 3)
    if np.any(desc1[:, :, ind] != desc2): raise Exception("Wrong desc")

    coord_window = get_windows_coords(pos2[0][np.newaxis, :], 3)
    desc = map_coordinates(pyr1[2], coord_window)
    desc -= np.mean(desc)
    desc /= np.linalg.norm(desc)
    if not np.isclose(1., np.dot(desc, desc1[:, :, ind[0]].flatten())):
        raise Exception("Bad dot: {0}".format(
            np.dot(desc, desc1[:, :, ind[0]].flatten())))

    pos2 = map_coord_2_level(pos1)
    desc2 = sample_descriptor(pyr1[2], pos2, 3)
    if np.any(desc1[:, :, :] != desc2[:, :, :]):
        raise Exception("Wrong desc 2")
    print('ok')
Exemple #5
0
# Img_2_feature_points = sol4_add.spread_out_corners(Img_2, 7, 7, 7)
# Img_2_feature_points_x = [x[0] for x in Img_2_feature_points]
# Img_2_feature_points_y = [y[1] for y in Img_2_feature_points]
# #Img_2_feature_points = sol4.harris_corner_detector(Img_2)
#
# fig = plt.figure("TEST 3.1 - Harris")
# sub = fig.add_subplot(121, title='Im_1')
# plt.imshow(Img_1, cmap=plt.cm.gray)
# plt.scatter(Img_1_feature_points_x, Img_1_feature_points_y)
# sub = fig.add_subplot(122, title='Im_2')
# plt.imshow(Img_2, cmap=plt.cm.gray)
# plt.scatter(Img_2_feature_points_x, Img_2_feature_points_y)

### TEST 3.2 ###
Img_1_descriptor = sol4.find_features(
    sol4_utils.build_gaussian_pyramid(Img_1, 3, 7)[0])  #TODO filter_size?
Img_2_descriptor = sol4.find_features(
    sol4_utils.build_gaussian_pyramid(Img_2, 3, 7)[0])  #TODO filter_size?
Img_3_descriptor = sol4.find_features(
    sol4_utils.build_gaussian_pyramid(Img_3, 3, 7)[0])  #TODO filter_size?

min_score = 0.4
match_ind1, match_ind2 = sol4.match_features(Img_1_descriptor[1],
                                             Img_2_descriptor[1], min_score)
Img_1_matched_indexes = Img_1_descriptor[0][match_ind1]
Img_1_feature_points_x = [x[0] for x in Img_1_matched_indexes]
Img_1_feature_points_y = [y[1] for y in Img_1_matched_indexes]
Img_2_matched_indexes = Img_2_descriptor[0][match_ind2]
Img_2_feature_points_x = [x[0] for x in Img_2_matched_indexes]
Img_2_feature_points_y = [y[1] for y in Img_2_matched_indexes]
match_ind2_2, match_ind3_2 = sol4.match_features(Img_2_descriptor[1],
Exemple #6
0
    s = 2**np.max(size)
    mask = np.ones((s, s))
    mask[:, left:right] = 0
    panorama_new = np.zeros(mask.shape)
    panorama_new[:panorama.shape[0], :panorama.shape[1]] = panorama
    panorama_part = np.zeros(mask.shape)
    panorama_part[:part.shape[0], left:right] = part
    p = sol4_utils.pyramid_blending(panorama_new, panorama_part, mask, 7, 3, 3)
    return p[:panorama.shape[0], :panorama.shape[1]]


im1 = sol4_utils.read_image('external/backyard1.jpg', 1)
im2 = sol4_utils.read_image('external/backyard2.jpg', 1)
im3 = sol4_utils.read_image('external/backyard3.jpg', 1)

pyr1, filter_vec1 = sol4_utils.build_gaussian_pyramid(im1, 3, 3)
pos1, desc1 = find_features(pyr1)

pyr2, filter_vec2 = sol4_utils.build_gaussian_pyramid(im2, 3, 3)
pos2, desc2 = find_features(pyr2)

pyr3, filter_vec3 = sol4_utils.build_gaussian_pyramid(im3, 3, 3)
pos3, desc3 = find_features(pyr3)

match_ind1, match_ind2 = match_features(desc1, desc2, 0)
p1 = pos1[match_ind1]
p2 = pos2[match_ind2]
H12, inliers12 = ransac_homography(p1, p2, 500, 6)
# display_matches(im1,im2,p1,p2,inliers12)

match_ind3, match_ind4 = match_features(desc2, desc3, 0)
Exemple #7
0
def presubmit():
    print('ex4 presubmission script')
    disclaimer = """
    Disclaimer
    ----------
    The purpose of this script is to make sure that your code is compliant
    with the exercise API and some of the requirements
    The script does not test the quality of your results.
    Don't assume that passing this script will guarantee that you will get
    a high grade in the exercise
    """
    print(disclaimer)

    print('=== Check Submission ===\n')
    # cwd = os.getcwd()
    # print(cwd)
    if os.path.exists('README'):
        readme_file = 'README'
    elif os.path.exists('README.md'):
        readme_file = 'README.md'
    else:
        print('No readme!')
        return False
    with open(readme_file) as f:
        lines = f.readlines()
    print('login: '******'submitted files:\n' +
          '\n'.join(map(lambda x: x.strip(), lines[1:])))

    print('\n=== Bonus submittes? ===')
    if os.path.exists('bonus.txt'):
        print('yes, algorithm description:')
        with open('bonus.txt') as f:
            print(f.read())
    else:
        print('no')

    print('\n=== Section 3.1 ===\n')
    im = sol4_utils.read_image('external/oxford1.jpg', 1)
    try:
        im1 = im[200:300, 200:400]
        im2 = im[200:300, 300:500]
        print('Harris corner detector...')
        pos = sol4.harris_corner_detector(im1)
        print('\tPassed!')
        print('Checking structure...')
        if pos.ndim != 2 or pos.shape[1] != 2:
            raise ValueError(
                'Incorrect shape of harris corner returned value.')
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    try:
        print('Sample descriptor')
        print('Trying to build Gaussian pyramid...')
        pyr, _ = sol4_utils.build_gaussian_pyramid(im1, 3, 3)
        print('\tPassed!')
        print(
            'Sample descriptor at the third level of the Gaussian pyramid...')
        rad = 3
        pos = np.array([[20, 25], [30, 35], [40, 45]])
        desc = sol4.sample_descriptor(pyr[2], pos, rad)

        print('Checking the descriptor type and structure...')
        if desc.dtype != np.float64:
            raise ValueError(
                'Descriptor\' type is not float64. It is %s instead.' %
                str(desc.dtype))
        if desc.ndim != 3 or desc.shape != (pos.shape[0], rad * 2 + 1,
                                            rad * 2 + 1):
            raise ValueError('Wrong shape or length of the Descriptor.')

        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    try:
        print('Find features.')
        pos1, desc1 = sol4.find_features(pyr)
        if pos1.ndim != 2 or pos1.shape[1] != 2:
            raise ValueError(
                'Incorrect shape of harris corner returned value.')
        if desc1.dtype != np.float64:
            raise ValueError('Descriptor\' type is not float64.')
        if desc1.ndim != 3 or desc1.shape != (pos1.shape[0], rad * 2 + 1,
                                              rad * 2 + 1):
            raise ValueError('Wrong shape or length of the Descriptor.')
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    print('\n=== Section 3.2 ===\n')
    try:
        print('Match Features')
        #        im2 = sol4_utils.read_image('presubmit_externals/oxford2.jpg', 1)
        pyr2, _ = sol4_utils.build_gaussian_pyramid(im2, 3, 3)
        pos2, desc2 = sol4.find_features(pyr2)
        match_ind1, match_ind2 = sol4.match_features(desc1, desc2, .5)
        print('\tPassed!')

        if match_ind1.ndim != 1 or not np.all(
                match_ind1.shape == match_ind2.shape):
            raise ValueError(
                'matching indices 1 and 2 should have the same length.')
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    print('\n=== Section 3.3 ===\n')
    try:
        print('Compute and apply homography')
        pos1 = pos1[match_ind1, :]
        pos2 = pos2[match_ind2, :]
        H, inliers = sol4.ransac_homography(pos1, pos2, 1000, 10)

        if H.shape != (3, 3):
            raise ValueError('homography should have shape (3,3)')
        if not np.isclose(H[-1, -1], 1):
            raise ValueError('homography should be normalized')
        if inliers.ndim != 1:
            raise ValueError('inliers should have shape (S,)')

        pos1_ = sol4.apply_homography(pos1, H)
        print('\tPassed!')
        if pos1_.ndim != 2 or pos1_.shape[1] != 2:
            raise ValueError(
                'Incorrect shape of points after apply homography.')

        print('display matches')
        sol4.display_matches(im1, im2, pos1, pos2, inliers)
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    print('\n=== Section 3.4 ===\n')
    try:
        print('Accumulate homographies')
        H2m = sol4.accumulate_homographies([H], 0)
        if type(H2m) is not list:
            raise ValueError(
                'Returned value from accumulate_homographies  should be a list!'
            )
        if any([h.shape != (3, 3) for h in H2m]):
            raise ValueError(
                'accumulate_homographies should return a list of 3X3 homographies!'
            )
        if len(H2m) != 2:
            raise ValueError(
                'accumulate_homographies should return a list of length equal to the number of input images!'
            )
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    print('\n=== Section 4.1 ===\n')
    try:
        print('Warp grayscale image')
        warped_image = sol4.warp_channel(im1, H2m[0])
        if warped_image.dtype != np.float64:
            raise ValueError('warped_image is not float64. It is %s instead.' %
                             str(warped_image.dtype))
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    try:
        print('Compute bounding box')
        bounding_box = sol4.compute_bounding_box(H2m[0], im1.shape[1],
                                                 im1.shape[0])
        if bounding_box.dtype != np.int:
            raise ValueError('bounding_box is not int. It is %s instead.' %
                             str(bounding_box.dtype))
        if not np.allclose(bounding_box.shape, [2, 2]):
            raise ValueError(
                'bounding_box shape is not 2x2. It is %s instead.' %
                str(bounding_box.shape))
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    print('\n=== All tests have passed ===')
    print('=== Pre-submission script done ===\n')

    print("""
    Please go over the output and verify that there are no failures/warnings.
    Remember that this script tested only some basic technical aspects of your implementation
    It is your responsibility to make sure your results are actually correct and not only
    technically valid.""")
    return True
 def im_to_points(im):
     pyr, _ = sol4_utils.build_gaussian_pyramid(im, 3, 7)
     return sol4.find_features(pyr)
Exemple #9
0
def im_to_points(im):
    #############################################################
    # implements the function in example_panoramas.py
    #############################################################
    pyr, vec = sut.build_gaussian_pyramid(im, 3, 3)
    return find_features(pyr)
Exemple #10
0
                  (out_folder, self.file_prefix))

    def show_panorama(self, panorama_index, figsize=(20, 20)):
        assert self.panoramas is not None
        plt.figure(figsize=figsize)
        plt.imshow(self.panoramas[panorama_index].clip(0, 1))
        plt.show()


if __name__ == '__main__':
    # # Read images
    im_oxford1 = sol4_utils.read_image('external/oxford1.jpg', 1)
    im_oxford2 = sol4_utils.read_image('external/oxford2.jpg', 1)

    # Points and descriptors
    pyr1, filter_vec1 = sol4_utils.build_gaussian_pyramid(im_oxford1, 3, 5)
    points1, descriptors1 = find_features(pyr1)
    pyr2, filter_vec2 = sol4_utils.build_gaussian_pyramid(im_oxford2, 3, 5)
    points2, descriptors2 = find_features(pyr2)

    # Match features
    indices1, indices2 = match_features(descriptors1, descriptors2, 0.5)
    interest1, interest2 = points1[indices1], points2[indices2]

    # Show images and display matches
    plt.imshow(im_oxford1, cmap='gray')
    plt.scatter(interest1[:, 0], interest1[:, 1], marker='.', c='r')
    plt.show()

    plt.imshow(im_oxford2, cmap='gray')
    plt.scatter(interest2[:, 0], interest2[:, 1], marker='.', c='r')