예제 #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)
예제 #2
0
def generate_panorama(data_dir, file_prefix, num_images, figsize=(20, 20)):
    # The naming convention for a sequence of images is nameN.jpg, where N is a running number 1,2,..
    files = [
        os.path.join(data_dir, '%s%d.jpg' % (file_prefix, i + 1))
        for i in range(num_images)
    ]

    # Read images.
    ims = [sol4_utils.read_image(f, 1) for f in files]

    # Extract feature point locations and descriptors.
    def im_to_points(im):
        pyr, _ = sol4_utils.build_gaussian_pyramid(im, 3, 7)
        return sol4.find_features(pyr)

    p_d = [im_to_points(im) for im in ims]

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

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

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

        # Display inlier and outlier matches.
        sol4.display_matches(ims[i],
                             ims[i + 1],
                             points1,
                             points2,
                             inliers=inliers)
        Hs.append(H12)

    # Compute composite homographies from the panorama coordinate system.
    Htot = sol4.accumulate_homographies(Hs, (num_images - 1) // 2)

    # Final panorama is generated using 3 channels of the RGB images
    ims_rgb = [sol4_utils.read_image(f, 2) for f in files]

    # Render panorama for each color channel and combine them.
    panorama = [
        sol4.render_panorama([im[..., i] for im in ims_rgb], Htot)
        for i in range(3)
    ]
    panorama = np.dstack(panorama)

    #plot the panorama
    plt.imsave('external/kitchen.jpg', panorama)
    plt.figure(figsize=figsize)
    plt.imshow(panorama.clip(0, 1))
    plt.show()
예제 #3
0
 def align_images(self, translation_only = False):
     """
     compute homographies between all images to a common coordinate system
     :param translation_only: see estimte_rigid_transform
     """
     points_and_descriptors = []
     for i, file in enumerate(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))
     h_s = []
     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]
         ind1, ind2 = match_features(desc1, desc2, .7)
         points1, points2 = points1[ind1, :], points2[ind2, :]
         h12, inliers = ransac_homography(points1, points2, 200, 6, translation_only)
         # Uncomment for debugging: display inliers and outliers among matching points.
         # In the submitted code this function should be commented out!
         h_s.append(h12)
     accumulated_homographies = accumulate_homographies(h_s, (len(h_s) - 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]
    def generate_panoramic_images(self, number_of_panoramas):
        """
        combine slices from input images to panoramas.
        :param number_of_panoramas: how many different slices to take from each input image
        """
        assert self.homographies is not None

        # compute bounding boxes of all warped input images in the coordinate system of the middle image (as given by the homographies)
        self.bounding_boxes = np.zeros((self.frames_for_panoramas.size, 2, 2))
        for i in range(self.frames_for_panoramas.size):
            self.bounding_boxes[i] = compute_bounding_box(self.homographies[i], self.w, self.h)

        # change our reference coordinate system to the panoramas
        # all panoramas share the same coordinate system
        global_offset = np.min(self.bounding_boxes, axis=(0, 1))
        self.bounding_boxes -= global_offset

        slice_centers = np.linspace(0, self.w, number_of_panoramas + 2, endpoint=True, dtype=np.int)[1:-1]
        warped_slice_centers = np.zeros((number_of_panoramas, self.frames_for_panoramas.size))
        # every slice is a different panorama, it indicates the slices of the input images from which the panorama
        # will be concatenated
        for i in range(slice_centers.size):
            slice_center_2d = np.array([slice_centers[i], self.h // 2])[None, :]
            # homography warps the slice center to the coordinate system of the middle image
            warped_centers = [apply_homography(slice_center_2d, h) for h in self.homographies]
            # we are actually only interested in the x coordinate of each slice center in the panoramas' coordinate system
            warped_slice_centers[i] = np.array(warped_centers)[:, :, 0].squeeze() - global_offset[0]

        panorama_size = np.max(self.bounding_boxes, axis=(0, 1)).astype(np.int) + 1

        # boundary between input images in the panorama
        x_strip_boundary = ((warped_slice_centers[:, :-1] + warped_slice_centers[:, 1:]) / 2)
        x_strip_boundary = np.hstack([np.zeros((number_of_panoramas, 1)),
                                      x_strip_boundary,
                                      np.ones((number_of_panoramas, 1)) * panorama_size[0]])
        x_strip_boundary = x_strip_boundary.round().astype(np.int)

        self.panoramas = np.zeros((number_of_panoramas, panorama_size[1], panorama_size[0], 3), dtype=np.float64)
        for i, frame_index in enumerate(self.frames_for_panoramas):
            # warp every input image once, and populate all panoramas
            image = sol4_utils.read_image(self.files[frame_index], 2)
            warped_image = warp_image(image, self.homographies[i])
            x_offset, y_offset = self.bounding_boxes[i][0].astype(np.int)
            y_bottom = y_offset + warped_image.shape[0]

            for panorama_index in range(number_of_panoramas):
                # take strip of warped image and paste to current panorama
                boundaries = x_strip_boundary[panorama_index, i:i + 2]
                image_strip = warped_image[:, boundaries[0] - x_offset: boundaries[1] - x_offset]
                x_end = boundaries[0] + image_strip.shape[1]
                self.panoramas[panorama_index, y_offset:y_bottom, boundaries[0]:x_end] = image_strip

        # crop out areas not recorded from enough angles
        # assert will fail if there is overlap in field of view between the left most image and the right most image
        crop_left = int(self.bounding_boxes[0][1, 0])
        crop_right = int(self.bounding_boxes[-1][0, 0])
        assert crop_left < crop_right, 'for testing your code with a few images do not crop.'
        print(crop_left, crop_right)
        self.panoramas = self.panoramas[:, :, crop_left:crop_right, :]
예제 #5
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')
예제 #6
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)
    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.
        # print('here')
        points_and_descriptors = []
        for file in self.files:
            # print('kaka')
            image = sol4_utils.read_image(file, 1)
            # print('bla',image)
            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.
            # print(points1)
            # print(points2)
            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.
        # print(Hs)
        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]
예제 #8
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)
예제 #9
0
    def __init__(self, data_dir, file_prefix, num_images):
        """
    The naming convention for a sequence of images is file_prefixN.jpg,
    where N is a running number 001, 002, 003...
    :param data_dir: path to input images.
    :param file_prefix: see above.
    :param num_images: number of images to produce the panoramas with.
    """
        self.file_prefix = file_prefix
        self.files = [
            os.path.join(data_dir, '%s%03d.jpg' % (file_prefix, i + 1))
            for i in range(num_images)
        ]
        self.files = list(filter(os.path.exists, self.files))
        self.panoramas = None
        self.homographies = None
        print('found %d images' % len(self.files))

        ######################
        self.images = []
        for file in self.files:
            self.images.append(sol4_utils.read_image(file, 1))
