Exemplo n.º 1
0
def test_warpImagePair():
    image_1 = cv2.imread("images/source/panorama_1/1.jpg")
    image_2 = cv2.imread("images/source/panorama_1/2.jpg")
    image_1_kp, image_2_kp, matches = assignment8.findMatchesBetweenImages(
        image_1, image_2, 20)
    homography = assignment8.findHomography(image_1_kp, image_2_kp, matches)
    warped_image = assignment8.warpImagePair(image_1, image_2, homography)

    # Read in answer that has the correct type / shape.
    type_answer = cv2.imread("images/testing/warped_image_1_2.jpg")

    print "Evaluating warpImagePair."
    # Test for type.
    if not type(warped_image) == type(type_answer):
        raise TypeError(
            ("Error - warped_image has type {}. " +
             "Expected type is {}.").format(type(warped_image),
                                            type(type_answer)))
    # Test for shape.
    if abs(np.sum(np.subtract(warped_image.shape, type_answer.shape))) > 200:
        print("WARNING - warped_image has shape {}. " +
              "Expected shape is around {}.").format(warped_image.shape,
                                                     type_answer.shape)
    print "warpImagePair testing passed."

    return True
Exemplo n.º 2
0
def main(image_files, output_folder):
    """ Generate a panorama from the images in the source folder """

    inputs = ((name, cv2.imread(name)) for name in sorted(image_files)
              if path.splitext(name)[-1][1:].lower() in EXTENSIONS)

    # start with the first image in the folder and process each image in order
    name, pano = inputs.next()
    print "\n  Starting with: {}".format(name)
    for name, next_img in inputs:

        if next_img is None:
            print "\nUnable to proceed: {} failed to load.".format(name)
            return

        print "  Adding: {}".format(name)

        kp1, kp2, matches = a8.findMatchesBetweenImages(pano, next_img, 10)
        homography = a8.findHomography(kp1, kp2, matches)
        min_xy, max_xy = a8.getBoundingCorners(pano, next_img, homography)
        pano = a8.warpCanvas(pano, homography, min_xy, max_xy)
        pano = a8.blendImagePair(pano, next_img, -min_xy)

    cv2.imwrite(path.join(output_folder, "output.jpg"), pano)
    print "  Done!"
Exemplo n.º 3
0
def test_warpImagePair():
    image_1 = cv2.imread("images/source/panorama_1/1.jpg")
    image_2 = cv2.imread("images/source/panorama_1/2.jpg")
    image_1_kp, image_2_kp, matches = assignment8.findMatchesBetweenImages(
        image_1, image_2, 20)
    homography = assignment8.findHomography(image_1_kp, image_2_kp, matches)
    warped_image = assignment8.warpImagePair(image_1, image_2, homography)

    # Read in answer that has the correct type / shape.
    type_answer = cv2.imread("images/testing/warped_image_1_2.jpg")

    print "Evaluating warpImagePair."
    # Test for type.
    if not type(warped_image) == type(type_answer):
        raise TypeError(
            ("Error - warped_image has type {}. " + 
             "Expected type is {}.").format(type(warped_image),
                                            type(type_answer)))
    # Test for shape.
    if abs(np.sum(np.subtract(warped_image.shape, type_answer.shape))) > 200:
        print ("WARNING - warped_image has shape {}. " +
               "Expected shape is around {}.").format(warped_image.shape,
                                                      type_answer.shape)
    print "warpImagePair testing passed."

    return True
Exemplo n.º 4
0
def test_findMatchesBetweenImages():
    """ This script will perform a unit test on the matching function.
    """
    # Hard code output matches.
    image_1 = cv2.imread("images/source/panorama_1/1.jpg")
    image_2 = cv2.imread("images/source/panorama_1/2.jpg")

    print "Evaluating findMatchesBetweenImages."

    image_1_kp, image_2_kp, matches = \
        assignment8.findMatchesBetweenImages(image_1, image_2, 20)

    if not type(image_1_kp) == list:
        raise TypeError(
            "Error - image_1_kp has type {}. Expected type is {}.".format(
                type(image_1_kp), list))

    if len(image_1_kp) > 0 and \
        not type(image_1_kp[0]) == type(cv2.KeyPoint()):
        raise TypeError(("Error - The items in image_1_kp have type {}. " + \
                         "Expected type is {}.").format(type(image_1_kp[0]),
                                                        type(cv2.KeyPoint())))

    if not type(image_2_kp) == list:
        raise TypeError(
            "Error - image_2_kp has type {}. Expected type is {}.".format(
                type(image_2_kp), list))

    if len(image_2_kp) > 0 and \
        not type(image_2_kp[0]) == type(cv2.KeyPoint()):
        raise TypeError(("Error - The items in image_2_kp have type {}. " + \
                         "Expected type is {}.").format(type(image_2_kp[0]),
                                                        type(cv2.KeyPoint())))

    if not type(matches) == list:
        raise TypeError(
            "Error - matches has type {}. Expected type is {}. ".format(
                type(matches), list))

    if len(matches) > 0 and not type(matches[0]) == type(cv2.DMatch()):
        raise TypeError(("Error - The items in matches have type {}. " + \
                         "Expected type is {}.").format(type(matches[0]),
                                                        type(cv2.DMatch())))
    print "findMatchesBetweenImages testing passed."
    return True
