Beispiel #1
0
def p2(p1, p2, savename):
    # read left and right images
    imgleft = read_img(p1)
    imgright = read_img(p2)
    # stitch image
    output = stitchimage(imgleft, imgright)
    # save stitched image
    save_img(output, './{}.jpg'.format(savename))
Beispiel #2
0
def main():
    img = read_img('./grace_hopper.png')

    # Feature Detection
    if not os.path.exists("./feature_detection"):
        os.makedirs("./feature_detection")

    # -- TODO Task 5: Corner Score --

    # (a): Complete corner_score()

    # (b)
    # Define offsets and window size and calulcate corner score
    u, v, W = 5, 0, (5, 5)

    score = corner_score(img, u, v, W)
    save_img(score, "./feature_detection/corner_score.png")

    #breakpoint()

    # (c): No Code

    # -- TODO Task 6: Harris Corner Detector --
    # (a): Complete harris_detector()

    # (b)
    harris_corners = harris_detector(img)
    save_img(harris_corners, "./feature_detection/harris_response.png")
Beispiel #3
0
def main():
    image = read_img('./cells/001cell.png')
    image = (image - np.mean(image)) / np.std(image)

    # # Create directory for polka_detections
    # if not os.path.exists("./polka_detections"):
    #     os.makedirs("./polka_detections")

    # # -- Detecting Polka Dots
    # print("Detect small polka dots")
    # # -- Detect Small Circles
    # sigma_1, sigma_2 = 10,20
    # gauss_1 = gaussian_filter(image, sigma_1)  # to implenent
    # gauss_2 = gaussian_filter(image, sigma_2)  # to implement

    # # calculate difference of gaussians
    # DoG_small = gauss_2 - gauss_1  # to implement

    # # visualize maxima
    # maxima = find_maxima(DoG_small, k_xy=int(sigma_1))
    # visualize_scale_space(DoG_small, sigma_1, sigma_2 / sigma_1,
    #                       './polka_detections/polka_small_DoG.png')
    # visualize_maxima(image, maxima, sigma_1, sigma_2 / sigma_1,
    #                  './polka_detections/polka_small.png')

    # # # -- Detect Large Circles
    # print("Detect large polka dots")
    # sigma_1, sigma_2 = 45, 60
    # gauss_1 = gaussian_filter(image, sigma_1)
    # gauss_2 = gaussian_filter(image, sigma_2)

    # # calculate difference of gaussians
    # DoG_large = gauss_2 - gauss_1

    # # visualize maxima
    # # Value of k_xy is a sugguestion; feel free to change it as you wish.
    # maxima = find_maxima(DoG_large, k_xy=10)
    # visualize_scale_space(DoG_large, sigma_1, sigma_2 / sigma_1,
    #                       './polka_detections/polka_large_DoG.png')
    # visualize_maxima(image, maxima, sigma_1, sigma_2 / sigma_1,
    #                  './polka_detections/polka_large.png')

    # TODO Implement scale_space() and try to find both polka dots
    k = 1.3
    min_sigma = 2
    scalespace = scale_space(image, min_sigma, k, 8)
    visualize_scale_space(scalespace, min_sigma, k,
                          './cell_detections/001cell_scalespace.png')
    # TODO Detect the cells in any one (or more) image(s) from vgg_cells
    # # Create directory for polka_detections
    # if not os.path.exists("./cell_detections"):
    #     os.makedirs("./cell_detections")
    kxy = 10
    ks = 4
    maxima = find_maxima(scalespace, kxy, ks)

    visualize_maxima(
        image, maxima, min_sigma, k, './cell_detections/001_kxy' + str(kxy) +
        '_ks' + str(ks) + 'Detect' + str(len(maxima)) + '.png')
Beispiel #4
0
def main():
    # The main function
    ########################
    img = read_img('./grace_hopper.png')

    ##### Feature Detection #####  
    if not os.path.exists("./feature_detection"):
        os.makedirs("./feature_detection")

    # define offsets and window size and calulcate corner score
    u, v, W = 0, 2, (5,5)
    
    score = corner_score(img, u, v, W)
    save_img(score, "./feature_detection/corner_score.png")

    harris_corners = harris_detector(img)
    save_img(harris_corners, "./feature_detection/harris_response.png")
