def rawDescriptorMatches(threshold): # keys: # ['__version__', '__header__', '__globals__', 'im1', 'descriptors2', 'im2', 'scales1', 'scales2', 'positions2', # 'descriptors1', 'positions1', 'orients2', 'orients1'] mat = scipy.io.loadmat("twoFrameData.mat") im1 = mat['im1'] im2 = mat['im2'] # user select descriptors plt.imshow(im1) MyROI = roipoly(roicolor='r') Ind = MyROI.getIdx(im1, mat['positions1']) selectedDescriptors = mat['descriptors1'][Ind] # all image2 descriptors im2Descriptors = mat['descriptors2'] # distance matrix # entry = dist2(x,c) # entry[i][j] corresponds to the distance between x[i] and c[j] dist = dist2(selectedDescriptors, im2Descriptors) # locate the distances that are less than threshold, and output the column as the index for descriptor2 # set used for removing duplicates matchedIndices = list(set(np.where(dist < threshold)[1])) # display boxes fig = plt.figure() bx = fig.add_subplot(111) bx.imshow(im2) coners = displaySIFTPatches(mat['positions2'][matchedIndices, :], mat['scales2'][matchedIndices, :], mat['orients2'][matchedIndices, :]) for j in range(len(coners)): bx.plot([coners[j][0][1], coners[j][1][1]], [coners[j][0][0], coners[j][1][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][1][1], coners[j][2][1]], [coners[j][1][0], coners[j][2][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][2][1], coners[j][3][1]], [coners[j][2][0], coners[j][3][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][3][1], coners[j][0][1]], [coners[j][3][0], coners[j][0][0]], color='g', linestyle='-', linewidth=1) bx.set_xlim(0, im2.shape[1]) bx.set_ylim(0, im2.shape[0]) plt.gca().invert_yaxis() plt.show()
import dist2 import numpy as np mat = scipy.io.loadmat("twoFrameData.mat") image1 = mat['im1'] image2 = mat['im2'] pl.imshow(image1) myRoi = roipoly(roicolor='r') Index1 = myRoi.getIdx(image1, mat['positions1']) descriptors1 = mat['descriptors1'][Index1, :] descriptors2 = mat['descriptors2'] dists = dist2.dist2(descriptors1, descriptors2) Index2 = np.argmin(dists, axis=1) fig=plt.figure() bx=fig.add_subplot(111) bx.imshow(image2) corners = displaySIFTPatches(mat['positions2'][Index2,:], mat['scales2'][Index2,:], mat['orients2'][Index2,:]) for j in range(len(corners)): bx.plot([corners[j][0][1], corners[j][1][1]], [corners[j][0][0], corners[j][1][0]], color='g', linestyle='-', linewidth=1) bx.plot([corners[j][1][1], corners[j][2][1]], [corners[j][1][0], corners[j][2][0]], color='g', linestyle='-', linewidth=1) bx.plot([corners[j][2][1], corners[j][3][1]], [corners[j][2][0], corners[j][3][0]], color='g', linestyle='-', linewidth=1) bx.plot([corners[j][3][1], corners[j][0][1]], [corners[j][3][0], corners[j][0][0]], color='g', linestyle='-', linewidth=1) bx.set_xlim(0, image2.shape[1]) bx.set_ylim(0, image2.shape[0]) plt.gca().invert_yaxis() plt.show()
fname = siftdir + fnames[i] mat = scipy.io.loadmat(fname) numfeats = mat['descriptors'].shape[0] #read the associated image imname = framesdir + fnames[i][:-4] im = misc.imread(imname) print 'imname = %s contains %d total features, each of dimension %d' %(imname, numfeats, mat['descriptors'].shape[1]); fig=plt.figure() ax=fig.add_subplot(111) ax.imshow(im) Ind = [750] coners = displaySIFTPatches(mat['positions'][Ind,:], mat['scales'][Ind,:], mat['orients'][Ind,:]) print coners for j in range(len(coners)): ax.plot([coners[j][0][1], coners[j][1][1]], [coners[j][0][0], coners[j][1][0]], color='g', linestyle='-', linewidth=1) ax.plot([coners[j][1][1], coners[j][2][1]], [coners[j][1][0], coners[j][2][0]], color='g', linestyle='-', linewidth=1) ax.plot([coners[j][2][1], coners[j][3][1]], [coners[j][2][0], coners[j][3][0]], color='g', linestyle='-', linewidth=1) ax.plot([coners[j][3][1], coners[j][0][1]], [coners[j][3][0], coners[j][0][0]], color='g', linestyle='-', linewidth=1) ax.set_xlim(0, im.shape[1]) ax.set_ylim(0, im.shape[0]) plt.gca().invert_yaxis() plt.show() #%% patch_num = 750
# load that file fname = siftdir + fnames[i] mat = scipy.io.loadmat(fname) numfeats = mat['descriptors'].shape[0] #read the associated image imname = framesdir + fnames[i][:-4] im = misc.imread(imname) # display the image and its SIFT features drawn as squares print 'imname = %s contains %d total features, each of dimension %d' %(imname, numfeats, mat['descriptors'].shape[1]); fig=plt.figure() ax=fig.add_subplot(111) ax.imshow(im) coners = displaySIFTPatches(mat['positions'], mat['scales'], mat['orients']) for j in range(len(coners)): ax.plot([coners[j][0][1], coners[j][1][1]], [coners[j][0][0], coners[j][1][0]], color='g', linestyle='-', linewidth=1) ax.plot([coners[j][1][1], coners[j][2][1]], [coners[j][1][0], coners[j][2][0]], color='g', linestyle='-', linewidth=1) ax.plot([coners[j][2][1], coners[j][3][1]], [coners[j][2][0], coners[j][3][0]], color='g', linestyle='-', linewidth=1) ax.plot([coners[j][3][1], coners[j][0][1]], [coners[j][3][0], coners[j][0][0]], color='g', linestyle='-', linewidth=1) ax.set_xlim(0, im.shape[1]) ax.set_ylim(0, im.shape[0]) plt.gca().invert_yaxis() plt.show() # now display the same image but only show N of the features print 'now showing a random subset of %d of those for visualization purposes only...' %(N) randinds = np.random.permutation(numfeats) fig=plt.figure()
matches = [] for descriptor in range(len(mat['descriptors2'])): for i in ind: vector1 = np.array(mat['descriptors1'][i]).reshape((-1, 1)) vector2 = np.array(mat['descriptors2'][descriptor]).reshape((-1, 1)) distance = dist2(np.array(vector1), np.array(vector2)).mean() if distance < .0075: matches.append(descriptor) break print len(matches) fig = plt.figure() bx = fig.add_subplot(111) bx.imshow(im2) coners = displaySIFTPatches(mat['positions2'][matches, :], mat['scales2'][matches, :], mat['orients2'][matches, :]) for j in range(len(coners)): bx.plot([coners[j][0][1], coners[j][1][1]], [coners[j][0][0], coners[j][1][0]], color='g', linestyle='-', linewidth=1.5) bx.plot([coners[j][1][1], coners[j][2][1]], [coners[j][1][0], coners[j][2][0]], color='g', linestyle='-', linewidth=1.5) bx.plot([coners[j][2][1], coners[j][3][1]], [coners[j][2][0], coners[j][3][0]],
#select a region within image 1 print ('use the mouse to draw a polygon, right click to end it') pl.imshow(im1) MyROI = roipoly(roicolor='r') #run this later___the blocking feature is not working in my IDE Ind = MyROI.getIdx(im1, im1_mat['positions']) print("features in region", len(Ind)) #from load data script - run this to view the SIFT features in image1 fig=plt.figure() bx=fig.add_subplot(111) bx.imshow(im1) coners = displaySIFTPatches(im1_mat['positions'][Ind,:], im1_mat['scales'][Ind,:], im1_mat['orients'][Ind,:]) for j in range(len(coners)): bx.plot([coners[j][0][1], coners[j][1][1]], [coners[j][0][0], coners[j][1][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][1][1], coners[j][2][1]], [coners[j][1][0], coners[j][2][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][2][1], coners[j][3][1]], [coners[j][2][0], coners[j][3][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][3][1], coners[j][0][1]], [coners[j][3][0], coners[j][0][0]], color='g', linestyle='-', linewidth=1) bx.set_xlim(0, im1.shape[1]) bx.set_ylim(0, im1.shape[0]) plt.gca().invert_yaxis() plt.show() # ## extract the image patch # #patch_num = 1 #img_patch = getPatchFromSIFTParameters(mat['positions'][patch_num,:], mat['scales'][patch_num], mat['orients'][patch_num], rgb2gray(im1))
fname = siftdir + fnames[i] mat = scipy.io.loadmat(fname) numfeats = mat['descriptors'].shape[0] #read the associated image imname = framesdir + fnames[i][:-4] im = misc.imread(imname) # display the image and its SIFT features drawn as squares print 'imname = %s contains %d total features, each of dimension %d' % ( imname, numfeats, mat['descriptors'].shape[1]) fig = plt.figure() ax = fig.add_subplot(111) ax.imshow(im) coners = displaySIFTPatches(mat['positions'], mat['scales'], mat['orients']) for j in range(len(coners)): ax.plot([coners[j][0][1], coners[j][1][1]], [coners[j][0][0], coners[j][1][0]], color='g', linestyle='-', linewidth=1) ax.plot([coners[j][1][1], coners[j][2][1]], [coners[j][1][0], coners[j][2][0]], color='g', linestyle='-', linewidth=1) ax.plot([coners[j][2][1], coners[j][3][1]], [coners[j][2][0], coners[j][3][0]], color='g',
pos1 = contents['positions1'] scales1 = contents['scales1'] orients1 = contents['orients1'] desc1 = contents['descriptors1'] im2 = contents['im2'] pos2 = contents['positions2'] scales2 = contents['scales2'] orients2 = contents['orients2'] fig, ax = plt.subplots() ax.imshow(im1) roiplotter = roipoly(roicolor='r') fig, ax = plt.subplots() ax.imshow(im1) roiplotter.displayROI() plt.savefig("roi.png") plt.show() indices = roiplotter.getIdx(im1, pos1) # np.save('desc_chosen', indices) desc_chosen = desc1[indices] desc2 = contents['descriptors2'] distances = dist2(desc_chosen, desc2) matches = np.argmin(distances, axis=1) displaySIFTPatches(im2, pos2[matches], scales2[matches], orients2[matches]) plt.savefig("desc_match.png") plt.show()
def rawDescriptorMatches(fileName): mat = scipy.io.loadmat(fileName) #numfeats=mat['im1'].shape[0] #numdimention=mat[im1].shape[1] #print(mat) im1 = mat['im1'] fig = plt.figure() #ax=fig.add_subplot(111) #ax.imshow(im1) # now show how to select a subset of the features using polygon drawing print('nuse the mouse to draw a polygon, right click to end it') plt.imshow(im1) MyROI = roipoly(roicolor='r') # Ind contains the indices of the SIFT features whose centers fall Ind = MyROI.getIdx(im1, mat['positions1']) descriptor_1 = mat['descriptors1'][Ind] print(descriptor_1.shape) descriptor_2 = mat['descriptors2'] distance_list = np.empty((0, 2)) for i in descriptor_1: sift_descriptor_vector = np.array(i, ndmin=2) distance = dist2(sift_descriptor_vector, descriptor_2) distance_list = np.vstack( (distance_list, [np.amin(distance), np.argmin(distance)])) print("shape of distance_list", distance_list.shape) distance_mean = np.mean(distance_list[:, 0], axis=0) distance_list_mask = distance_list[:, 0] < (distance_mean * ratio) print("shape of distance_mask", distance_list_mask.shape) distance_list = distance_list[distance_list_mask, :] print("shape of after mask", distance_list.shape) distance_array = np.array(distance_list, dtype=np.uint) im2 = mat['im2'] fig = plt.figure() bx = fig.add_subplot(111) #coners = displaySIFTPatches(mat['positions2'][min_distance], mat['scales2'][min_distance], mat['orients2'][min_distance]) coners = displaySIFTPatches(mat['positions2'][distance_array[:, 1], :], mat['scales2'][distance_array[:, 1], :], mat['orients2'][distance_array[:, 1], :]) #coners = displaySIFTPatches(mat['positions2'][tlist[:, 1], :], mat['scales2'][tlist[:, 1], :], # mat['orients2'][tlist[:, 1], :]) for j in range(len(coners)): bx.plot([coners[j][0][1], coners[j][1][1]], [coners[j][0][0], coners[j][1][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][1][1], coners[j][2][1]], [coners[j][1][0], coners[j][2][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][2][1], coners[j][3][1]], [coners[j][2][0], coners[j][3][0]], color='g', linestyle='-', linewidth=1) bx.plot([coners[j][3][1], coners[j][0][1]], [coners[j][3][0], coners[j][0][0]], color='g', linestyle='-', linewidth=1) bx.set_xlim(0, im2.shape[1]) bx.set_ylim(0, im2.shape[0]) plt.gca().invert_yaxis() bx.imshow(im2) plt.show()
# load that file mat = scipy.io.loadmat(os.path.join(siftdir, fname)) num_feats = mat["descriptors"].shape[0] print(f"{fname} contains {num_feats} features", flush=True) print(f"feature dimension: {mat['descriptors'].shape[1]}", flush=True) # read the associated image im = imageio.imread(os.path.join(framesdir, fname[:-4])) # display the image and its SIFT features drawn as squares fig = plt.figure() ax = fig.add_subplot(111) ax.imshow(im) coners = displaySIFTPatches(mat["positions"], mat["scales"], mat["orients"]) for j in range(len(coners)): ax.plot( [coners[j][0][1], coners[j][1][1]], [coners[j][0][0], coners[j][1][0]], color="g", linestyle="-", linewidth=1, ) ax.plot( [coners[j][1][1], coners[j][2][1]], [coners[j][1][0], coners[j][2][0]], color="g", linestyle="-", linewidth=1, )