Exemplo n.º 5
0
def test_findMatchesBetweenImages():
    """ This script will perform a unit test on the matching function.
    """
    # Hard code output matches.
    image_1 = cv2.imread("images/source/panorama_1/1.jpg")
    image_2 = cv2.imread("images/source/panorama_1/2.jpg")

    print "Evaluating findMatchesBetweenImages."

    image_1_kp, image_2_kp, matches = \
        assignment8.findMatchesBetweenImages(image_1, image_2, 20)

    if not type(image_1_kp) == list:
        raise TypeError(
            "Error - image_1_kp has type {}. Expected type is {}.".format(
                type(image_1_kp), list))

    if len(image_1_kp) > 0 and \
        not type(image_1_kp[0]) == type(cv2.KeyPoint()):
        raise TypeError(("Error - The items in image_1_kp have type {}. " + \
                         "Expected type is {}.").format(type(image_1_kp[0]),
                                                        type(cv2.KeyPoint())))

    if not type(image_2_kp) == list:
        raise TypeError(
            "Error - image_2_kp has type {}. Expected type is {}.".format(
                type(image_2_kp), list))

    if len(image_2_kp) > 0 and \
        not type(image_2_kp[0]) == type(cv2.KeyPoint()):
        raise TypeError(("Error - The items in image_2_kp have type {}. " + \
                         "Expected type is {}.").format(type(image_2_kp[0]),
                                                        type(cv2.KeyPoint())))

    if not type(matches) == list:
        raise TypeError(
            "Error - matches has type {}. Expected type is {}. ".format(
                type(matches), list))

    if len(matches) > 0 and not type(matches[0]) == type(cv2.DMatch()):
        raise TypeError(("Error - The items in matches have type {}. " + \
                         "Expected type is {}.").format(type(matches[0]),
                                                        type(cv2.DMatch())))
    print "findMatchesBetweenImages testing passed."
    return True
Exemplo n.º 6
0
def test_findHomography():
    # Hard code output matches.
    image_1 = cv2.imread("images/source/panorama_1/1.jpg")
    image_2 = cv2.imread("images/source/panorama_1/2.jpg")

    image_1_kp, image_2_kp, matches = assignment8.findMatchesBetweenImages(
        image_1, image_2, 20)

    homography = assignment8.findHomography(image_1_kp, image_2_kp, matches)

    ans = np.array([[1.43341688e+00, 2.97957049e-02, -1.10725647e+03],
                    [1.71655562e-01, 1.36708332e+00, -5.06635922e+02],
                    [1.06589023e-04, 6.16589688e-05, 1.00000000e+00]])

    print "Evaluating findHomography."
    # Test for type.
    if not type(homography) == type(ans):
        raise TypeError(
            ("Error - homography has type {}. " +
             "Expected type is {}.").format(type(homography), type(ans)))
    # Test for shape.
    if not homography.shape == ans.shape:
        raise ValueError(
            ("Error - homography has shape {}." +
             " Expected shape is {}.").format(homography.shape, ans.shape))
    if not type(homography[0][0]) == type(ans[0][0]):
        raise TypeError(
            ("Error - The items in homography have type {}. " +
             "Expected type is {}.").format(type(homography), type(ans)))

    # if not np.max(abs(np.subtract(homography, ans))) < 2.0:
    #     print "If your homography looks significantly different make sure " + \
    #           "you look at the warped image output. That is the only way " + \
    #           "of knowing if it is correct. Images should be aligned well. " + \
    #           "We expect ORB & SIFT to output equivalent homographies, but " + \
    #           "if you run into this error please take a look at your output."
    #     raise ValueError(
    #         ("Error - your output seems to be significantly different, " +
    #          "please verify this yourself. \n Given output {}. \n" +
    #          "Expected output {}. \n").format(homography, ans))
    print "findHomography testing passed."
    return True