Beispiel #5
0
def main():
    # The main function
    img = read_img('./grace_hopper.png')
    """ Image Patches """
    if not os.path.exists("./image_patches"):
        os.makedirs("./image_patches")

    # -- Image Patches --
    # Q1
    # patches = image_patches(img)
    # TODO choose a few patches and save them
    # chosen_patches = patches[:3]

    # save_img(chosen_patches[0], "./image_patches/q1_patch1.png")
    # save_img(chosen_patches[1], "./image_patches/q1_patch2.png")
    # save_img(chosen_patches[2], "./image_patches/q1_patch3.png")

    # Q2: No code
    """ Gaussian Filter """
    # if not os.path.exists("./gaussian_filter"):
    #     os.makedirs("./gaussian_filter")

    # -- Gaussian Filter --
    # Q1: No code

    # Q2

    # TODO: Calculate the kernel described in the question.
    # There is tolerance for the kernel.
    va = 1 / (2 * np.log(2))
    k = np.zeros((3, 3))
    for i in range(3):
        for j in range(3):
            k[i, j] = 1 / (2 * np.pi * va) * np.exp(-((i - 1)**2 +
                                                      (j - 1)**2) / (2 * va))

    filtered_gaussian = convolve(img, k)
    save_img(filtered_gaussian, "./gaussian_filter/q2_gaussian.png")

    # # Q3
    edge_detect, _, _ = edge_detection(img)
    save_img(edge_detect, "./gaussian_filter/q3_edge.png")
    edge_with_gaussian, _, _ = edge_detection(filtered_gaussian)
    save_img(edge_with_gaussian, "./gaussian_filter/q3_edge_gaussian.png")

    print("Gaussian Filter is done. ")
def main():
    image = read_img('polka.png')

    # Create directory for polka_detections
    if not os.path.exists("./polka_detections"):
        os.makedirs("./polka_detections")

    # -- Detecting Polka Dots
    print("Detect small polka dots")
    # -- Detect Small Circles
    sigma_1, sigma_2 = None, None
    gauss_1 = None  # to implenent
    gauss_2 = None  # to implement

    # calculate difference of gaussians
    DoG_small = None  # to implement
    # ~~START DELETE~~
    image = image / 255.
    sigma_1 = 10
    sigma_2 = 14
    gauss_1 = gaussian_filter(image, sigma_1)
    gauss_2 = gaussian_filter(image, sigma_2)
    DoG_small = gauss_2 - gauss_1
    # ~~END DELETE~~

    # visualize maxima
    maxima = find_maxima(DoG_small, k_xy=int(sigma_1))
    visualize_scale_space(DoG_small, sigma_1, sigma_2 / sigma_1,
                          './polka_detections/polka_small_DoG.png')
    visualize_maxima(image, maxima, sigma_1, sigma_2 / sigma_1,
                     './polka_detections/polka_small.png')

    # -- Detect Large Circles
    print("Detect large polka dots")
    sigma_1, sigma_2 = None, None
    gauss_1 = None
    gauss_2 = None

    # calculate difference of gaussians
    DoG_large = None
    # ~~START DELETE~~
    sigma_1 = 30
    sigma_2 = 40
    gauss_1 = gaussian_filter(image, sigma_1)
    gauss_2 = gaussian_filter(image, sigma_2)
    DoG_large = gauss_2 - gauss_1
    # ~~END DELETE~~

    # visualize maxima
    # Value of k_xy is a sugguestion; feel free to change it as you wish.
    maxima = find_maxima(DoG_large, k_xy=10)
    visualize_scale_space(DoG_large, sigma_1, sigma_2 / sigma_1,
                          './polka_detections/polka_large_DoG.png')
    visualize_maxima(image, maxima, sigma_1, sigma_2 / sigma_1,
                     './polka_detections/polka_large.png')

    # TODO Implement scale_space() and try to find both polka dots
    # ~~START DELETE~~
    min_sigma = 12
    k = np.sqrt(1.8)
    ss = scale_space(image, min_sigma, k=k, S=8)
    visualize_scale_space(ss, min_sigma, k,
                          './polka_detections/scale_space.png')
    # ~~END DELETE~~

    # TODO Detect the cells in any one (or more) image(s) from vgg_cells
    # Create directory for polka_detections
    if not os.path.exists("./cell_detections"):
        os.makedirs("./cell_detections")
