from PIL import Image import pylab as pl import numpy as np from common import imtools im = np.array(Image.open('data/empire.jpg')) pl.figure('Orignal') pl.imshow(im) im_resz = imtools.imresize(im, [142, 200]) pl.figure('Resized') pl.imshow(im_resz) pl.show()
from PIL import Image import numpy as np import pylab as plt from common import imtools from common import harris # This is the Harris point matching example in Figure 2-2. im1 = np.array(Image.open("data/crans_1_small.jpg").convert("L")) im2 = np.array(Image.open("data/crans_2_small.jpg").convert("L")) # resize to make matching faster im1 = imtools.imresize(im1, (int(im1.shape[1] / 2), int(im1.shape[0] / 2))) im2 = imtools.imresize(im2, (int(im2.shape[1] / 2), int(im2.shape[0] / 2))) wid = 5 harrisim = harris.compute_harris_response(im1, 5) filtered_coords1 = harris.get_harris_points(harrisim, wid + 1) d1 = harris.get_descriptors(im1, filtered_coords1, wid) harrisim = harris.compute_harris_response(im2, 5) filtered_coords2 = harris.get_harris_points(harrisim, wid + 1) d2 = harris.get_descriptors(im2, filtered_coords2, wid) print('starting matching') matches = harris.match_twosided(d1, d2) plt.figure() plt.gray() harris.plot_matches(im1, im2, filtered_coords1, filtered_coords2, matches) plt.show()
from common import sift import pydot download_path = "data/EiffelTower" thumbnail_path = os.getcwd() + "/graphs/thumbnails/" # list of downloaded filenames im_paths = imtools.get_imlist(download_path) nbr_images = len(im_paths) im_list = [] for path in im_paths: im = np.array(Image.open(path).convert("L")) im_list.append( imtools.imresize(im, (int(im.shape[1] / 2), int(im.shape[0] / 2)))) matchscores = np.zeros((nbr_images, nbr_images)) for i in range(nbr_images): for j in range(i, nbr_images): # only compute upper triangle print('comparing ', im_paths[i], im_paths[j]) l1, d1 = sift.detect_and_compute(im_list[i]) l2, d2 = sift.detect_and_compute(im_list[j]) matches = sift.match_twosided(d1, d2) nbr_matches = np.sum(matches > 0) print('number of matches = ', nbr_matches) matchscores[i, j] = nbr_matches # copy values for i in range(nbr_images):
from common import imtools from common import sift import pydot download_path = "data/WhiteHouse" thumbnail_path = os.getcwd() + "/graphs/thumbnails/" # list of downloaded filenames im_paths = imtools.get_imlist(download_path) nbr_images = len(im_paths) im_list = [] for path in im_paths: im = np.array(Image.open(path).convert("L")) im_list.append(imtools.imresize(im,(int(im.shape[1]/2),int(im.shape[0]/2)))) matchscores = np.zeros((nbr_images,nbr_images)) for i in range(nbr_images): for j in range(i,nbr_images): # only compute upper triangle print('comparing ', im_paths[i], im_paths[j]) l1,d1 = sift.detect_and_compute(im_list[i]) l2,d2 = sift.detect_and_compute(im_list[j]) matches = sift.match_twosided(d1,d2) nbr_matches = np.sum(matches > 0) print('number of matches = ', nbr_matches) matchscores[i,j] = nbr_matches # copy values
""" Exercise 4 Create copies of an image with different resolutions (for example by halving the size a few times). Extract SIFT features for each image. Plot and match features to get a feel for how and when the scale independence breaks down. """ from PIL import Image import numpy as np import pylab as plt from common import imtools from common import sift # Seems to break down around 8, clearly breaks down at 16. div_factor = 8 im = np.array(Image.open('data/empire.jpg').convert("L")) im_resized = imtools.imresize(im, (int(im.shape[1]/div_factor),int(im.shape[0]/div_factor))) kp1, desc1 = sift.detect_and_compute(im) kp2, desc2 = sift.detect_and_compute(im_resized) print('starting matching') matches = sift.match_twosided(desc1,desc2) plt.figure() plt.gray() sift.plot_matches(im,im_resized,kp1,kp2,matches) plt.show()