def test_homography_torture(self): """Test homography estimation accuracy across many inputs. This "torture test" for homography estimation exercises the homography() function across several input images and a broad range of perspective distortions, computing a final "score" that is the median difference between the estimated and true homography. """ # Initialize the random module to a known state. random.seed(42) differences = [] for filename in ("houses_left.png", "camera_hand.png", "boots.png", "clouds.png", "foam.png"): image = cv2.imread(os.path.join("test_data/torture", filename), cv2.CV_LOAD_IMAGE_GRAYSCALE) rows, cols = image.shape for i in range(10): H_expected = self._random_homography(rows, cols) image_warped = cv2.warpPerspective(image, H_expected, (int(cols * 1.5), int(rows * 1.5))) # Uncomment to visualize. # cv2.imshow("warp", image_warped) # cv2.waitKey(0) H_actual = pano_stitcher.homography(image_warped, image) H_difference = numpy.absolute(H_expected - H_actual) H_difference_magnitude = numpy.linalg.norm(H_difference) differences.append(H_difference_magnitude) print "Difference for", filename, "homography", i, ":", print H_difference_magnitude print "Median difference: ", sorted(differences)[len(differences) / 2]
def test_homography(self): """Checks that a known homography is recovered accurately.""" # Load the left_houses image. houses_left = cv2.imread("test_data/houses_left.png", cv2.CV_LOAD_IMAGE_GRAYSCALE) rows, cols = houses_left.shape # Warp with a known homography. H_expected = self._known_homography(rows, cols) houses_left_warped = cv2.warpPerspective(houses_left, H_expected, (cols, rows)) # Compute the homography with the library. H_actual = pano_stitcher.homography(houses_left_warped, houses_left) # The two should be nearly equal. H_difference = numpy.absolute(H_expected - H_actual) H_difference_magnitude = numpy.linalg.norm(H_difference) print "Expected homography:" print H_expected print "Actual homography:" print H_actual print "Difference:" print H_difference print "Magnitude of difference:", H_difference_magnitude max_difference_magnitude = 0.5 self.assertLessEqual(H_difference_magnitude, max_difference_magnitude)
def main(): cards_1 = cv2.imread("cards_1.jpg") cards_2 = cv2.imread("cards_2.jpg") cards_3 = cv2.imread("cards_3.jpg") H_1 = pano_stitcher.homography(cards_2, cards_1) H_2 = pano_stitcher.homography(cards_2, cards_2) H_3 = pano_stitcher.homography(cards_2, cards_3) cards_1_warped, cards_1_origin = pano_stitcher.warp_image(cards_1, H_1) cards_2_warped, cards_2_origin = pano_stitcher.warp_image(cards_2, H_2) cards_3_warped, cards_3_origin = pano_stitcher.warp_image(cards_3, H_3) images = (cards_1_warped, cards_3_warped, cards_2_warped) origins = (cards_1_origin, cards_3_origin, cards_2_origin) pano = pano_stitcher.create_mosaic(images, origins) cv2.imwrite("pano.png", pano)
def create_pano1(): gray_left = cv2.imread("my_panos/images/left.png", cv2.CV_LOAD_IMAGE_GRAYSCALE) left = cv2.imread("my_panos/images/left.png") # , -1) gray_middle = cv2.imread("my_panos/images/middle.png", cv2.CV_LOAD_IMAGE_GRAYSCALE) middle = cv2.imread("my_panos/images/middle.png") # ***middle = cv2.cvtColor(middle, cv2.COLOR_BGR2BGRA) gray_right = cv2.imread("my_panos/images/right.png", cv2.CV_LOAD_IMAGE_GRAYSCALE) right = cv2.imread("my_panos/images/right.png") # , -1) # rows, cols = left_image.shape # cv2.namedWindow('image', cv2.WINDOW_NORMAL) # cv2.imshow("image", left_image) # cv2.waitKey(0) # compute homography for left image homography1 = pano_stitcher.homography(gray_middle, gray_left) # warp left image warped_left, origin1 = pano_stitcher.warp_image(left, homography1) # compute homography for right image homography2 = pano_stitcher.homography(gray_middle, gray_right) # warp right image warped_right, origin2 = pano_stitcher.warp_image(right, homography2) # print("warped_left: ", warped_left.shape) # print("middle: ", middle.shape) # print("warped_right: ", warped_right.shape) images = (warped_left, warped_right, middle) origins = (origin1, origin2, (0, 0)) mosaic1 = pano_stitcher.create_mosaic(images, origins) cv2.imwrite("feetMosaic.png", mosaic1)
Three hardcoded images are used to create a simple panorama. """ import cv2 import numpy as np import pano_stitcher as ps # Open all of the images b1 = cv2.imread('my_panos/src/part1.jpg') b2 = cv2.imread('my_panos/src/part2.jpg') b3 = cv2.imread('my_panos/src/part3.jpg') # Find a homography to warp image 1 # onto image 2, warp it b1_homog = ps.homography(b2, b1) b1_warped, b1_origins = ps.warp_image(b1, b1_homog) print 'origins for warped b1:', b1_origins # Set b2 to be at the origin b2_origins = (0, 0) # Find a homography to warp image 3 # onto image 2, warp it b3_homog = ps.homography(b2, b3) b3_warped, b3_origins = ps.warp_image(b3, b3_homog) print 'origins for warped b3:', b3_origins # Convert b2 to a 4-channel image b2 = cv2.cvtColor(b2, cv2.COLOR_BGR2BGRA)
import cv2 import numpy as np import pano_stitcher as ps # Load source images p1 = cv2.imread('my_panos/src/part1.jpg') p2 = cv2.imread('my_panos/src/part2.jpg') p3 = cv2.imread('my_panos/src/part3.jpg') # Warp first image by the homography mapping # the first image to the second image p1_homography = ps.homography(p2, p1) p1_warped, p1_origin = ps.warp_image(p1, p1_homography) # Warp third image by the homography mapping # the third image to the second image p3_homography = ps.homography(p2, p3) p3_warped, p3_origin = ps.warp_image(p3, p3_homography) # Add alpha channel to second image blue, green, red = cv2.split(p2) alpha = np.zeros(green.shape, dtype=np.uint8) alpha.fill(255) p2 = cv2.merge([blue, green, red, alpha]) # Composite warped images and image in target plane pano = ps.create_mosaic( [p1_warped, p2, p3_warped], [p1_origin, (0, 0), p3_origin]) cv2.imwrite('my_panos/pano.jpg', pano)
import cv2 import numpy as np import pano_stitcher as ps # Load source images p1 = cv2.imread('my_panos/src/part1.jpg') p2 = cv2.imread('my_panos/src/part2.jpg') p3 = cv2.imread('my_panos/src/part3.jpg') # Warp first image by the homography mapping # the first image to the second image p1_homography = ps.homography(p2, p1) p1_warped, p1_origin = ps.warp_image(p1, p1_homography) # Warp third image by the homography mapping # the third image to the second image p3_homography = ps.homography(p2, p3) p3_warped, p3_origin = ps.warp_image(p3, p3_homography) # Add alpha channel to second image blue, green, red = cv2.split(p2) alpha = np.zeros(green.shape, dtype=np.uint8) alpha.fill(255) p2 = cv2.merge([blue, green, red, alpha]) # Composite warped images and image in target plane pano = ps.create_mosaic([p1_warped, p2, p3_warped], [p1_origin, (0, 0), p3_origin]) cv2.imwrite('my_panos/pano.jpg', pano)
import numpy left = cv2.imread("3.jpg", -1) right = cv2.imread("5.jpg", -1) right2 = cv2.imread("6.jpg", -1) middle = cv2.imread("4.jpg", -1) left1 = cv2.imread("2.jpg", -1) left2 = cv2.imread("1.jpg", -1) leftImages = [left2, left1, left] rightImages = [right2, right] while len(leftImages) > 1: print "Left Image Loop" image = leftImages.pop(0) h**o = pano_stitcher.homography(leftImages[0], image) warped, origin = pano_stitcher.warp_image(image, h**o) leftImages[0] = pano_stitcher.create_mosaic( [warped, cv2.cvtColor(leftImages[0], cv2.COLOR_BGR2BGRA)], [origin, (0, 0)]) leftImages[0] = cv2.cvtColor(leftImages[0], cv2.COLOR_BGRA2BGR) left_pano = leftImages[0] cv2.imshow("sup", left_pano) cv2.waitKey() while len(rightImages) > 1: print "Right Image Loop" image = rightImages.pop(0) h**o = pano_stitcher.homography(rightImages[0], image) warped, origin = pano_stitcher.warp_image(image, h**o)
# Arbitrary index a = 0 for i in left: # Show the name of the image print sys.argv[i] # Open it and append it to the left images list left_images.append(cv2.imread(sys.argv[i])) # If closest to the middle image, warp into that perspective # Otherwise, warp into the perspective of the last image # that was warped if a is 0: h = ps.homography(middle_image, left_images[a]) else: h = ps.homography(left_warped_images[a - 1], left_images[a]) warped, origins = ps.warp_image(left_images[a], h) left_warped_images.append(warped) # If closest to the middle image, use normal origins # Otherwise, use the origins + the origins of the # last image that was warped if a is 0: left_images_origins.append(origins) else: prev_origins = left_images_origins[a - 1] new_origins = (origins[0] + prev_origins[0], origins[1] + prev_origins[1]) left_images_origins.append(new_origins)
import numpy left = cv2.imread("3.jpg",-1) right = cv2.imread("5.jpg",-1) right2 = cv2.imread("6.jpg",-1) middle = cv2.imread("4.jpg",-1) left1 = cv2.imread("2.jpg",-1) left2 = cv2.imread("1.jpg",-1) leftImages = [left2, left1, left] rightImages = [right2, right] while len(leftImages) > 1: print "Left Image Loop" image = leftImages.pop(0) h**o = pano_stitcher.homography(leftImages[0], image) warped, origin = pano_stitcher.warp_image(image, h**o) leftImages[0] = pano_stitcher.create_mosaic([warped, cv2.cvtColor(leftImages[0],cv2.COLOR_BGR2BGRA)], [origin, (0,0)]) leftImages[0] = cv2.cvtColor(leftImages[0],cv2.COLOR_BGRA2BGR) left_pano = leftImages[0] cv2.imshow("sup", left_pano) cv2.waitKey() while len(rightImages) > 1: print "Right Image Loop" image = rightImages.pop(0) h**o = pano_stitcher.homography(rightImages[0], image) warped, origin = pano_stitcher.warp_image(image, h**o) rightImages[0] = pano_stitcher.create_mosaic([warped, cv2.cvtColor(rightImages[0],cv2.COLOR_BGR2BGRA)], [origin, (0,0)]) leftImages[0] = cv2.cvtColor(leftImages[0],cv2.COLOR_BGRA2BGR)
rawimages = [] for i in range(leftmost, rightmost+1): rawimages.append(cv2.imread(imgdir+"3-" + str(i)+".jpg")) rightmost -= leftmost middle -= leftmost leftmost -= leftmost rawimages[middle] = cv2.cvtColor(rawimages[middle], cv2.COLOR_BGR2BGRA) warpedimages = [] warpedcorners = [] for i in range(0,middle): l = rawimages[i] r = rawimages[i+1] h = pano_stitcher.homography(r, l) print("Finished left homography #"+str(i)) warpedimage, warpedcorner = pano_stitcher.warp_image(l, h) print("Warped image #"+str(i)) warpedimages.append(warpedimage) warpedcorners.append(warpedcorner) for i in range(middle,rightmost) : l = rawimages[i] r = rawimages[i+1] h = pano_stitcher.homography(l,r) print("Finished right homography #"+str(i)) warpedimage, warpedcorner = pano_stitcher.warp_image(r, h) print("Warped image #"+str(i))
# Arbitrary index a = 0 for i in left: # Show the name of the image print sys.argv[i] # Open it and append it to the left images list left_images.append(cv2.imread(sys.argv[i])) # If closest to the middle image, warp into that perspective # Otherwise, warp into the perspective of the last image # that was warped if a is 0: h = ps.homography(middle_image, left_images[a]) else: h = ps.homography(left_warped_images[a - 1], left_images[a]) warped, origins = ps.warp_image(left_images[a], h) left_warped_images.append(warped) # If closest to the middle image, use normal origins # Otherwise, use the origins + the origins of the # last image that was warped if a is 0: left_images_origins.append(origins) else: prev_origins = left_images_origins[a - 1] new_origins = ( origins[0] + prev_origins[0], origins[1] + prev_origins[1]