Beispiel #7
0
    #breakpoint()
    output = cv2.warpPerspective(img,
                                 H, (output_w, output_h),
                                 flags=cv2.INTER_LINEAR)

    #breakpoint()

    return output


if __name__ == "__main__":
    # Task 5

    case_name = "threebody"

    I = read_img(os.path.join("task5", case_name, "book.jpg"))
    corners = np.load(os.path.join("task5", case_name, "corners.npy"))
    size = np.load(os.path.join("task5", case_name, "size.npy"))

    result = make_synthetic_view(I, corners, size)
    save_img(result, case_name + "_frontoparallel.jpg")

    case_name = "palmer"

    I = read_img(os.path.join("task5", case_name, "book.jpg"))
    corners = np.load(os.path.join("task5", case_name, "corners.npy"))
    size = np.load(os.path.join("task5", case_name, "size.npy"))

    result = make_synthetic_view(I, corners, size)
    save_img(result, case_name + "_frontoparallel.jpg")
Beispiel #8
0
    #breakpoint()
    draw_matches(img1, img2, keypoints1, keypoints2, matches)
    test = keypoints1[matches[:, [0]], [0]]
    XY = np.hstack((keypoints1[matches[:, [0]],
                               [0]], keypoints1[matches[:, [0]], [1]]))
    XY = np.hstack((XY, keypoints2[matches[:, [1]], [0]]))
    XY = np.hstack((XY, keypoints2[matches[:, [1]], [1]]))

    H = RANSAC_fit_homography(XY)
    #breakpoint()
    stitched = warp_and_combine(img1, img2, H, XY)
    return stitched


if __name__ == "__main__":

    #Possible starter code; you might want to loop over the task 6 images
    #to_stitch = 'eynsham'
    #to_stitch = 'mertoncourtyard'
    to_stitch_list = [
        'eynsham', 'florence2', 'florence3', 'florence3_alt', 'lowetag',
        'mertonchapel', 'mertoncourtyard', 'vgg'
    ]
    #to_stitch_list = ['eynsham', 'mertoncourtyard']
    for to_stitch in to_stitch_list:
        I1 = read_img(os.path.join('task6', to_stitch, 'p1.jpg'))
        I2 = read_img(os.path.join('task6', to_stitch, 'p2.jpg'))
        res = make_warped(I1, I2)
        save_img(res, "result_" + to_stitch + ".jpg")
        print("finished:", to_stitch)