Exemplo n.º 7
0
def test_findHomography():
    # Hard code output matches.
    image_1 = cv2.imread("images/source/panorama_1/1.jpg")
    image_2 = cv2.imread("images/source/panorama_1/2.jpg")

    image_1_kp, image_2_kp, matches = assignment8.findMatchesBetweenImages(
        image_1, image_2, 20)

    homography = assignment8.findHomography(image_1_kp, image_2_kp, matches)

    ans = np.array([[1.43341688e+00, 2.97957049e-02, -1.10725647e+03],
                    [1.71655562e-01, 1.36708332e+00, -5.06635922e+02],
                    [1.06589023e-04, 6.16589688e-05, 1.00000000e+00]])

    print "Evaluating findHomography."
    # Test for type.
    if not type(homography) == type(ans):
        raise TypeError(
            ("Error - homography has type {}. " + 
             "Expected type is {}.").format(type(homography), type(ans)))
    # Test for shape.
    if not homography.shape == ans.shape:
        raise ValueError(
            ("Error - homography has shape {}." +
             " Expected shape is {}.").format(homography.shape, ans.shape))
    if not type(homography[0][0]) == type(ans[0][0]):
        raise TypeError(
            ("Error - The items in homography have type {}. " + 
             "Expected type is {}.").format(type(homography), type(ans)))

    # if not np.max(abs(np.subtract(homography, ans))) < 2.0:
    #     print "If your homography looks significantly different make sure " + \
    #           "you look at the warped image output. That is the only way " + \
    #           "of knowing if it is correct. Images should be aligned well. " + \
    #           "We expect ORB & SIFT to output equivalent homographies, but " + \
    #           "if you run into this error please take a look at your output."
    #     raise ValueError(
    #         ("Error - your output seems to be significantly different, " +
    #          "please verify this yourself. \n Given output {}. \n" +
    #          "Expected output {}. \n").format(homography, ans))
    print "findHomography testing passed."
    return True
Exemplo n.º 8
0
        panorama_filepaths = []

        for filename in filenames:
            name, ext = os.path.splitext(filename)
            if ext.lower() in exts:
                panorama_filepaths.append(os.path.join(dirname, filename))
        panorama_filepaths.sort()

        for pan_fp in panorama_filepaths:
            panorama_inputs.append(cv2.imread(pan_fp))

        if len(panorama_inputs) > 1:
            print ("Found {} images in folder {}. " + \
                   "Processing them.").format(len(panorama_inputs), dirname)
        else:
            continue

        print "Computing matches."
        cur_img = panorama_inputs[0]
        for new_img in panorama_inputs[1:]:
            image_1_kp, image_2_kp, matches = \
                assignment8.findMatchesBetweenImages(cur_img, new_img, 20)
            print "Computing homography."
            homography = assignment8.findHomography(image_1_kp, image_2_kp,
                                                    matches)
            print "Warping the image pair."
            cur_img = assignment8.warpImagePair(cur_img, new_img, homography)

        print "Writing output image to {}".format(outfolder)
        cv2.imwrite(os.path.join(outfolder, setname) + ".jpg", cur_img)
Exemplo n.º 9
0
        panorama_filepaths = []

        for filename in filenames:
            name, ext = os.path.splitext(filename)
            if ext.lower() in exts:
                panorama_filepaths.append(os.path.join(dirname, filename))
        panorama_filepaths.sort()

        for pan_fp in panorama_filepaths:
            panorama_inputs.append(cv2.imread(pan_fp))

        if len(panorama_inputs) > 1:
            print ("Found {} images in folder {}. " + \
                   "Processing them.").format(len(panorama_inputs), dirname)
        else:
            continue

        print "Computing matches."
        cur_img = panorama_inputs[0]
        for new_img in panorama_inputs[1:]:
            image_1_kp, image_2_kp, matches = \
                assignment8.findMatchesBetweenImages(cur_img, new_img, 20)
            print "Computing homography."
            homography = assignment8.findHomography(image_1_kp, image_2_kp,
                                                    matches)
            print "Warping the image pair."
            cur_img = assignment8.warpImagePair(cur_img, new_img, homography)
        
        print "Writing output image to {}".format(outfolder)
        cv2.imwrite(os.path.join(outfolder, setname) + ".jpg", cur_img)