示例#1
0
    # Read image
    impath = os.path.join("images", "lake.jpg")
    im = plt.imread(impath)

    # Define the convolutional kernels
    h_a = np.ones((3, 3)) / 9
    h_b = np.array([
        [1, 4, 6, 4, 1],
        [4, 16, 24, 16, 4],
        [6, 24, 36, 24, 6],
        [4, 16, 24, 16, 4],
        [1, 4, 6, 4, 1],
    ]) / 256
    # Convolve images
    smoothed_im1 = convolve_im(im.copy(), h_a)
    smoothed_im2 = convolve_im(im, h_b)

    # DO NOT CHANGE
    assert isinstance(smoothed_im1, np.ndarray), \
        f"Your convolve function has to return a np.array. " +\
        f"Was: {type(smoothed_im1)}"
    assert smoothed_im1.shape == im.shape, \
        f"Expected smoothed im ({smoothed_im1.shape}" + \
        f"to have same shape as im ({im.shape})"
    assert smoothed_im2.shape == im.shape, \
        f"Expected smoothed im ({smoothed_im1.shape}" + \
        f"to have same shape as im ({im.shape})"

    save_im("convolved_im_h_a.jpg", smoothed_im1)
    save_im("convolved_im_h_b.jpg", smoothed_im2)
示例#2
0
        [-1, -1, -1],
        [-1, 8, -1],
        [-1, -1, -1],
    ])
    h_d = np.array([
        [0, -1, 0],
        [-1, 5, -1],
        [0, -1, 0],
    ])
    # Convolve images
    smoothed_im1 = convolve_im(lake_im, h_a)
    smoothed_im2 = convolve_im(lake_im, h_b)
    edge_im = convolve_im(lake_im, h_c)
    sharpened_im = convolve_im(lake_im, h_d)

    save_im("convolved_im_h_a.png", smoothed_im1)
    save_im("convolved_im_h_b.png", smoothed_im2)
    save_im("edge_detection.png", edge_im)
    save_im("sharpened.png", sharpened_im)

    # DO NOT CHANGE
    assert isinstance(smoothed_im1, np.ndarray), \
        "Your convolve function has to return a np.array. " +\
        "Was: {type(smoothed_im1)}"
    assert smoothed_im1.shape == lake_im.shape, \
        "Expected smoothed lake_im ({smoothed_im1.shape}" + \
        "to have same shape as lake_im ({lake_im.shape})"
    assert smoothed_im2.shape == lake_im.shape, \
        "Expected smoothed lake_im ({smoothed_im1.shape}" + \
        "to have same shape as lake_im ({lake_im.shape})"