Beispiel #9
0
def main():
    # The main function
    img = read_img('./grace_hopper.png')
    """ Image Patches """
    if not os.path.exists("./image_patches"):
        os.makedirs("./image_patches")

    # -- TODO Task 1: Image Patches --
    #(a)
    #First complete image_patches()
    patches = image_patches(img)
    # Now choose any three patches and save them
    # chosen_patches should have those patches stacked vertically/horizontally
    chosen_patches = patches[0]
    chosen_patches = np.vstack((chosen_patches, patches[1]))
    chosen_patches = np.vstack((chosen_patches, patches[2]))
    save_img(chosen_patches, "./image_patches/q1_patch.png")

    #(b), (c): No code
    """ Convolution and Gaussian Filter """
    if not os.path.exists("./gaussian_filter"):
        os.makedirs("./gaussian_filter")

    #breakpoint()

    # -- TODO Task 2: Convolution and Gaussian Filter --

    # (a): No code

    # (b): Complete convolve()

    # (c)
    # Calculate the Gaussian kernel described in the question.
    # There is tolerance for the kernel.
    sigma = 0.572
    #sigma = 2.0
    kernel_gaussian = gaussian_kernel_generator(sigma)

    filtered_gaussian = convolve(img, kernel_gaussian)
    save_img(filtered_gaussian, "./gaussian_filter/q2_gaussian.png")
    #breakpoint()

    #
    # (d), (e): No code

    # (f): Complete edge_detection()

    # (g)
    # Use edge_detection() to detect edges
    # for the orignal and gaussian filtered images.
    _, _, edge_detect = edge_detection(img)
    save_img(edge_detect, "./gaussian_filter/q3_edge.png")
    _, _, edge_with_gaussian = edge_detection(filtered_gaussian)
    save_img(edge_with_gaussian, "./gaussian_filter/q3_edge_gaussian.png")

    print("Gaussian Filter is done. ")
    #breakpoint()

    # -- TODO Task 3: Sobel Operator --

    if not os.path.exists("./sobel_operator"):
        os.makedirs("./sobel_operator")

    # (a): No code

    # (b): Complete sobel_operator()

    # (c)
    Gx, Gy, edge_sobel = sobel_operator(img)
    save_img(Gx, "./sobel_operator/q2_Gx.png")
    save_img(Gy, "./sobel_operator/q2_Gy.png")
    save_img(edge_sobel, "./sobel_operator/q2_edge_sobel.png")

    print("Sobel Operator is done. ")

    #breakpoint()

    # -- TODO Task 4: LoG Filter --
    if not os.path.exists("./log_filter"):
        os.makedirs("./log_filter")

    # (a)
    kernel_LoG1 = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
    kernel_LoG2 = np.array([[0, 0, 3, 2, 2, 2, 3, 0, 0],
                            [0, 2, 3, 5, 5, 5, 3, 2, 0],
                            [3, 3, 5, 3, 0, 3, 5, 3, 3],
                            [2, 5, 3, -12, -23, -12, 3, 5, 2],
                            [2, 5, 0, -23, -40, -23, 0, 5, 2],
                            [2, 5, 3, -12, -23, -12, 3, 5, 2],
                            [3, 3, 5, 3, 0, 3, 5, 3, 3],
                            [0, 2, 3, 5, 5, 5, 3, 2, 0],
                            [0, 0, 3, 2, 2, 2, 3, 0, 0]])

    #breakpoint()

    filtered_LoG1 = convolve(img, kernel_LoG1)
    filtered_LoG2 = convolve(img, kernel_LoG2)
    # Use convolve() to convolve img with kernel_LOG1 and kernel_LOG2
    save_img(filtered_LoG1, "./log_filter/q1_LoG1.png")
    save_img(filtered_LoG2, "./log_filter/q1_LoG2.png")

    # (b)
    # Follow instructions in pdf to approximate LoG with a DoG
    data = np.load('log1d.npz')
    LoG_50 = data['log50']
    gauss_50 = data['gauss50']
    gauss_53 = data['gauss53']
    DoG = gauss_53 - gauss_50
    x = np.arange(-250, 251)
    plt.plot(x, LoG_50, label='Laplacian')
    plt.plot(x, DoG, label='DoG')
    plt.legend()
    plt.show()
    #breakpoint()
    print("LoG Filter is done. ")
