Exemple #1
0
def display_routine(min_score=0.8, ransaic_iter=75, inlier_tol=10):
    # Read the two images in such order so they will be aligned from left to right.
    # Make sure that you've put "read_image()" and "relpath()" from ex3 in sol4_utils.py.
    image1 = ut.read_image(ut.relpath("dump/boat/boat051.jpg"), 1)
    image2 = ut.read_image(ut.relpath("dump/boat/boat118.jpg"), 1)

    # For each image - find the harris detector points and their corresponding descriptors
    # using its gaussian pyramid.
    # Make sure you've put your build_guassian_pyramid func from ex3 in sol4_utils.py
    # along with accompanying functions.
    points1, desc1 = sol4.find_features(ut.build_gaussian_pyramid(image1, 3, 3)[0])
    points2, desc2 = sol4.find_features(ut.build_gaussian_pyramid(image2, 3, 3)[0])

    # Match the points between the two images.
    # If implemented correctly, m_indices1 and m_indices2 would hold the
    # corresponding indices in each descriptors arrays, such that:
    # m_indices1[i] would be the matching descriptor, and therefore point,
    # of m_indices2[i], for some i.
    # THERE MIGHT BE DUPLICATIONS, but the RANSAC algorithm would solve it.
    m_indices1, m_indices2 = sol4.match_features(desc1, desc2, min_score)
    p1, p2 = points1[m_indices1], points2[m_indices2]

    # Find for each point in p1 the most fitting point in p2, using RANSAC.
    # The inliers array hold the indices in p1, in which the inliers are located
    # according the algorithm.
    H, inliers = sol4.ransac_homography(p1, p2, ransaic_iter, inlier_tol)

    # Display the two images aligned, and the matching points from both images.
    # TIP: the x values of p2 need to be shifted to the right by image.shape[1]
    # in order to be displayed in the second image.
    sol4.display_matches(image1, image2, p1, p2, inliers)
    def test_structure_find_features(self):

        # Check structure
        self._structure_tester(sol.find_features, r'(pyr)', False, False)

        # Checks 'spread_out_corners' was used instead of 'harris_corner_detector'
        find_features_text = inspect.getsource(sol.find_features)
        self.assertTrue(
            r"spread_out_corners(" in find_features_text,
            msg=
            f"You should use 'spread_out_corners' and not use 'harris_corner_detector' in 'find_features' function"
        )
        self.assertTrue(
            r"harris_corner_detector(" not in find_features_text,
            msg=
            f"You should use 'spread_out_corners' and not use 'harris_corner_detector' in 'find_features' function"
        )

        # check return shape
        res = sol.find_features([
            np.ones((1024, 1024)),
            np.ones((512, 512)),
            np.ones((256, 256)),
            np.ones((128, 128))
        ])
        self.assertEqual(
            2,
            len(res),
            msg=
            f"'find_features' function's output should be an array of length 2"
        )
Exemple #3
0
def test_apply_hom():
    from sol4 import apply_homography
    print('Test hom')
    im1 = read_image('external/oxford1.jpg', 1)
    pyr1, f = build_gaussian_pyramid(im1, 3, 3)
    pos1, desc1 = sol4.find_features(pyr1)
    H = np.eye(3)
    pos2 = apply_homography(pos1, H)
    if np.any(pos1 != pos2): raise Exception("bad hommie")
    print('Hom ok')
Exemple #4
0
def test_matches():
    im1 = read_image('external/oxford1.jpg', 1)
    # im1 = read_image('external/oxford1.jpg', 1)[140:300,420:600]
    im2 = read_image('external/oxford2.jpg', 1)
    # im2 = read_image('external/oxford2.jpg', 1)[180:320,140:300]
    pyr1, f = build_gaussian_pyramid(im1, 3, 3)
    pyr2, f = build_gaussian_pyramid(im2, 3, 3)
    pos1, desc1 = sol4.find_features(pyr1)
    pos2, desc2 = sol4.find_features(pyr2)

    # pos1=np.fliplr(pos1)
    # pos2=np.fliplr(pos2)

    plt.figure()
    plt.subplot(2, 2, 1)
    plt.imshow(im1, cmap=plt.cm.gray)
    plt.scatter(pos1[:, 0], pos1[:, 1])
    plt.subplot(2, 2, 2)
    plt.imshow(im2, cmap=plt.cm.gray)
    plt.scatter(pos2[:, 0], pos2[:, 1])

    ind1, ind2 = sol4.match_features(desc1, desc2, 0.7)
    mpos1, mpos2 = pos1[ind1], pos2[ind2]

    plt.subplot(2, 2, 3)
    plt.imshow(im1, cmap=plt.cm.gray)
    plt.scatter(mpos1[:, 0], mpos1[:, 1])
    plt.subplot(2, 2, 4)
    plt.imshow(im2, cmap=plt.cm.gray)
    plt.scatter(mpos2[:, 0], mpos2[:, 1])

    # for i1, i2 in zip(ind1, ind2):
    #     print('Ind1: {0}, Ind2: {1}, DescSim: {2}'.format(i1, i2, np.dot(desc1[:,:,i1].flatten(),
    #                                                                          desc2[:,:,i2].flatten())))

    H, inliers = sol4.ransac_homography(mpos1, mpos2, 500, 7)
    sol4.display_matches(im1, im2, mpos1, mpos2, inliers)
Exemple #5
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 #6
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 #7
0
# #Img_1_feature_points = sol4.harris_corner_detector(Img_1)
# 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 #8
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)