예제 #1
0
def test_render_pyr():
    print("Start testing rendering pyramids")
    max_size = 7
    for kersize in [3]:
        for impth in images_grey:
            im = sample_valid_image(impth)
            pyr, filter = sol3.build_gaussian_pyramid(im, max_size, kersize)
            sol3.display_pyramid(pyr, len(pyr))
            sol3.display_pyramid(pyr, len(pyr) - 1)
            sol3.display_pyramid(pyr, len(pyr) - 2)
            sol3.display_pyramid(pyr, len(pyr) - 3)
            sol3.display_pyramid(pyr, len(pyr) - 4)
            impyr = sol3.render_pyramid(pyr, len(pyr))

            pyr, filter = sol3.build_laplacian_pyramid(im, max_size, kersize)
            sol3.display_pyramid(pyr, len(pyr))
            sol3.display_pyramid(pyr, len(pyr) - 1)
            sol3.display_pyramid(pyr, len(pyr) - 2)
            sol3.display_pyramid(pyr, len(pyr) - 3)
            sol3.display_pyramid(pyr, len(pyr) - 4)
            impyr = sol3.render_pyramid(pyr, len(pyr))

            plt.show()
    plt.show()
    print("Done ok")
    def _test_reconstruct_module(self, im, im_name):
        """
        Tests module for laplacian_to_image on a specific image.
        :param im: The image to test.
        :param im_name: The image's name.
        :return: -
        """

        my_gaussian, filter_vec = sol.build_gaussian_pyramid(im, 4, 5)
        my_laplacian, filter_vec = sol.build_laplacian_pyramid(im, 4, 5)

        gaussian_pyr = _cv2_build_gaussian_pyramid(im, 4)
        laplacian_pyr = _cv2_build_laplacian_pyramid(gaussian_pyr)

        new_im = sol.laplacian_to_image(laplacian_pyr, filter_vec, np.ones(len(laplacian_pyr)))

        self._compare_images(im, new_im, im_name, r'laplacian_to_image')
예제 #3
0
def test_gaus_pyr():
    print("Start testing gaussian pyramid")
    max_size = 7
    if sol3.get_filter_kernel(3).dtype != np.float32:
        raise Exception("Kernel type is wrong")

    for kersize in [3, 5, 7]:
        for impth in images_grey:
            im = sample_valid_image(impth)
            pyr, filter = sol3.build_gaussian_pyramid(im, max_size, kersize)

            if len(pyr) > max_size:
                raise Exception("Pyramid is too long")
            if pyr[-1].shape[0] < 16 or pyr[-1].shape[1] < 16:
                raise Exception("Pyramid top is too short")
            for p in pyr:
                if p.dtype != np.float32:
                    raise Exception("Pyramid has wrong dtype")
    print("Done ok")
예제 #4
0
def check_between_zero_to_one(var, name):
    string = name
    if var.max() > 1:
        string += " ***Error*** Not between [0,1]"
    else:
        string += " OK - range is between [0,1]"
    print(string)


rgb_to_gray = sol3.read_image(rgb_im, IMG_GRAYSCALE)

plt.figure("rgb_to_gray")
plt.imshow(rgb_to_gray, cmap=plt.cm.gray)

g_pyr, filter_vec = sol3.build_gaussian_pyramid(rgb_to_gray, 10, 3)

fig = plt.figure("GAUSSIAN PYRAMID")
for i in range(len(g_pyr)):
    fig.add_subplot(1, len(g_pyr), i + 1)
    plt.imshow(g_pyr[i], cmap=plt.cm.gray)

for i in range(len(g_pyr)):
    #check_float32(g_pyr[i], "G[" + str(i) + "]")
    check_between_zero_to_one(g_pyr[i], "G[" + str(i) + "]")

l_pyr, l_filter_vec = sol3.build_laplacian_pyramid(rgb_to_gray, 10, 3)

fig = plt.figure("LAPLACIAN PYRAMID")
for i in range(len(l_pyr)):
    fig.add_subplot(1, len(l_pyr), i + 1)
예제 #5
0
def test_gauss():
    im = read_image('s.png', GRAY_REP)
    pyr, filter = sol3.build_gaussian_pyramid(im, 6, 5)
    sol3.display_pyramid(pyr, 6)
def _show_gaussian_pyr(img, max_levels, filter_size):
    _show_pyramid(sol.build_gaussian_pyramid(img, max_levels, filter_size))
