def rotate_match(img): """ Args: img: numpy array representing the image with size (H, W, 3) Returns: match_count: Be in type of list. Each item is a tuple (count of correct matches, degree) """ h, w, _ = img.shape match_count = [] for deg in range(0, 360, 10): M = cv2.getRotationMatrix2D((w // 2, h // 2), deg, 1) img2 = cv2.warpAffine(img, M, (max(h, w), max(h, w))) locs1, desc1 = BRIEF.briefLite(img) locs2, desc2 = BRIEF.briefLite(img2) matches = BRIEF.briefMatch(desc1, desc2) BRIEF.plotMatches(img, img2, matches, locs1, locs2) match_count.append((matches.shape[0], deg)) return match_count
def initProcessImage(image): global isInitialized global old_descriptors global my_oldDescriptors global old_keypoints global my_oldKeypoints_wHarris global current_matches global my_currentMatches global keypoint_counter global my_keypointCounter old_frame = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) old_keypoints, old_descriptors = feature_detector.detectAndCompute( old_frame, None) my_oldKeypoints = FAST9(old_frame, threshold=40, n=9) old_harrisMeasures = HARRIS(old_frame, my_oldKeypoints, k=0.04, offset=3) my_oldKeypoints_wHarris=np.column_stack((my_oldKeypoints,\ old_harrisMeasures)) my_oldKeypoints_wHarris=my_oldKeypoints_wHarris[\ my_oldKeypoints_wHarris[:,2].argsort()[::-1]] my_oldKeypoints_wHarris = my_oldKeypoints_wHarris[0:80, :] my_oldDescriptors = BRIEF(old_frame, my_oldKeypoints_wHarris, kernel=2, l=256, S=31) keypoint_counter = len(old_keypoints) my_keypointCounter = my_oldKeypoints_wHarris.shape[0] current_matches = dict((i, i) for i in xrange(len(old_keypoints))) my_currentMatches = dict( (i, i) for i in xrange(my_oldKeypoints_wHarris.shape[0])) isInitialized = True print 'Initializing frame 1' print 'my_keypointCounter', my_keypointCounter print 'my_currentMatches', my_currentMatches print 'my_oldKeypoints_wHarris', my_oldKeypoints_wHarris '''
import numpy as np import cv2 import matplotlib.pyplot as plt import BRIEF as br if __name__ == '__main__': test_pattern_file = '../results/testPattern.npy' compareX, compareY = np.load(test_pattern_file) compareX, compareY = br.makeTestPattern() match_count = [] for i in range(0, 360, 10): im1 = cv2.imread('../data/model_chickenbroth.jpg') im2 = cv2.imread('../data/model_chickenbroth.jpg') rows, cols, _ = im2.shape M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -i, 1) im2 = cv2.warpAffine(im2, M, (cols, rows)) locs1, desc1 = br.briefLite(im1) locs2, desc2 = br.briefLite(im2) matches = br.briefMatch(desc1, desc2) match_count.append(matches.shape[0]) match_count = np.asarray(match_count) plt.bar(np.arange(0, 360, 10), match_count) plt.xlabel('Degree of rotation', fontsize=15) plt.ylabel('No of matches', fontsize=15) plt.show() plt.close()
import numpy as np import cv2 from matplotlib import pyplot as plt from scipy.spatial.distance import cdist from keypointDetect import DoGdetector import BRIEF im = cv2.imread('../data/model_chickenbroth.jpg') dim = np.array([im.shape[1], im.shape[0]]) center = dim / 2 angle = np.array([]) numMatches = np.array([]) for i in range(0, 361, 10): rotMat = cv2.getRotationMatrix2D(tuple(center), i, 1) rotIm = cv2.warpAffine(im, rotMat, tuple(dim)) locs1, desc1 = BRIEF.briefLite(im) locs2, desc2 = BRIEF.briefLite(rotIm) matches = BRIEF.briefMatch(desc1, desc2) angle = np.append(angle, i) matchNum = matches.shape[0] #print (matchNum) numMatches = np.append(numMatches, matchNum) #cv2.imshow('a',rotIm) #cv2.waitKey(0) plt.bar(angle, numMatches, alpha=1) #plt.xticks(angle) plt.ylabel('Matches') plt.savefig('../results/rotation_matches.png') #plt.title('Brief number of matches v/s angle of rotation')
import numpy as np import cv2 import os from keypointDetect import DoGdetector import BRIEF import matplotlib.pyplot as plt img = cv2.imread('../data/model_chickenbroth.jpg') height = img.shape[0] width = img.shape[1] image_center = (width / 2, height / 2) locs, descs = BRIEF.briefLite(img) angles = range(0, 190, 10) print(angles) total_matches = [] for n in angles: rot_img = cv2.getRotationMatrix2D(image_center, n, 1) affine_img = cv2.warpAffine(img, rot_img, (height, width)) locs1, desc1 = BRIEF.briefLite(affine_img) inter_matches = BRIEF.briefMatch(descs, desc1) total_matches.append(inter_matches.shape[0]) plt.bar(angles, total_matches, align='center') plt.xlabel('Rotation angle (in degree)') plt.ylabel('No of matches') plt.title('Test with an image and its rotations')
import numpy as np import cv2 import BRIEF import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt if __name__ == '__main__': compareX, compareY = BRIEF.makeTestPattern() # test briefLite im = cv2.imread('../data/model_chickenbroth.jpg') locs, desc = BRIEF.briefLite(im) dst = cv2.imread('../data/model_chickenbroth.jpg') rotMatches = [] rows = dst.shape[0] cols = dst.shape[1] locsd, descd = BRIEF.briefLite(dst) matches = BRIEF.briefMatch(desc, descd) rotMatches.append(matches.shape[0]) fig = plt.figure(0) tmp = fig.add_subplot(6, 6, 1) plt.imshow(dst) for i in range(35): M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 10 * (i + 1), 1) dst_new = cv2.warpAffine(dst, M, (cols, rows)) locsd, descd = BRIEF.briefLite(dst_new) matches = BRIEF.briefMatch(desc, descd) rotMatches.append(matches.shape[0]) tmp = fig.add_subplot(6, 6, i + 2)
#Sort in descending order of harris measure keypoints_wHarris=np.column_stack((keypoints,harris_measures)) print 'Sorting and printing keypoints with top Harris measure...' print 'Top Harris measure keypoints shown in red in image' keypoints_wHarris=keypoints_wHarris[keypoints_wHarris[:,2].argsort()[::-1]] #print 'keypoints_wHarris',keypoints_wHarris # Part III:Orientation for FAST print 'Finding orientation of keypoints...' thetas=orientation(im[j,:,:],keypoints_wHarris,offset) okeypoints_wHarris=np.column_stack((keypoints_wHarris,thetas)) #print okeypoints_wHarris # Part IV: BRIEF # Get BRIEF descriptor descriptors=BRIEF(im[j,:,:],keypoints_wHarris,kernel,l,S) ''' #Part V: Plotting the results plt.gray() implot = plt.imshow(im[j,:,:]) plt.scatter(x=keypoints[0:,1], y=keypoints[0:,0], c='g', s=10) plt.scatter(x=keypoints_wHarris[0:80,1], y=keypoints_wHarris[0:80,0], c='r', s=10) #plt.title('FAST9 with %f threshold on %s.jpeg' \ # % (float(threshold), filename)) plt.show() ''' #FAST keypoints keypoints0 = FAST9(im[0,:,:], threshold, n)
import cv2 import BRIEF import matplotlib.pyplot as plt im1 = cv2.imread('../data/model_chickenbroth.jpg') im2 = cv2.imread( '../data/model_chickenbroth.jpg') #'../data/chickenbroth_01.jpg') locs1, desc1 = BRIEF.briefLite(im1) #print("desc:") #print(desc1.shape) thetas = range(0, 360, 10) matchn = [] for theta in range(0, 360, 10): rot_mat = cv2.getRotationMatrix2D((im2.shape[1] // 2, im2.shape[0] // 2), theta, 1) rot_im2 = cv2.warpAffine(im2, rot_mat, (im2.shape[1], im2.shape[0])) locs2, desc2 = BRIEF.briefLite(rot_im2) matches = BRIEF.briefMatch(desc1, desc2) matchn.append(matches.shape[0]) plt.bar(thetas, matchn) plt.show()
import cv2 import skimage.io import numpy as np import BRIEF import matplotlib.pyplot as plt image_path = '../data/model_chickenbroth.jpg' img = skimage.io.imread(image_path) match_num = np.zeros(37) for i in range(int(360 / 10) + 1): im1 = img cols = img.shape[0] rows = img.shape[1] M = cv2.getRotationMatrix2D((cols / 2, rows / 2), i * 10, 1) im2 = cv2.warpAffine(im1, M, (rows, cols)) locs1, desc1 = BRIEF.briefLite(im1) locs2, desc2 = BRIEF.briefLite(im2) match_num[i] = BRIEF.briefMatch(desc1, desc2).shape[0] plt.bar(range(len(match_num)), match_num) plt.show()
def processImage(image, plot=False): global old_descriptors global my_oldDescriptors global old_keypoints global my_oldKeypoints_wHarris global current_matches global my_currentMatches global image_number global keypoint_counter global my_keypointCounter print 'image number', image_number if not isInitialized: initProcessImage(image) return image new_frame = image new_frame = cv2.cvtColor(new_frame, cv2.COLOR_BGR2GRAY) new_keypoints, new_descriptors = feature_detector.detectAndCompute( new_frame, None) my_newKeypoints = FAST9(new_frame, threshold=40, n=9) new_harrisMeasures = HARRIS(new_frame, my_newKeypoints, k=0.04, offset=3) my_newKeypoints_wHarris=np.column_stack((my_newKeypoints,\ new_harrisMeasures)) my_newKeypoints_wHarris=my_newKeypoints_wHarris[\ my_newKeypoints_wHarris[:,2].argsort()[::-1]] my_newKeypoints_wHarris = my_newKeypoints_wHarris[0:80, :] my_newDescriptors = BRIEF(new_frame, my_newKeypoints_wHarris, kernel=2, l=256, S=31) print 'old keypoints', my_oldKeypoints_wHarris[0:5, :] print 'new keypoints', my_newKeypoints_wHarris[0:5, :] ''' plt.gray() implot = plt.imshow(new_frame) plt.scatter(x=my_newKeypoints[0:,1], y=my_newKeypoints[0:,0], c='g', s=10) plt.scatter(x=my_newKeypoints_wHarris[0:,1], y=my_newKeypoints_wHarris[0:,0], c='r', s=10) plt.show() ''' if new_descriptors is not None and old_descriptors is not None: matches = matcher.match(new_descriptors, old_descriptors) my_matches=match_keypoints(my_oldDescriptors, my_newDescriptors,\ thold_hamming = 64) #print 'my_matches',my_matches inliers, model = refine_match(my_oldKeypoints_wHarris,\ my_newKeypoints_wHarris,\ my_matches) my_matches = my_matches[inliers, :] print 'my_matches', my_matches else: matches = [] # Each match produces: # queryIdx - index of keypoint in new_keypoints # trainIdx - index of keypoint in old_keypoints # Two possibilities for each match # (1) The match is new # Meaning: we haven't tracked that keypoint before # (2) The match is a continuation # meaning: we've tracked that keypoint before next_matches = {} my_nextMatches = {} for match in matches: if match.trainIdx in current_matches: keypoint_no = current_matches[match.trainIdx] current_index = match.queryIdx next_matches[current_index] = keypoint_no else: keypoint_no = keypoint_counter keypoint_counter += 1 current_index = match.queryIdx next_matches[current_index] = keypoint_no for k in range(0, my_matches.shape[0]): if my_matches[k, 0] in my_currentMatches: #print 'my_matches[k,0]',my_matches[k,0] my_keypointNo = my_currentMatches[my_matches[k, 0]] #print 'my_keypointNo',my_keypointNo my_currentIndex = my_matches[k, 1] #print 'my_currentIndex',my_currentIndex my_nextMatches[my_currentIndex] = my_keypointNo #print 'my_nextMatches[my_currentIndex]',my_nextMatches[my_currentIndex] else: my_keypointNo = my_keypointCounter my_keypointCounter += 1 my_currentIndex = my_matches[k, 1] my_nextMatches[my_currentIndex] = my_keypointNo current_matches = next_matches old_keypoints, old_descriptors = new_keypoints, new_descriptors my_currentMatches = my_nextMatches my_oldKeypoints_wHarris, my_oldDescriptors = my_newKeypoints_wHarris,\ my_newDescriptors image_number += 1 #print 'image_number',image_number image_frame = drawFeaturePoints(image, current_matches, new_keypoints) image_frame = my_drawFeaturePoints(image_frame, my_matches,\ my_newKeypoints_wHarris) if image_number == 1: imwrite('frame1.jpg', image_frame) if image_number == 100: imwrite('frame100.jpg', image_frame) if image_number == 200: imwrite('frame200.jpg', image_frame) if image_number == 300: imwrite('frame300.jpg', image_frame) #print 'my current_matches',my_currentMatches #print 'my_currentIndex',my_currentIndex #image_frame = my_drawFeaturePoints(image_frame, my_currentMatches,\ # my_newKeypoints_wHarris) if plot: plt.imshow(image_frame) plt.show() return image_frame
Spyder Editor This is a temporary script file. """ #from math import * from PIL import Image #import numpy as np import os import BRIEF #import scipy.ndimage # charger image en niveau de gris Liste = os.listdir('Image_data') I = Image.open('Image_data/'+Liste[1]).convert('L') I_crop=I.crop((0,0,50,50)) # function parameterd S=20 Nd=10 d=BRIEF.BRIEF(I_crop,S,Nd)