def reginQueries(): #get kmeans with open('cluster_question3.pkl', "rb") as f: kmeans = pickle.load(f) #get histogram and histogram mapping with open("histogram.pkl", 'rb') as f: histogram = pickle.load(f) histogram = np.array(histogram) print(histogram.shape) with open("histogram_mapping.pkl", 'rb') as f: histogram_mappping = pickle.load(f) print(len(histogram_mappping)) #get selected region framesdir = 'frames/' siftdir = 'sift/' histogram_choosed=[] hist_choosed=np.zeros(k) fnames = glob.glob(siftdir + '*.mat') fnames = [i[-27:] for i in fnames] fname = siftdir + fnames[ID] mat = scipy.io.loadmat(fname, verify_compressed_data_integrity=False) imname = framesdir + fnames[ID][:-4] im = imread(imname) pl.imshow(im) MyROI = roipoly(roicolor='r') Ind = MyROI.getIdx(im, mat['positions']) # get parameters describtors = mat['descriptors'][Ind, :] if len(describtors) > 0: predict_result = kmeans.predict(describtors) for label in predict_result: hist_choosed[label] += 1 histogram_choosed.append(hist_choosed) histogram_choosed = np.array(histogram_choosed) return histogram,histogram_mappping,histogram_choosed
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()
def regionQueries(): # get the 4 images and sifts dir = 'regionQueries/' querySiftNames = glob.glob(dir + '*.mat') queryImageNames = glob.glob(dir + '*.jpeg') querySifts = [] queryImages = [] for i in range(len(querySiftNames)): querySifts.append(scipy.io.loadmat(querySiftNames[i])) queryImages.append(misc.imread(queryImageNames[i])) # for each of them for i in range(len(querySifts)): image = queryImages[i] sift = querySifts[i] # select a region plt.imshow(image) MyROI = roipoly(roicolor='r') Ind = MyROI.getIdx(image, sift['positions']) # get the descriptors selectedDescriptors = sift['descriptors'][Ind] # build histogram queryHistogram = buildHistogram(selectedDescriptors) # initialize dictionary similarityMap = {} # for each of the images imagesdir = '100sift/' imageNames = glob.glob(imagesdir + '*.mat') for imageName in imageNames: print imageName imageSift = scipy.io.loadmat(imageName) # build histogram imageHistogram = buildHistogram(imageSift['descriptors']) # get similarity values similarity = getSimilarityValue(queryHistogram, imageHistogram) # put in the dictionary similarityMap[imageName] = similarity # sort dictionary sorted_similarityMap = sorted(similarityMap.items(), key=operator.itemgetter(1)) sorted_similarityMap.reverse() # take top 5 to display top5Images = [] for m in range(5): name = sorted_similarityMap[m][0] name = name[-27:-4] top5Images.append(name) displayImageList(top5Images)
def gen_regions(query_imnames): for query_imname in query_imnames: query_im = Image.open(os.path.join(FRAME_DIR, query_imname)) desc, _ = get_desc_imname(query_imname + ".mat") pos, _, _ = get_keypoints(query_imname + ".mat") fig, ax = plt.subplots() ax.imshow(query_im) roi_plotter = roipoly(roicolor='r') fig, ax = plt.subplots() ax.imshow(query_im) roi_plotter.displayROI() plt.savefig(query_imname + "_reg_query.png") plt.show() indices = roi_plotter.getIdx(query_im, pos) np.save(query_imname + '_reg_ind', indices)
import scipy.io import matplotlib.pyplot as plt import pylab as pl from selectRegion import roipoly from displaySIFTPatches import displaySIFTPatches 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)
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, im.shape[1]) bx.set_ylim(0, im.shape[0]) plt.gca().invert_yaxis() plt.show() # 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' pl.imshow(im) MyROI = roipoly(roicolor='r') Ind = MyROI.getIdx(im, mat['positions']) # Ind contains the indices of the SIFT features whose centers fall # within the selected region of interest. # Note that these indices apply to the *rows* of 'descriptors' and # 'positions', as well as the entries of 'scales' and 'orients' # now display the same image but only in the polygon. fig=plt.figure() bx=fig.add_subplot(111) bx.imshow(im) coners = displaySIFTPatches(mat['positions'][Ind,:], mat['scales'][Ind,:], 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)
import numpy as np import scipy.io import matplotlib.pyplot as plt from displaySIFTPatches import displaySIFTPatches from selectRegion import roipoly from dist2 import dist2 mat = scipy.io.loadmat('twoFrameData.mat') im1 = mat['im1'] im2 = mat['im2'] plt.imshow(im1) MyROI = roipoly(roicolor='r') ind = MyROI.getIdx(im1, mat['positions1']) 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, :],
if __name__ == '__main__': contents = sio.loadmat("./twoFrameData.mat") im1 = contents['im1'] 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)
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()
import numpy as np import scipy.io import matplotlib.pyplot as plt from selectRegion import roipoly import imageio if __name__ == "__main__": frame_num = int(input("Frame Num: ")) im = np.array( imageio.imread("PS4Frames/frames/friends_{}.jpeg".format( str(frame_num).zfill(10)))).astype(np.uint8) mat = scipy.io.loadmat("PS4SIFT/sift/friends_{}.jpeg.mat".format( str(frame_num).zfill(10))) plt.show() plt.imshow(im) plt.title("Select Region of Interest") roi = roipoly(color="r") region = np.array((roi.all_x_points, roi.all_y_points)).T indices = roi.get_indices(im, mat["positions"]) np.save("region-{}.npy".format(frame_num), region) np.save("points-{}.npy".format(frame_num), indices)