예제 #7
0
def presubmit():
    print('ex3 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')
    # readme_file = 'current/README'
    # if not os.path.exists(readme_file):
    #     readme_file = 'current/README.md'
    #     if not os.path.exists(readme_file):
    #         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=== Answers to questions ===')
    # for q in [1,2,3]:
    #     if not os.path.exists('current/answer_q%d.txt'%q):
    #         print ('No answer_q%d.txt!'%q)
    #         return False
    #     print ('\nAnswer to Q%d:'%q)
    #     with open('current/answer_q%d.txt'%q) as f:
    #         print (f.read())

    print('\n=== Section 3.1 ===\n')
    im_orig = read_image('externals/monkey.jpg', 1)
    try:
        print('Trying to build Gaussian pyramid...')
        gpyr, filter_vec = sol3.build_gaussian_pyramid(im_orig, 3, 3)
        print('\tPassed!')
        print('Checking Gaussian pyramid type and structure...')
        if type(gpyr) is not list:
            raise ValueError(
                'Returned pyramid is not a list type. It is %s instead.' %
                str(type(gpyr)))
        if len(gpyr) != 3:
            raise ValueError(
                'Length of pyramid is wrong. Expecting length 3 list.')
        if filter_vec.shape != (1, 3):
            raise ValueError('Wrong blur filter size. Expecting 1x3')
        if any([l.dtype != np.float64 for l in gpyr]):
            raise ValueError(
                'At least one of the levels in the pyramid is not float64.')
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    try:
        print('Trying to build Laplacian pyramid...')
        lpyr, filter_vec = sol3.build_laplacian_pyramid(im_orig, 3, 3)
        print('\tPassed!')
        print('Checking Laplacian pyramid type and structure...')
        if type(lpyr) is not list:
            raise ValueError(
                'Returned pyramid is not a list type. It is %s instead.' %
                str(type(lpyr)))
        if len(lpyr) != 3:
            raise ValueError(
                'Length of pyramid is wrong. Expecting length 3 list.')
        if filter_vec.shape != (1, 3):
            raise ValueError('Wrong blur filter size. Expecting 1x3')
        if any([l.dtype != np.float64 for l in lpyr]):
            raise ValueError(
                'At least one of the levels in the pyramid is not float64.')
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    print('\n=== Section 3.2 ===\n')
    try:
        print('Trying to build Laplacian pyramid...')
        lpyr, filter_vec = sol3.build_laplacian_pyramid(im_orig, 3, 3)
        print('\tPassed!')
        print(
            'Trying to reconstruct image from pyramid... (we are not checking for quality!)'
        )
        im_r = sol3.laplacian_to_image(lpyr, filter_vec, [1, 1, 1])
        print('\tPassed!')
        print('Checking reconstructed image type and structure...')
        if im_r.dtype != np.float64:
            raise ValueError(
                'Reconstructed image is not float64. It is %s instead.' %
                str(im_r.dtype))
        if im_orig.shape != im_r.shape:
            raise ValueError(
                'Reconstructed image is not the same size as the original image.'
            )
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    print('\n=== Section 3.3 ===\n')
    try:
        print('Trying to build Gaussian pyramid...')
        gpyr, filter_vec = sol3.build_gaussian_pyramid(im_orig, 3, 3)
        print('\tPassed!')
        print('Trying to render pyramid to image...')
        im_pyr = sol3.render_pyramid(gpyr, 2)
        print('\tPassed!')
        print('Checking structure of returned image...')
        if im_pyr.shape != (400, 600):
            raise ValueError(
                'Rendered pyramid is not the expected size. Expecting 400x600. Found %s.'
                % str(im_pyr.shape))
        print('\tPassed!')
        print(
            'Trying to display image... (if DISPLAY env var not set, assumes running w/o screen)'
        )
        sol3.display_pyramid(gpyr, 2)
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    print('\n=== Section 4 ===\n')
    try:
        print(
            'Trying to blend two images... (we are not checking the quality!)')
        im_blend = sol3.pyramid_blending(
            im_orig, im_orig, np.zeros((400, 400), dtype=np.float64), 3, 3, 5)
        print('\tPassed!')
        print('Checking size of blended image...')
        if im_blend.shape != im_orig.shape:
            raise ValueError(
                'Size of blended image is different from the original images and mask used.'
            )
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False
    try:
        print('Tring to call blending_example1()...')
        im1, im2, mask, im_blend = sol3.blending_example1()
        print('\tPassed!')
        print('Checking types of returned results...')
        if im1.dtype != np.float64:
            raise ValueError('im1 is not float64. It is %s instead.' %
                             str(im1.dtype))
        if im2.dtype != np.float64:
            raise ValueError('im2 is not float64. It is %s instead.' %
                             str(im2.dtype))
        if mask.dtype != np.bool:
            raise ValueError('mask is not bool. It is %s instead.' %
                             str(mask.dtype))
        if im_blend.dtype != np.float64:
            raise ValueError('im_blend is not float64. It is %s instead.' %
                             str(im_blend.dtype))
        print('\tPassed!')
    except:
        print(traceback.format_exc())
        return False

    try:
        print('Tring to call blending_example2()...')
        im1, im2, mask, im_blend = sol3.blending_example2()
        print('\tPassed!')
        print('Checking types of returned results...')
        if im1.dtype != np.float64:
            raise ValueError('im1 is not float64. It is %s instead.' %
                             str(im1.dtype))
        if im2.dtype != np.float64:
            raise ValueError('im2 is not float64. It is %s instead.' %
                             str(im2.dtype))
        if mask.dtype != np.bool:
            raise ValueError('mask is not bool. It is %s instead.' %
                             str(mask.dtype))
        if im_blend.dtype != np.float64:
            raise ValueError('im_blend is not float64. It is %s instead.' %
                             str(im_blend.dtype))
        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