예제 #10
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)
예제 #11
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')
예제 #12
0

def blend(panorama, part, left, right):
    size = (np.around(np.log2(panorama.shape))).astype(np.int32)
    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]
예제 #13
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
예제 #14
0
        if os.path.exists('%s.mp4' % self.file_prefix):
            os.remove('%s.mp4' % self.file_prefix)
        # write output video to current folder
        os.system('ffmpeg -framerate 3 -i %s/panorama%%02d.png %s.mp4' %
                  (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')
예제 #15
0
def main():
    # a = np.array([[2.,3.,6.], [5.,6.,1.], [3.,8.,9.]])
    # print a
    # print map_coordinates(a, [[0,1], [2,0]])
    #
    im = sut.read_image('external/backyard1.jpg', 1)

    H = np.array([[1, 1, 2], [1, 3, 1], [9, 1, 1]])
    a = [[2, 3], [4, 5], [8, 8]]
    b = np.column_stack((a, np.ones(len(a))))
    c = np.dot(H, b.T).T
    d = (c.T / c[:, 2]).T
    e = np.delete(d, -1, axis=1)

    n = np.arange(9).reshape(3, 3)
    m = np.arange(9).reshape(3, 3) + 1
    x = m + 6

    v = np.array([n, m, x])

    # pos = sad.spread_out_corners(im, 7, 7, 40)
    # plt.imshow(im, 'gray')
    # plt.scatter(pos[:,0], pos[:,1])
    # plt.show()
    # pos = harris_corner_detector(im)
    #
    # pyr, vec = sut.build_gaussian_pyramid(im, 3, 3)
    #
    # # for i in pyr:
    # #     plt.imshow(i, 'gray')
    # #     plt.show()
    #
    # desc = sample_descriptor(pyr[2], pos, 3)

    # b = sample_descriptor(a, np.array([[3,3]]), 1)
    # print a; print b[:,:,0];

    # plt.imshow(im, 'gray')
    # for i in range(desc.shape[2]):
    #     print desc[:,:,i].shape
    #     plt.imshow(desc[:,:,i], 'gray')
    #     plt.show()

    window_x_before = 0
    window_x_after = 2
    window_y_before = 0
    window_y_after = 2

    # ccc = np.empty((7,7,400))
    # print ccc[:, :, 0].shape

    x = np.arange(window_x_before, window_x_after + 1).astype(np.float32) / 2
    y = np.arange(window_y_before, window_y_after + 1).astype(np.float32) / 2

    indexes = np.transpose([np.tile(x, len(y)), np.repeat(y, len(x))])
    # print indexes
    # print a; print b
    # print np.concatenate((a, b), axis=1)
    # print map_coordinates(a[:,:,0], [indexes[:,1], indexes[:,0]], order=1, prefilter=False).reshape(3,3)

    a = np.array([[[1, 2, 3], [1, 2, 4], [3, 3, 3]],
                  [[1, 2, 3], [1, 2, 4], [3, 3, 3]],
                  [[1, 2, 3], [1, 2, 4], [3, 3, 3]]])
    b = np.array([[[2, 3, 1], [2, 2, 2], [1, 1, 1]],
                  [[2, 3, 1], [2, 2, 2], [1, 1, 1]],
                  [[2, 3, 1], [2, 2, 2], [1, 1, 1]]])
예제 #16
0
import sol4
import sol4_utils
import sol4_add
import numpy as np
import matplotlib.pyplot as plt

REPRESENTATION_GRAYSCALE = 1
REPRESENTATION_RGB = 2

Img_1 = sol4_utils.read_image("external/backyard1.jpg",
                              REPRESENTATION_GRAYSCALE)
Img_2 = sol4_utils.read_image("external/backyard2.jpg",
                              REPRESENTATION_GRAYSCALE)
Img_3 = sol4_utils.read_image("external/backyard3.jpg",
                              REPRESENTATION_GRAYSCALE)

### TEST 3.1 ###
# Img_1_feature_points = sol4_add.spread_out_corners(Img_1, 7, 7, 7)
# Img_1_feature_points_x = [x[0] for x in Img_1_feature_points]
# Img_1_feature_points_y = [y[1] for y in Img_1_feature_points]
# #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')
예제 #17
0
파일: init.py 프로젝트: ShaulRo1/SLAM
    """
    assert ((titles is None) or (len(images) == len(titles)))
    n_images = len(images)
    if titles is None: titles = ['Image (%d)' % i for i in range(1, n_images + 1)]
    fig = plt.figure()
    for n, (image, title) in enumerate(zip(images, titles)):
        a = fig.add_subplot(cols, np.ceil(n_images / float(cols)), n + 1)
        if image.ndim == 2:
            plt.gray()
        plt.imshow(image)
        a.set_title(title)
    fig.set_size_inches(np.array(fig.get_size_inches()) * n_images)
    plt.show()


im = s4_utils.read_image("/Users/shaulr/PycharmProjects/miniSLAM/table_small/table_small_1/table_small_1_100_depth.png", s4_utils.RGB_REP)
original_im = s4_utils.read_image("/Users/shaulr/PycharmProjects/miniSLAM/table_small/table_small_1/table_small_1_100.png", s4_utils.RGB_REP)
edge = feature.canny(im, sigma=5)


thresh = 10
ims = [original_im, im, edge]
titles = ['original', 'depth', 'canny_edge_sigma_3']
# show_images(ims, 1, titles)


for _ in range(0, 8):
    print('****'+ str(_) +'******')
    plane, all_ims, b_im, edges = ransac_plane(im, 1, thresh)
    ims.append(b_im)
    # ims.append(edges)
예제 #18
0
def generate_panorama(data_dir,
                      file_prefix,
                      num_images,
                      pan_gen,
                      figsize=(20, 20)):
    """
    Geneare panorama out of the files in the given dir with the given prefix.
    displays the results
    Finally, it saves the result in the data_dir with the name file_prefix_panoram.jpg
    :param data_dir: The directory where the image are
    :param file_prefix: The prefix for each image (convention is nameN)
    :param num_images: how many images to render from the series
    :param pan_gen: A function which recieves ims_rgb and Htot (Accumulkated homographies) and use them to generate and
                  return a rgb panorama image
    :param figsize: The figure size of the final panorama
    """
    # The naming convention for a sequence of images is nameN.jpg, where N is a running number 1,2,..
    files = [
        os.path.join(data_dir, '%s%d.jpg' % (file_prefix, i + 1))
        for i in range(num_images)
    ]

    # Read images.
    ims = [sol4_utils.read_image(f, 1) for f in files]

    # Extract feature point locations and descriptors.
    def im_to_points(im):
        pyr, _ = sol4_utils.build_gaussian_pyramid(im, 3, 7)
        return sol4.find_features(pyr)

    p_d = [im_to_points(im) for im in ims]

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

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

        # Compute homography using RANSAC.
        H12, inliers = sol4.ransac_homography(points1, points2, 10000, 8)

        # Display inlier and outlier matches.
        sol4.display_matches(ims[i],
                             ims[i + 1],
                             points1,
                             points2,
                             inliers=inliers)
        Hs.append(H12)

    # Compute composite homographies from the panorama coordinate system.
    Htot = sol4.accumulate_homographies(Hs, (num_images - 1) // 2)

    # Final panorama is generated using 3 channels of the RGB images
    ims_rgb = [sol4_utils.read_image(f, 2) for f in files]

    # generate the panorama
    panorama = pan_gen(ims_rgb, Htot)

    # save the result
    imsave(os.path.join(data_dir, file_prefix + '_panorama.jpg'), panorama)

    # plot the panorama
    plt.figure(figsize=figsize)
    plt.imshow(panorama.clip(0, 1))
    plt.show()
예제 #19
0
    cols (Default = 1): Number of columns in figure (number of rows is
                        set to np.ceil(n_images/float(cols))).

    titles: List of titles corresponding to each image. Must have
            the same length as titles.
    """
    assert ((titles is None) or (len(images) == len(titles)))
    n_images = len(images)
    if titles is None:
        titles = ['Image (%d)' % i for i in range(1, n_images + 1)]
    fig = plt.figure()
    for n, (image, title) in enumerate(zip(images, titles)):
        a = fig.add_subplot(cols, np.ceil(n_images / float(cols)), n + 1)
        if image.ndim == 2:
            plt.gray()
        plt.imshow(image)
        a.set_title(title)
    fig.set_size_inches(np.array(fig.get_size_inches()) * n_images)
    plt.show()


im = s4_utils.read_image(
    "C:/Users/Shaul Ro/PycharmProjects/Tutorialed_Work/assets/table_small_1_100_depth.png",
    s4_utils.RGB_REP)
original_im = s4_utils.read_image(
    "C:/Users/Shaul Ro/PycharmProjects/Tutorialed_Work/assets/table_small_1_100.png",
    s4_utils.RGB_REP)
edge = feature.canny(im, sigma=5)

plane, all_ims, best_im = ransac_plane(im, 9, 4)
show_images(all_ims, 3)