Beispiel #10
0
def main():
    image = read_img('polka.png')
    # import pdb; pdb.set_trace()
    # Create directory for polka_detections
    if not os.path.exists("./polka_detections"):
        os.makedirs("./polka_detections")

    # -- TODO Task 7: Single-scale Blob Detection --
    
    # (a), (b): Detecting Polka Dots
    # First, complete gaussian_filter()
    print("Detecting small polka dots")
    # -- Detect Small Circles
    #sigma_1, sigma_2 = None, None
    sigma_1 = 4.5/np.sqrt(2)
    sigma_2 = 1.5*sigma_1
    gauss_1 = gaussian_filter(image, sigma_1)  # to implement
    gauss_2 = gaussian_filter(image, sigma_2)  # to implement

    # calculate difference of gaussians
    DoG_small = gauss_2 - gauss_1  # to implement

    # visualize maxima
    maxima = find_maxima(DoG_small, k_xy=10)
    print("There are",len(maxima), "maxima being detected")
    visualize_scale_space(DoG_small, sigma_1, sigma_2 / sigma_1,
                          './polka_detections/polka_small_DoG.png')
    visualize_maxima(image, maxima, sigma_1, sigma_2 / sigma_1,
                     './polka_detections/polka_small.png')

    #breakpoint()
    # -- Detect Large Circles
    print("Detecting large polka dots")
    sigma_1 = 11/np.sqrt(2)
    sigma_2 = 1.6*sigma_1
    gauss_1 = gaussian_filter(image, sigma_1)  # to implement
    gauss_2 = gaussian_filter(image, sigma_2)  # to implement

    # calculate difference of gaussians
    DoG_large = gauss_2 - gauss_1  # to implement

    # visualize maxima
    # Value of k_xy is a sugguestion; feel free to change it as you wish.
    maxima = find_maxima(DoG_large, k_xy=10)
    print("There are",len(maxima), "maxima being detected")
    visualize_scale_space(DoG_large, sigma_1, sigma_2 / sigma_1,
                          './polka_detections/polka_large_DoG.png')
    visualize_maxima(image, maxima, sigma_1, sigma_2 / sigma_1,
                     './polka_detections/polka_large.png')

    #breakpoint()
    
    # # -- TODO Task 8: Cell Counting --
    print("Detecting cells")

    # Detect the cells in any four (or more) images from vgg_cells
    image1 = read_img('./cells/043cell.png')
    image2 = read_img('./cells/057cell.png')
    image3 = read_img('./cells/061cell.png')
    image4 = read_img('./cells/166cell.png')

    # Create directory for cell_detections
    if not os.path.exists("./cell_detections"):
        os.makedirs("./cell_detections")

    #image1
    
    sigma_1 = 5.0/np.sqrt(2)
    sigma_2 = 7.0*sigma_1
    gauss_1 = gaussian_filter(image1, sigma_1)
    gauss_2 = gaussian_filter(image1, sigma_2)

    # calculate difference of gaussians
    DoG_043 = gauss_2 - gauss_1  # to implement

    # visualize maxima
    # Value of k_xy is a sugguestion; feel free to change it as you wish.
    maxima = find_maxima(DoG_043, k_xy=7)
    print("There are",len(maxima), "cells being detected")
    visualize_scale_space(DoG_043, sigma_1, sigma_2 / sigma_1,
                          './cell_detections/cell_detections_DoG_043.png')
    visualize_maxima(image1, maxima, sigma_1, sigma_2 / sigma_1,
                     './cell_detections/cell_detections_043.png')
    
    
    #image2
    
    sigma_1 = 3.0/np.sqrt(2)
    sigma_2 = 8.3*sigma_1
    gauss_1 = gaussian_filter(image2, sigma_1)
    gauss_2 = gaussian_filter(image2, sigma_2)

    # calculate difference of gaussians
    DoG_057 = gauss_2 - gauss_1  # to implement

    # visualize maxima
    # Value of k_xy is a sugguestion; feel free to change it as you wish.
    maxima = find_maxima(DoG_057, k_xy=7)
    print("There are",len(maxima), "cells being detected")
    visualize_scale_space(DoG_057, sigma_1, sigma_2 / sigma_1,
                          './cell_detections/cell_detections_DoG_057.png')
    visualize_maxima(image2, maxima, sigma_1, sigma_2 / sigma_1,
                     './cell_detections/cell_detections_057.png')
    
    
    #image3
    
    #image3 = resize_image(image3, 125)
    sigma_1 = 5.0/np.sqrt(2)
    sigma_2 = 6.9*sigma_1
    gauss_1 = gaussian_filter(image3, sigma_1)
    gauss_2 = gaussian_filter(image3, sigma_2)

    # calculate difference of gaussians
    DoG_061 = gauss_2 - gauss_1  # to implement

    # visualize maxima
    # Value of k_xy is a sugguestion; feel free to change it as you wish.
    maxima = find_maxima(DoG_061, k_xy=11)
    print("There are",len(maxima), "cells being detected")
    visualize_scale_space(DoG_061, sigma_1, sigma_2 / sigma_1,
                          './cell_detections/cell_detections_DoG_061.png')
    visualize_maxima(image3, maxima, sigma_1, sigma_2 / sigma_1,
                     './cell_detections/cell_detections_061.png')
    
    

    #image4
    
    sigma_1 = 5.0/np.sqrt(2)
    sigma_2 = 5.9*sigma_1
    gauss_1 = gaussian_filter(image4, sigma_1)
    gauss_2 = gaussian_filter(image4, sigma_2)

    # calculate difference of gaussians
    DoG_166 = gauss_2 - gauss_1  # to implement

    # visualize maxima
    # Value of k_xy is a sugguestion; feel free to change it as you wish.
    maxima = find_maxima(DoG_166, k_xy=6)
    print("There are",len(maxima), "cells being detected")
    visualize_scale_space(DoG_166, sigma_1, sigma_2 / sigma_1,
                          './cell_detections/cell_detections_DoG_166.png')
    visualize_maxima(image4, maxima, sigma_1, sigma_2 / sigma_1,
                     './cell_detections/cell_detections_166.png')