def getGauss(size,i): sigma = 1.2 if i == 0: return h.gauss2x(size,sigma) if i == 1: return h.gauss2y(size,sigma) if i == 2: return h.gauss2xy(size,sigma) if i == 3: return h.gaussdx(size,sigma) if i == 4: return h.gaussdy(size,sigma) if i == 5: return h.gauss(size,sigma)
def getGauss(size,i): sigma = 1.2 if i == 0: return h.gauss(9,sigma) if i == 1: return h.gauss(15,sigma) if i == 2: return h.gauss(21,sigma) if i == 3: return h.gauss(27,sigma) if i == 4: return h.gauss(39,sigma) if i == 5: return h.gauss(51,sigma) if i == 6: return h.gauss(75,sigma) if i == 7: return h.gauss(99,sigma)
def getGauss(size,i): """ Input : size of filter, sigma Output: retrns filter """ sigma = 1.2 if i == 0: return h.gauss2x(size,sigma) if i == 1: return h.gauss2y(size,sigma) if i == 2: return h.gauss2xy(size,sigma) if i == 3: return h.gaussdx(size,sigma) if i == 4: return h.gaussdy(size,sigma) if i == 5: return h.gauss(size,sigma)
def surf_descriptor(I_name, keypoints): """ input: [y, x, s], name of picture output: Descriptor """ I_bw = cv2.imread(I_name, 0) I = cv2.imread(I_name) I_int = cv2.integral(I_bw) keypoint_descriptor = [] final_keypoints = [] print("starting descriptor") # Removing keypoints if the keypoint collecting region is outside the image for p in keypoints: hs = haar_size(p[2]) max_size_y = len(I_bw) - (20 * p[2] + hs) max_size_x = len(I_bw[0]) - (20 * p[2] + hs) min_size_x = min_size_y = round_four(20 * p[2]) + hs #print("maxsize y = " + str(max_size_y), "maxsize x " + str(max_size_x), "minsizex/y = " + str(min_size_y)) if ((p[0] < max_size_y) and (min_size_y < p[0]) and \ (p[1] < max_size_x) and (min_size_x < p[1])): cv2.circle(I, (p[1].astype(int), p[0].astype(int)), int(round(p[2])), (0, 0, 255), 3) final_keypoints.append([p[0], p[1], p[2]]) cv2.imwrite("zzdes-" + I_name[:-4] + ".jpg", I) print("length of final_keypoints:" + str(len(final_keypoints))) #keypoint area = [y, x, s] for p_a in final_keypoints: region_size = round_four(20 * p_a[2]) sub_region_size = region_size / 4.0 descriptor = np.zeros([64, 1]) d_x = np.zeros([20, 20]) d_y = np.zeros([20, 20]) """ a g b ###### ###### e ##p### f ###### ###### ###### c gh d the integral square aecg subtracted from fbhd gives the dx-haar-wavelet response """ # Calculating the haar wavelet response, in a 20x20 window haar_hs = haar_size(p_a[2]) / 2 incre = (sub_region_size / 5.0) / 5.0 y1 = -region_size / 2 + int(p_a[0]) for y in range(0, 20): x1 = -region_size / 2 + int(p_a[1]) for x in range(0, 20): #print(y1, region_size/2 + int(p_a[1])) #print(y1- haar_hs + 1, x1 + haar_hs - 1) #print("y = " + str(y1), "x = " + str(x1), "scale = " + str(p[2]), "hs = " + str(hs)) #print("y = " + str(I_int[y1,x1])) #print(haar_hs, incre) if (y1 < 0 or x1 < 0): print(y1, x1) print("y=" + str(p_a[0]), "x=" + str(p_a[1]), "scale=" + str(p[2])) a = I_int[y1 - haar_hs, x1 - haar_hs] b = I_int[y1 - haar_hs, x1 + haar_hs] c = I_int[y1 + haar_hs, x1 - haar_hs] d = I_int[y1 + haar_hs, x1 + haar_hs] e = I_int[y1, x1 - haar_hs] f = I_int[y1, x1 + haar_hs] g = I_int[y1 - haar_hs, x1] h = I_int[y1 + haar_hs, x1] d_x[y, x] = (h + a - c - g) - (d + g - h - b) d_y[y, x] = (f + a - e - b) - (d + e - c - f) x1 += incre y1 += incre d_x = np.multiply(h2.gauss(len(d_x), 3.3 * p[2]), d_x) d_y = np.multiply(h2.gauss(len(d_y), 3.3 * p[2]), d_y) #if ((p_a[0] == 712 and p_a[1] == 1514) or (p_a[0] == 530 and p_a[1] == 327)): # np.set_printoptions(precision=5) # print(d_x) # print(d_y) i = 0 for y in range(0, 4): for x in range(0, 4): field_dx = np.sum(d_x[y * 5:(y + 1) * 5, x * 5:(x + 1) * 5]) field_dy = np.sum(d_y[y * 5:(y + 1) * 5, x * 5:(x + 1) * 5]) field_abs_dx = np.sum( np.abs(d_x[y * 5:(y + 1) * 5, x * 5:(x + 1) * 5])) field_abs_dy = np.sum( np.abs(d_y[y * 5:(y + 1) * 5, x * 5:(x + 1) * 5])) descriptor[i + 0] = field_dx descriptor[i + 1] = field_dy descriptor[i + 2] = field_abs_dx descriptor[i + 3] = field_abs_dy i += 4 keypoint_descriptor.append( [p_a, descriptor / np.linalg.norm(descriptor)]) #print("length of keypoint descriptor: " + str(len(keypoint_descriptor))) #print("\n") return (keypoint_descriptor)
def surf_descriptor_w_orientation(I_name, keypoints): """ input: [y, x, s, o], name of picture output: Descriptor """ I_bw = cv2.imread(I_name, 0) I = cv2.imread(I_name) I_int = cv2.integral(I_bw) keypoint_descriptor = [] final_keypoints = [] print("starting descriptor") # Removing keypoints if the keypoint collecting region is outside the image for p_a in keypoints: hs = haar_size(p_a[2]) max_size_y = len(I_bw) - (20 * p_a[2] + hs) max_size_x = len(I_bw[0]) - (20 * p_a[2] + hs) min_size_x = min_size_y = round_four(20 * p_a[2]) + hs #print("maxsize y = " + str(max_size_y), "maxsize x " + str(max_size_x), "minsizex/y = " + str(min_size_y)) if ((p_a[0] < max_size_y) and (min_size_y < p_a[0]) and \ (p_a[1] < max_size_x) and (min_size_x < p_a[1])): cv2.circle(I, (p_a[1].astype(int), p_a[0].astype(int)), int(round(p_a[2])), (0,0,255), 3) final_keypoints.append([p_a[0], p_a[1], p_a[2], p_a[3]]) cv2.imwrite("zzdes-" + I_name[:-4] + ".jpg", I) print("length of final_keypoints:" + str(len(final_keypoints))) #keypoint area = [y, x, s] counter = 1 for p_a in final_keypoints: col, row = np.shape(I_bw) rotmat = cv2.getRotationMatrix2D((p_a[1], p_a[0]), p_a[3], 1) dst = cv2.warpAffine(I_bw, rotmat, (int(row), int(col))) I_int = cv2.integral(dst) print(counter) counter += 1 region_size = round_four(20 * p_a[2]) sub_region_size = region_size/4.0 descriptor = np.zeros([64, 1]) d_x = np.zeros([20,20]) d_y = np.zeros([20,20]) """ a g b ###### ###### e ##p### f ###### ###### ###### c gh d the integral square aecg subtracted from fbhd gives the dx-haar-wavelet response """ # Calculating the haar wavelet response, in a 20x20 window haar_hs = haar_size(p_a[2]) / 2 incre = (sub_region_size/5.0)/5.0 y1 = -region_size/2 + int(p_a[0]) for y in range(0, 20): x1 =-region_size/2 + int(p_a[1]) for x in range(0, 20): #print(y1, region_size/2 + int(p_a[1])) #print(y1- haar_hs + 1, x1 + haar_hs - 1) #print("y = " + str(y1), "x = " + str(x1), "scale = " + str(p[2]), "hs = " + str(hs)) #print("y = " + str(I_int[y1,x1])) #print(haar_hs, incre) if (y1 < 0 or x1 < 0): print(y1,x1) print("y="+str(p_a[0]),"x="+str(p_a[1]), "scale="+str(p[2])) a = I_int[y1 - haar_hs, x1 - haar_hs] b = I_int[y1 - haar_hs, x1 + haar_hs] c = I_int[y1 + haar_hs, x1 - haar_hs] d = I_int[y1 + haar_hs, x1 + haar_hs] e = I_int[y1, x1 - haar_hs] f = I_int[y1, x1 + haar_hs] g = I_int[y1 - haar_hs, x1] h = I_int[y1 + haar_hs, x1] d_x[y, x] = (h + a - c - g) - (d + g - h - b) d_y[y, x] = (f + a - e - b) - (d + e - c - f) x1 += incre y1 += incre d_x = np.multiply(h2.gauss(len(d_x), 3.3 * p_a[2]), d_x) d_y = np.multiply(h2.gauss(len(d_y), 3.3 * p_a[2]), d_y) #if ((p_a[0] == 712 and p_a[1] == 1514) or (p_a[0] == 530 and p_a[1] == 327)): # np.set_printoptions(precision=5) # print(d_x) # print(d_y) i = 0 for y in range(0, 4): for x in range(0, 4): field_dx = np.sum(d_x[y * 5 : (y+1) * 5, x * 5 : (x+1) * 5]) field_dy = np.sum(d_y[y * 5 : (y+1) * 5, x * 5 : (x+1) * 5]) field_abs_dx = np.sum(np.abs(d_x[y * 5 : (y+1) * 5, x * 5 : (x+1) * 5])) field_abs_dy = np.sum(np.abs(d_y[y * 5 : (y+1) * 5, x * 5 : (x+1) * 5])) descriptor[i + 0] = field_dx descriptor[i + 1] = field_dy descriptor[i + 2] = field_abs_dx descriptor[i + 3] = field_abs_dy i += 4 keypoint_descriptor.append([p_a[:-1], descriptor / np.linalg.norm(descriptor)]) #print("length of keypoint descriptor: " + str(len(keypoint_descriptor))) #print("\n") return(keypoint_descriptor)
def sift_orientation(I_name, points): window_size = 16 """ Input : image I, interest points, size of window Output: assigns an orientation to the interest points """ k_con = math.sqrt(2) k = math.sqrt(2) """ sigma1 = np.array([k_con/2, 1.0, k_con , k_con ** 2, k_con ** 3, \ k_con, k_con ** 2, k_con ** 3, k_con ** 4, k_con ** 5, \ k_con ** 3, k_con ** 4, k_con ** 5, k_con ** 6, k_con ** 7, \ k_con ** 5, k_con ** 6, k_con ** 7, k_con ** 8, k_con ** 9]) """ sigma1 = np.array([1.3, 1.6, 1.6 * k, 1.6 * k ** 2, 1.6 * k ** 3, \ 1.6 * k, 1.6 * k ** 2, 1.6 * k ** 3, 1.6 * k ** 4, 1.6 * k ** 5, \ 1.6 * k ** 3, 1.6 * k ** 4, 1.6 * k ** 5, 1.6 * k ** 6, 1.6 * k ** 7, \ 1.6 * k ** 5, 1.6 * k ** 6, 1.6 * k ** 7, 1.6 * k ** 8, 1.6 * k ** 9, \ 1.6 * k ** 7, 1.6 * k ** 8, 1.6 * k ** 9, 1.6 * k ** 10, 1.6 * k ** 11]) I = cv2.imread(I_name).astype(float) I_show = cv2.imread(I_name).astype(float) I0 = cv2.imread(I_name, 0).astype(float) I1 = misc.imresize(I0, 50, 'bilinear').astype(float) I2 = misc.imresize(I0, 25, 'bilinear').astype(float) I3 = misc.imresize(I0, 1 / 8.0, 'bilinear').astype(float) I4 = misc.imresize(I0, 1 / 16.0, 'bilinear').astype(float) o0sc = np.zeros((5, I0.shape[0], I0.shape[1])) o1sc = np.zeros((5, I1.shape[0], I1.shape[1])) o2sc = np.zeros((5, I2.shape[0], I2.shape[1])) o3sc = np.zeros((5, I3.shape[0], I3.shape[1])) o4sc = np.zeros((5, I4.shape[0], I4.shape[1])) for i in range(0, 5): """ o1sc[i,:,:] = cv2.filter2D(I0, -1, h.gauss(9, sigma1[i])) o2sc[i,:,:] = misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 5])), 50, 'bilinear').astype(float) o3sc[i,:,:] = misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 10])), 25, 'bilinear').astype(float) o4sc[i,:,:] = misc.imresize(misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 15])), 25, 'bilinear').astype(float), 50, 'bilinear').astype(float) """ o0sc[i, :, :] = ndimage.filters.gaussian_filter(I0, sigma1[i]) o1sc[i, :, :] = misc.imresize( ndimage.filters.gaussian_filter(I0, sigma1[i]), 50, 'bilinear') o2sc[i, :, :] = misc.imresize( ndimage.filters.gaussian_filter(I0, sigma1[i]), 25, 'bilinear') o3sc[i, :, :] = misc.imresize( ndimage.filters.gaussian_filter(I0, sigma1[i]), 1 / 8.0, 'bilinear') o4sc[i, :, :] = misc.imresize( ndimage.filters.gaussian_filter(I0, sigma1[i]), 1 / 16.0, 'bilinear') I = [o0sc[0], o0sc[1], o0sc[2], o0sc[3], o0sc[4], o1sc[0], o1sc[1], o1sc[2], o1sc[3], o1sc[4], \ o2sc[0], o2sc[1], o2sc[2], o2sc[3], o2sc[4], o3sc[0], o3sc[1], o3sc[2], o3sc[3], o3sc[4], \ o4sc[1], o4sc[2], o4sc[3], o4sc[4], o4sc[4]] # point with orientation; [y, x, s, o] final_points = [] ret_points = [] # length of outer list o = 0 for p in points: counter = 1 max_size_y = len(I[int(p[2])]) - (window_size) max_size_x = len(I[int(p[2])][0]) - (window_size) min_size_y = window_size min_size_x = window_size if ((p[0] < max_size_y) and (min_size_y < p[0]) and \ (p[1] < max_size_x and min_size_x < p[1])): if (p[2] > 5 and p[2] < 8): cv2.circle(I_show, (int(p[1] * 2), int(p[0] * 2)), 12, (0, 255, 0), 2) if (p[2] > 10 and p[2] < 13): cv2.circle(I_show, (int(p[1] * 4), int(p[0] * 4)), 24, (255, 0, 0), 2) if (p[2] > 15 and p[2] < 18): cv2.circle(I_show, (int(p[1] * 8), int(p[0] * 8)), 30, (125, 125, 0), 2) final_points.append(p) o += 1 cv2.imwrite("points.jpg", I_show) # size of all usable points o print("length of usable points, orientation", o) i = 0 for p in final_points: bins = np.zeros([36]) window_size = 16 gauss_window = h.gauss(window_size, 1.5 * p[2]) ip_window = h.create_window(I[int(p[2])], p, window_size + 2) orien_of_bin = [] # creates bin for each point hold_mag = np.empty((window_size, window_size)) hold_ori = np.empty((window_size, window_size)) for y in range(0, window_size): for x in range(0, window_size): magnitude_p = magnitude(ip_window, [y + 1, x + 1]) orientation_p = orientation(ip_window, [y + 1, x + 1]) orientation_p = math.floor(orientation_p / 10.0) hold_mag[y, x] = magnitude_p hold_ori[y, x] = orientation_p hold_mag = np.multiply(hold_mag, gauss_window) hold_mag = np.reshape(hold_mag, -1) hold_ori = np.reshape(hold_ori, -1) for j in range(0, len(hold_ori)): bins[hold_ori[j]] += hold_mag[j] # index of max element in bin max_index = bins.argmax() max_val = bins[max_index] for j in range(0, 35): if (bins[j] >= max_val * 0.8): orien_of_bin.append(j) new_orien = list(orien_of_bin) cc = 0 for i in new_orien: if (i == 1): A = 0 B = bins[i] C = bins[i + 1] if (i == 35): A = bins[i - 1] B = bins[i] C = 0 else: A = bins[i - 1] B = bins[i] C = bins[i + 1] a = A + (C - A) / 2.0 - B b = (C - A) / 2.0 c = B if (b == 0 or a == 0): toppoint = 0 else: toppoint = -b / (2 * a) degree = (toppoint * 10) + i * 10 #print(degree) if (degree < 0): degree = 360.0 + degree ret_points.append([p[0], p[1], p[2], degree]) cc += 1 if (cc == 1): break counter += 1 ret_points = np.array(ret_points) return (ret_points)
def sift_descriptor(I_name, points_orientations): """ Input : Image name I, points (y,x,sigma) output: points p, and features f """ k_con = math.sqrt(2) k = k_con """ sigma1 = np.array([k_con/2, 1.0, k_con , k_con ** 2, k_con ** 3, \ k_con, k_con ** 2, k_con ** 3, k_con ** 4, k_con ** 5, \ k_con ** 3, k_con ** 4, k_con ** 5, k_con ** 6, k_con ** 7, \ k_con ** 5, k_con ** 6, k_con ** 7, k_con ** 8, k_con ** 9, \ k_con ** 10, k_con ** 11, k_con ** 12, k_con ** 13, k_con ** 14]) """ sigma1 = np.array([1.3, 1.6, 1.6 * k, 1.6 * k ** 2, 1.6 * k ** 3, \ 1.6 * k, 1.6 * k ** 2, 1.6 * k ** 3, 1.6 * k ** 4, 1.6 * k ** 5, \ 1.6 * k ** 3, 1.6 * k ** 4, 1.6 * k ** 5, 1.6 * k ** 6, 1.6 * k ** 7, \ 1.6 * k ** 5, 1.6 * k ** 6, 1.6 * k ** 7, 1.6 * k ** 8, 1.6 * k ** 9, \ 1.6 * k ** 7, 1.6 * k ** 8, 1.6 * k ** 9, 1.6 * k ** 10, 1.6 * k ** 11]) I = cv2.imread(I_name) I0 = cv2.imread(I_name, 0) I1 = misc.imresize(I0, 50, 'bilinear').astype(float) I2 = misc.imresize(I0, 25, 'bilinear').astype(float) I3 = misc.imresize(I0, 1/8.0, 'bilinear').astype(float) I4 = misc.imresize(I0, 1/16.0, 'bilinear').astype(float) o0sc = np.zeros((5, I0.shape[0],I0.shape[1])) o1sc = np.zeros((5, I1.shape[0],I1.shape[1])) o2sc = np.zeros((5, I2.shape[0],I2.shape[1])) o3sc = np.zeros((5, I3.shape[0],I3.shape[1])) o4sc = np.zeros((5, I4.shape[0],I4.shape[1])) for i in range(0,5): """ o1sc[i,:,:] = cv2.filter2D(I0, -1, h.gauss(9, sigma1[i])) o2sc[i,:,:] = misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 5])), 50, 'bilinear').astype(float) o3sc[i,:,:] = misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 10])), 25, 'bilinear').astype(float) o4sc[i,:,:] = misc.imresize(misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 15])), 25, 'bilinear').astype(float), 50, 'bilinear').astype(float) """ o0sc[i,:,:] = ndimage.filters.gaussian_filter(I0, sigma1[i]) o1sc[i,:,:] = misc.imresize(ndimage.filters.gaussian_filter(I0, sigma1[i]), 50, 'bilinear') o2sc[i,:,:] = misc.imresize(ndimage.filters.gaussian_filter(I0, sigma1[i]), 25, 'bilinear') o3sc[i,:,:] = misc.imresize(ndimage.filters.gaussian_filter(I0, sigma1[i]), 1/8.0, 'bilinear') o4sc[i,:,:] = misc.imresize(ndimage.filters.gaussian_filter(I0, sigma1[i]), 1/16.0, 'bilinear') I = [o0sc[0], o0sc[1], o0sc[2], o0sc[3], o0sc[4], o1sc[0], o1sc[1], o1sc[2], o1sc[3], o1sc[4], \ o2sc[0], o2sc[1], o2sc[2], o2sc[3], o2sc[4], o3sc[0], o3sc[1], o3sc[2], o3sc[3], o3sc[4], \ o4sc[0], o4sc[1], o4sc[2], o4sc[3], o4sc[4]] window_size = 16 gauss_window = h.gauss(window_size, 8) final_points = [] orien_of_bin = [] len_of_desc = len(points_orientations) mag_bin = np.zeros([len_of_desc, 16, 4, 4]) ori_bin = np.zeros([len_of_desc, 16, 4, 4]) final_desc = [] incrementeur = 0 for p in points_orientations: hold_mag = np.empty([window_size,window_size]) hold_ori = np.empty([window_size,window_size]) ip_window = h.create_window_angles(I[int(p[2])], p, window_size + 2) # creates bin for each point for y in range(0, window_size): for x in range(0, window_size): magnitude_p = magnitude(ip_window, [y + 1, x + 1]) orientation_p = orientation(ip_window, [y + 1, x + 1]) hold_mag[y, x] = magnitude_p hold_ori[y, x] = orientation_p hold_mag = np.multiply(hold_mag, gauss_window) hold_mag = np.reshape(hold_mag, -1) hold_ori = np.reshape(hold_ori, -1) v = 0 for w in range(0, 4): for i in range(0, 13, 4): for j in range(0, 4): for k in range(0, 4): mag_bin[incrementeur][v][j][k] = hold_mag[k + i + j * 16 + w * 64] ori = hold_ori[k + i + j * 16 + w * 64] ori_bin[incrementeur][v][j][k] = ori v += 1 incrementeur += 1 """ val t-3 t-2 t-1 t0 t1 t2 t3 directions: first dir is pointing north, northeast, east... """ directions = np.array([[-2.5,1.5, -4],[-2.5,5.5, -3],[1.5,5.5, 1],[5.5,5.5, 5], \ [5.5,2.5, 4],[-2.5,5.5, 3],[-2.5,1.5, -1],[-2.5,-2.5, -5]]) mid = np.array([1.5, 1.5]) incc = 0 for i in range(0, len(mag_bin)): descriptor = np.zeros([16,8]) holder_mag = mag_bin[i] holder_ori = ori_bin[i] for region in range(0, 16): for y in range(0, 4): for x in range(0, 4): coor = np.array([y, x]) bin_nr = (holder_ori[region, y, x] / 45.0) if (bin_nr > 7): bin_nr = bin_nr - 1 t1_nr = math.ceil(bin_nr) % 8 tm1_nr = math.floor(bin_nr) % 8 if (bin_nr % 1.0 == 0): tm1_nr = (bin_nr + 1) % 8 t2_nr = (t1_nr + 1) % 8 tm2_nr = (tm1_nr - 1) % 8 t0_weight = 1 - np.linalg.norm(mid - coor) / 2.7 t0 = t0_weight * holder_mag[region, y, x] A = 0 B = t0 C = 0 a = -t0/9 b = 0 c = t0 offset = bin_nr % 1.0 t1 = a * (1+offset)**2 + b * (1+offset) + c tm1 = a * (-1 + offset)**2 + b * (-1+offset) + c t2 = a * (2+offset)**2 + b * (2+offset) + c tm2 = a * (-2+offset)**2 + b * (-2+offset) + c descriptor[region, t1_nr] += t1 descriptor[region, tm1_nr] += tm1 descriptor[region, t2_nr] += t2 descriptor[region, tm2_nr] += tm2 # first quadrant if (y >= 0 and y <= 1 and x >= 2 and x <= 3): dir1 = directions[0] dir2 = directions[1] dir3 = directions[2] (w1,w2,w3,bool1,bool2,bool3) = is_inbound([y,x],region, dir1, dir2, dir3) if (y >= 0 and y <= 1 and x >= 0 and x <= 1): dir1 = directions[6] dir2 = directions[7] dir3 = directions[0] (w1,w2,w3,bool1,bool2,bool3) = is_inbound([y,x],region, dir1, dir2, dir3) if (y >= 2 and y <= 3 and x >= 0 and x <= 1): dir1 = directions[4] dir2 = directions[5] dir3 = directions[6] (w1,w2,w3,bool1,bool2,bool3) = is_inbound([y,x],region, dir1, dir2, dir3) if (y >= 2 and y <= 3 and x >= 2 and x <= 3): dir1 = directions[2] dir2 = directions[3] dir3 = directions[4] (w1,w2,w3,bool1,bool2,bool3) = is_inbound([y,x],region, dir1, dir2, dir3) descriptor[(region + dir1[2]) * bool1, t1_nr] += w1 * t1 descriptor[(region + dir1[2]) * bool1, tm1_nr] += w1 * tm1 descriptor[(region + dir1[2]) * bool1, t2_nr] += w1 * t2 descriptor[(region + dir1[2]) * bool1, tm2_nr] += w1 * tm1 descriptor[(region + dir2[2]) * bool2, t1_nr] += w2 * t1 descriptor[(region + dir2[2]) * bool2, tm1_nr] += w2 * tm1 descriptor[(region + dir2[2]) * bool2, t2_nr] += w2 * t2 descriptor[(region + dir2[2]) * bool2, tm2_nr] += w2 * tm2 descriptor[(region + dir3[2]) * bool3, t1_nr] += w3 * t1 descriptor[(region + dir3[2]) * bool3, tm1_nr] += w3 * tm1 descriptor[(region + dir3[2]) * bool3, t2_nr] += w3 * t2 descriptor[(region + dir3[2]) * bool3, tm2_nr] += w3 * tm2 final_desc.append(np.array(descriptor.reshape(-1))) incc += 1 print("length of descriptor processed: ", incc) ret = [] for vec_i in range(0, len(final_desc)): dd = final_desc[vec_i] / np.linalg.norm(final_desc[vec_i]) dd = np.clip(dd, 0, 0.2) dd = dd / np.linalg.norm(dd) ret.append([points_orientations[vec_i], np.array(dd)]) return(ret)
def decriptor_representation(Img, points_orientations): I = cv2.imread(Img, 0) window_size = 16 max_point_size_y = len(I) - (window_size + 2) min_point_size_y = window_size max_point_size_x = len(I[0]) - (window_size + 5) min_point_size_x = window_size gauss_window = h.gauss(window_size, 8) points = points_orientations final_points = [] orien_of_bin = [] len_of_desc = len(points) mag_bin = numpy.zeros([len_of_desc, 16, 4, 4]) ori_bin = numpy.zeros([len_of_desc, 16, 4, 4]) final_desc = numpy.zeros([len_of_desc, 128]) incrementeur = 0 for p in points: hold_mag = h.create_window(I, [35,35], window_size) hold_ori = h.create_window(I, [35,35], window_size) ip_window = h.create_window(I, p, window_size + 2) # creates bin for each point for y in range(0, window_size): for x in range(0, window_size): magnitude_p = magnitude(ip_window, [y + 1, x + 1]) orientation_p = orientation(ip_window, [y + 1, x + 1]) hold_mag[y][x] = magnitude_p hold_ori[y][x] = orientation_p hold_mag = numpy.multiply(hold_mag, gauss_window) hold_mag = numpy.reshape(hold_mag, -1) hold_ori = numpy.reshape(hold_ori, -1) holder = numpy.zeros([4,4]) v = 0 for w in range(0, 4): for i in range(0, 13, 4): for j in range(0, 4): for k in range(0, 4): mag_bin[incrementeur][v][j][k] = hold_mag[k + i + j * 16 + w * 64] ori = math.floor((360 - hold_ori[k + i + j * 16 + w * 64]) / 45.0) - 1 #print(hold_ori[k + i + j * 16 + w * 64]) / 45.0)) ori_bin[incrementeur][v][j][k] = ori v += 1 #with open('file2.txt','a') as f_handle: # numpy.savetxt(f_handle, ori_bin[incrementeur], delimiter=" ", fmt="%s") #numpy.savetxt('file2.txt', d_bin[incrementeur], delimiter=" ", fmt="%s") incrementeur += 1 mag_val = 0 ori_val = 0 #print("\n\n\n") for w in range(0, len(points)): for i in range(0, 16): for j in range(0, 4): for k in range(0, 4): mag_val = mag_bin[w][i][k][j] ori_val = ori_bin[w][i][k][j] #print(ori_val, ori_val + 8*i) #print("\n\n") final_desc[w][8*i + ori_val] += mag_val for vec_i in range(0, len(final_desc)): final_desc[vec_i] = final_desc[vec_i] / numpy.linalg.norm(final_desc[vec_i]) final_desc[vec_i] = numpy.clip(final_desc[vec_i], 0, 0.2) final_desc[vec_i] = final_desc[vec_i] / numpy.linalg.norm(final_desc[vec_i]) oo = open("descriptor.txt", "w") for ff in final_desc: for j in range(0,128): oo.write(str(ff[j]) + "\n") oo.write("\n\n") oo.close() return(final_desc)
def surf_orientation(I_name, point): """ input: (y, x, s), integral_img for scale s output : (y, x, s, o) """ counter = 1 final_keypoints = [] orientations = [] I_int = cv2.imread(I_name, 0).astype(float) scale_pics = [cv2.filter2D(I_int, -1, hh.gauss(9, 1.2)), cv2.filter2D(I_int, -1, hh.gauss(15, 1.2)), \ cv2.filter2D(I_int, -1, hh.gauss(21, 1.2)), cv2.filter2D(I_int, -1, hh.gauss(27, 1.2)), \ cv2.filter2D(I_int, -1, hh.gauss(39, 1.2)), cv2.filter2D(I_int, -1, hh.gauss(75, 1.2))] scale_pics = [cv2.integral(scale_pics[0]), cv2.integral(scale_pics[1]), \ cv2.integral(scale_pics[2]), cv2.integral(scale_pics[3]), \ cv2.integral(scale_pics[4]), cv2.integral(scale_pics[5])] for p_a in point: hs = h2.haar_size(p_a[2]) max_size_y = len(I_int) - (20 * p_a[2] + hs) max_size_x = len(I_int[0]) - (20 * p_a[2] + hs) min_size_x = min_size_y = h2.round_four(20 * p_a[2] ) + hs if ((p_a[0] < max_size_y) and (min_size_y < p_a[0]) and \ (p_a[1] < max_size_x) and (min_size_x < p_a[1])): #cv2.circle(I, (p[1].astype(int), p[0].astype(int)), int(round(p[2])), (0,0,255), 3) final_keypoints.append([p_a[0], p_a[1], p_a[2]]) #keypoint area = [y, x, s] for p_a in final_keypoints: window_size = (int(6 * p_a[2]) / p_a[2]) + 1 d_x = np.zeros([window_size, window_size]) d_y = np.zeros([window_size, window_size]) if (p_a[2] > 1.9 and p_a[2] < 2.1): I_int = scale_pics[0] if (p_a[2] > 2.7 and p_a[2] < 2.9): I_int = scale_pics[1] if (p_a[2] > 3.5 and p_a[2] < 3.6): I_int = scale_pics[2] if (p_a[2] > 5.1 and p_a[2] < 5.3): I_int = scale_pics[3] if (p_a[2] > 6.7 and p_a[2] < 6.9): I_int = scale_pics[4] if (p_a[2] > 9.9 and p_a[2] < 10.1): I_int = scale_pics[5] """ a g b ###### ###### e ##p### f ###### ###### ###### c gh d the integral square aecg subtracted from fbhd gives the dx-haar-wavelet response # Calculating the haar wavelet response, in a 20x20 window """ haar_hs = int(window_size / 2) incre = int(p_a[2]) #y1 = - haar_hs * incre + int(p_a[0]) y1 = 0 for y in range(int(p_a[0]) - (haar_hs * incre), int(p_a[0]) + (haar_hs * incre), incre): #x1 = - haar_hs * incre + int(p_a[1]) x1 = 0 for x in range(int(p_a[1]) - (haar_hs * incre), int(p_a[1]) + (haar_hs * incre), incre): if (y1 < 0 or x1 < 0): print(y1,x1) #print("y = " + str(y), "x = " + str(x), "scale = " + str(p_a[2]), "hs = " + str(hs)) a = I_int[y - haar_hs, x - haar_hs] b = I_int[y - haar_hs, x + haar_hs] c = I_int[y + haar_hs, x - haar_hs] d = I_int[y + haar_hs, x + haar_hs] e = I_int[y, x - haar_hs] f = I_int[y, x + haar_hs] g = I_int[y - haar_hs, x] h = I_int[y + haar_hs, x] d_x[y1, x1] = (h + a - c - g) - (d + g - h - b) d_y[y1, x1] = (f + a - e - b) - (d + e - c - f) x1 += 1 y1 += 1 #y1 = - haar_hs * incre + int(p_a[0]) #for y in range(0, window_size, int(incre)): #x1 = - haar_hs * incre + int(p_a[1]) #for x in range(0, window_size, int(incre)): #x1 += incre #y1 += incre d_x = np.multiply(hh.gauss(len(d_x), 2.5 * p_a[2]), d_x) d_y = np.multiply(hh.gauss(len(d_y), 2.5 * p_a[2]), d_y) d_x = hh.circle_matrix(d_x) d_y = hh.circle_matrix(d_y) np.set_printoptions(precision=0) #print("\n") #print(d_x) #print("\n") #print(d_y) #print("\n") dirs = np.zeros([6, 2]) for y in range (0, len(d_x)): for x in range(0, len(d_x)): dirs[math.floor(hh.orientation([d_x[y,x], d_y[y,x]]) / 60.0), 0] += d_x[y,x] dirs[math.floor(hh.orientation([d_x[y,x], d_y[y,x]]) / 60.0), 1] += d_y[y,x] #print(dirs) #print("\n") #print("\n") #print("\n") #print("\n") orientations.append(hh.orientation(dirs[np.argmax(np.sum(np.abs(dirs), axis=1))])) ret_points = [] for i in range(0, len(final_keypoints)): ret_points.append([final_keypoints[i][0], final_keypoints[i][1], final_keypoints[i][2], orientations[i]]) return(ret_points)
def sift_orientation(I, points, window_size): window_size = 16 """ Input : image I, interest points, size of window Output: assigns an orientation to the interest points """ # point with orientation; [[y, x],[o1, o2, o3]] pwo = [] max_point_size_y = len(I[0]) - (window_size + 2) min_point_size_y = window_size max_point_size_x = len(I[0][0]) - (window_size + 5) min_point_size_x = window_size hold_mag = h.create_window(I[0], [35,35], window_size) hold_ori = h.create_window(I[0], [35,35], window_size) final_points = [] orien_of_bin = [] # length of outer list o = 0 for p in points: if ((p[0] < max_point_size_y and min_point_size_y < p[0]) and \ (p[1] < max_point_size_x and min_point_size_x < p[1])): final_points.append(p) o += 1 # size of all usable points o bins = numpy.zeros([o, 1, 36]) i = 0 for p in final_points: # if a point is too close to the border of the image, it is discarded if ((p[0] < max_point_size_y and p[0] > min_point_size_y) and \ (p[1] < max_point_size_x and p[1] > min_point_size_x)): # the sigma value for the gauss window, is 1.5 * scale # NOTE! Only works for first octave gauss_window = h.gauss(window_size, 1.5 + (1 * p[2])) print(p) ip_window = h.create_window(I[p[2]], p, window_size + 2) # creates bin for each point for y in range(0, window_size): for x in range(0, window_size): magnitude_p = magnitude(ip_window, [y + 1, x + 1]) orientation_p = orientation(ip_window, [y + 1, x + 1]) orientation_p = math.floor(orientation_p/10.0) hold_mag[y][x] = magnitude_p hold_ori[y][x] = orientation_p hold_mag = numpy.multiply(hold_mag, gauss_window) hold_mag = numpy.reshape(hold_mag, -1) hold_ori = numpy.reshape(hold_ori, -1) for j in range(0, len(hold_ori)): bins[i][0][hold_ori[j]] += hold_mag[j] hold_mag = h.create_window(I[0], [35,35], window_size) hold_ori = h.create_window(I[0], [35,35], window_size) bin_i = bins[i][0] # index of max element in bin max_index = max(bin_i) max_index = [k for k, j in enumerate(bin_i) if j == max_index] # finds points within 80% of interest point holder_bin = [] holder_bin.append(max_index[0]) max_val = bin_i[max_index] for j in range(0, 35): if (bin_i[j] >= max_val * 0.8 and j != max_index[0]): holder_bin.append(j) orien_of_bin.append(holder_bin) i += 1 new_orien = list(orien_of_bin) for i in range(0, len(orien_of_bin)): holder = [] for j in orien_of_bin[i]: if (j == 1): A = 0 B = bins[i][0][j] C = bins[i][0][j] if (j == 35): A = bins[i][0][j-1] B = bins[i][0][j] C = 0 else: A = bins[i][0][j - 1] B = bins[i][0][j] C = bins[i][0][j + 1] a = A + (C-A)/2.0 -B b = (C-A)/2.0 c = B toppoint = -b / (2 * a) point = toppoint * 10 + j * 10 if (point < 0): point = 360.0 + point holder.append(point) new_orien[i] = holder o = open('orients.txt', 'w') for i in range(0, len(orien_of_bin)): o.write(str(orien_of_bin[i]) + "\t" + str(new_orien[i]) + "\n") o.close() #print([final_points,new_orien]) final_points2 = [] for i in range(0, len(final_points)): for o in new_orien[i]: final_points2.append([final_points[i][0], final_points[i][1], o]) oo = open('interest_p_ori_mag.txt', 'w') for pp in final_points2: oo.write(str(pp[0]) + " " + str(pp[1]) + " " + str(pp[2]) + "\n") oo.close() #print(final_points2) return (final_points2)
def sift_descriptor(I_name, points_orientations): """ Input : Image name I, points (y,x,sigma) output: points p, and features f """ k_con = math.sqrt(2) k = k_con """ sigma1 = np.array([k_con/2, 1.0, k_con , k_con ** 2, k_con ** 3, \ k_con, k_con ** 2, k_con ** 3, k_con ** 4, k_con ** 5, \ k_con ** 3, k_con ** 4, k_con ** 5, k_con ** 6, k_con ** 7, \ k_con ** 5, k_con ** 6, k_con ** 7, k_con ** 8, k_con ** 9, \ k_con ** 10, k_con ** 11, k_con ** 12, k_con ** 13, k_con ** 14]) """ sigma1 = np.array([1.3, 1.6, 1.6 * k, 1.6 * k ** 2, 1.6 * k ** 3, \ 1.6 * k, 1.6 * k ** 2, 1.6 * k ** 3, 1.6 * k ** 4, 1.6 * k ** 5, \ 1.6 * k ** 3, 1.6 * k ** 4, 1.6 * k ** 5, 1.6 * k ** 6, 1.6 * k ** 7, \ 1.6 * k ** 5, 1.6 * k ** 6, 1.6 * k ** 7, 1.6 * k ** 8, 1.6 * k ** 9, \ 1.6 * k ** 7, 1.6 * k ** 8, 1.6 * k ** 9, 1.6 * k ** 10, 1.6 * k ** 11]) I = cv2.imread(I_name) I0 = cv2.imread(I_name, 0) I1 = misc.imresize(I0, 50, 'bilinear').astype(float) I2 = misc.imresize(I0, 25, 'bilinear').astype(float) I3 = misc.imresize(I0, 1 / 8.0, 'bilinear').astype(float) I4 = misc.imresize(I0, 1 / 16.0, 'bilinear').astype(float) o0sc = np.zeros((5, I0.shape[0], I0.shape[1])) o1sc = np.zeros((5, I1.shape[0], I1.shape[1])) o2sc = np.zeros((5, I2.shape[0], I2.shape[1])) o3sc = np.zeros((5, I3.shape[0], I3.shape[1])) o4sc = np.zeros((5, I4.shape[0], I4.shape[1])) for i in range(0, 5): """ o1sc[i,:,:] = cv2.filter2D(I0, -1, h.gauss(9, sigma1[i])) o2sc[i,:,:] = misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 5])), 50, 'bilinear').astype(float) o3sc[i,:,:] = misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 10])), 25, 'bilinear').astype(float) o4sc[i,:,:] = misc.imresize(misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 15])), 25, 'bilinear').astype(float), 50, 'bilinear').astype(float) """ o0sc[i, :, :] = ndimage.filters.gaussian_filter(I0, sigma1[i]) o1sc[i, :, :] = misc.imresize( ndimage.filters.gaussian_filter(I0, sigma1[i]), 50, 'bilinear') o2sc[i, :, :] = misc.imresize( ndimage.filters.gaussian_filter(I0, sigma1[i]), 25, 'bilinear') o3sc[i, :, :] = misc.imresize( ndimage.filters.gaussian_filter(I0, sigma1[i]), 1 / 8.0, 'bilinear') o4sc[i, :, :] = misc.imresize( ndimage.filters.gaussian_filter(I0, sigma1[i]), 1 / 16.0, 'bilinear') I = [o0sc[0], o0sc[1], o0sc[2], o0sc[3], o0sc[4], o1sc[0], o1sc[1], o1sc[2], o1sc[3], o1sc[4], \ o2sc[0], o2sc[1], o2sc[2], o2sc[3], o2sc[4], o3sc[0], o3sc[1], o3sc[2], o3sc[3], o3sc[4], \ o4sc[0], o4sc[1], o4sc[2], o4sc[3], o4sc[4]] window_size = 16 gauss_window = h.gauss(window_size, 8) final_points = [] orien_of_bin = [] len_of_desc = len(points_orientations) mag_bin = np.zeros([len_of_desc, 16, 4, 4]) ori_bin = np.zeros([len_of_desc, 16, 4, 4]) final_desc = [] incrementeur = 0 for p in points_orientations: hold_mag = np.empty([window_size, window_size]) hold_ori = np.empty([window_size, window_size]) ip_window = h.create_window_angles(I[int(p[2])], p, window_size + 2) # creates bin for each point for y in range(0, window_size): for x in range(0, window_size): magnitude_p = magnitude(ip_window, [y + 1, x + 1]) orientation_p = orientation(ip_window, [y + 1, x + 1]) hold_mag[y, x] = magnitude_p hold_ori[y, x] = orientation_p hold_mag = np.multiply(hold_mag, gauss_window) hold_mag = np.reshape(hold_mag, -1) hold_ori = np.reshape(hold_ori, -1) v = 0 for w in range(0, 4): for i in range(0, 13, 4): for j in range(0, 4): for k in range(0, 4): mag_bin[incrementeur][v][j][k] = hold_mag[k + i + j * 16 + w * 64] ori = hold_ori[k + i + j * 16 + w * 64] ori_bin[incrementeur][v][j][k] = ori v += 1 incrementeur += 1 """ val t-3 t-2 t-1 t0 t1 t2 t3 directions: first dir is pointing north, northeast, east... """ directions = np.array([[-2.5,1.5, -4],[-2.5,5.5, -3],[1.5,5.5, 1],[5.5,5.5, 5], \ [5.5,2.5, 4],[-2.5,5.5, 3],[-2.5,1.5, -1],[-2.5,-2.5, -5]]) mid = np.array([1.5, 1.5]) incc = 0 for i in range(0, len(mag_bin)): descriptor = np.zeros([16, 8]) holder_mag = mag_bin[i] holder_ori = ori_bin[i] for region in range(0, 16): for y in range(0, 4): for x in range(0, 4): coor = np.array([y, x]) bin_nr = (holder_ori[region, y, x] / 45.0) if (bin_nr > 7): bin_nr = bin_nr - 1 t1_nr = math.ceil(bin_nr) % 8 tm1_nr = math.floor(bin_nr) % 8 if (bin_nr % 1.0 == 0): tm1_nr = (bin_nr + 1) % 8 t2_nr = (t1_nr + 1) % 8 tm2_nr = (tm1_nr - 1) % 8 t0_weight = 1 - np.linalg.norm(mid - coor) / 2.7 t0 = t0_weight * holder_mag[region, y, x] A = 0 B = t0 C = 0 a = -t0 / 9 b = 0 c = t0 offset = bin_nr % 1.0 t1 = a * (1 + offset)**2 + b * (1 + offset) + c tm1 = a * (-1 + offset)**2 + b * (-1 + offset) + c t2 = a * (2 + offset)**2 + b * (2 + offset) + c tm2 = a * (-2 + offset)**2 + b * (-2 + offset) + c descriptor[region, t1_nr] += t1 descriptor[region, tm1_nr] += tm1 descriptor[region, t2_nr] += t2 descriptor[region, tm2_nr] += tm2 # first quadrant if (y >= 0 and y <= 1 and x >= 2 and x <= 3): dir1 = directions[0] dir2 = directions[1] dir3 = directions[2] (w1, w2, w3, bool1, bool2, bool3) = is_inbound([y, x], region, dir1, dir2, dir3) if (y >= 0 and y <= 1 and x >= 0 and x <= 1): dir1 = directions[6] dir2 = directions[7] dir3 = directions[0] (w1, w2, w3, bool1, bool2, bool3) = is_inbound([y, x], region, dir1, dir2, dir3) if (y >= 2 and y <= 3 and x >= 0 and x <= 1): dir1 = directions[4] dir2 = directions[5] dir3 = directions[6] (w1, w2, w3, bool1, bool2, bool3) = is_inbound([y, x], region, dir1, dir2, dir3) if (y >= 2 and y <= 3 and x >= 2 and x <= 3): dir1 = directions[2] dir2 = directions[3] dir3 = directions[4] (w1, w2, w3, bool1, bool2, bool3) = is_inbound([y, x], region, dir1, dir2, dir3) descriptor[(region + dir1[2]) * bool1, t1_nr] += w1 * t1 descriptor[(region + dir1[2]) * bool1, tm1_nr] += w1 * tm1 descriptor[(region + dir1[2]) * bool1, t2_nr] += w1 * t2 descriptor[(region + dir1[2]) * bool1, tm2_nr] += w1 * tm1 descriptor[(region + dir2[2]) * bool2, t1_nr] += w2 * t1 descriptor[(region + dir2[2]) * bool2, tm1_nr] += w2 * tm1 descriptor[(region + dir2[2]) * bool2, t2_nr] += w2 * t2 descriptor[(region + dir2[2]) * bool2, tm2_nr] += w2 * tm2 descriptor[(region + dir3[2]) * bool3, t1_nr] += w3 * t1 descriptor[(region + dir3[2]) * bool3, tm1_nr] += w3 * tm1 descriptor[(region + dir3[2]) * bool3, t2_nr] += w3 * t2 descriptor[(region + dir3[2]) * bool3, tm2_nr] += w3 * tm2 final_desc.append(np.array(descriptor.reshape(-1))) incc += 1 print("length of descriptor processed: ", incc) ret = [] for vec_i in range(0, len(final_desc)): dd = final_desc[vec_i] / np.linalg.norm(final_desc[vec_i]) dd = np.clip(dd, 0, 0.2) dd = dd / np.linalg.norm(dd) ret.append([points_orientations[vec_i], np.array(dd)]) return (ret)
def sift_orientation(I_name, points): window_size = 16 """ Input : image I, interest points, size of window Output: assigns an orientation to the interest points """ k_con = math.sqrt(2) k = math.sqrt(2) """ sigma1 = np.array([k_con/2, 1.0, k_con , k_con ** 2, k_con ** 3, \ k_con, k_con ** 2, k_con ** 3, k_con ** 4, k_con ** 5, \ k_con ** 3, k_con ** 4, k_con ** 5, k_con ** 6, k_con ** 7, \ k_con ** 5, k_con ** 6, k_con ** 7, k_con ** 8, k_con ** 9]) """ sigma1 = np.array([1.3, 1.6, 1.6 * k, 1.6 * k ** 2, 1.6 * k ** 3, \ 1.6 * k, 1.6 * k ** 2, 1.6 * k ** 3, 1.6 * k ** 4, 1.6 * k ** 5, \ 1.6 * k ** 3, 1.6 * k ** 4, 1.6 * k ** 5, 1.6 * k ** 6, 1.6 * k ** 7, \ 1.6 * k ** 5, 1.6 * k ** 6, 1.6 * k ** 7, 1.6 * k ** 8, 1.6 * k ** 9, \ 1.6 * k ** 7, 1.6 * k ** 8, 1.6 * k ** 9, 1.6 * k ** 10, 1.6 * k ** 11]) I = cv2.imread(I_name).astype(float) I_show = cv2.imread(I_name).astype(float) I0 = cv2.imread(I_name, 0).astype(float) I1 = misc.imresize(I0, 50, 'bilinear').astype(float) I2 = misc.imresize(I0, 25, 'bilinear').astype(float) I3 = misc.imresize(I0, 1/8.0, 'bilinear').astype(float) I4 = misc.imresize(I0, 1/16.0, 'bilinear').astype(float) o0sc = np.zeros((5, I0.shape[0],I0.shape[1])) o1sc = np.zeros((5, I1.shape[0],I1.shape[1])) o2sc = np.zeros((5, I2.shape[0],I2.shape[1])) o3sc = np.zeros((5, I3.shape[0],I3.shape[1])) o4sc = np.zeros((5, I4.shape[0],I4.shape[1])) for i in range(0,5): """ o1sc[i,:,:] = cv2.filter2D(I0, -1, h.gauss(9, sigma1[i])) o2sc[i,:,:] = misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 5])), 50, 'bilinear').astype(float) o3sc[i,:,:] = misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 10])), 25, 'bilinear').astype(float) o4sc[i,:,:] = misc.imresize(misc.imresize(cv2.filter2D(I0, -1, h.gauss(9,sigma1[i + 15])), 25, 'bilinear').astype(float), 50, 'bilinear').astype(float) """ o0sc[i,:,:] = ndimage.filters.gaussian_filter(I0, sigma1[i]) o1sc[i,:,:] = misc.imresize(ndimage.filters.gaussian_filter(I0, sigma1[i]), 50, 'bilinear') o2sc[i,:,:] = misc.imresize(ndimage.filters.gaussian_filter(I0, sigma1[i]), 25, 'bilinear') o3sc[i,:,:] = misc.imresize(ndimage.filters.gaussian_filter(I0, sigma1[i]), 1/8.0, 'bilinear') o4sc[i,:,:] = misc.imresize(ndimage.filters.gaussian_filter(I0, sigma1[i]), 1/16.0, 'bilinear') I = [o0sc[0], o0sc[1], o0sc[2], o0sc[3], o0sc[4], o1sc[0], o1sc[1], o1sc[2], o1sc[3], o1sc[4], \ o2sc[0], o2sc[1], o2sc[2], o2sc[3], o2sc[4], o3sc[0], o3sc[1], o3sc[2], o3sc[3], o3sc[4], \ o4sc[1], o4sc[2], o4sc[3], o4sc[4], o4sc[4]] # point with orientation; [y, x, s, o] final_points = [] ret_points = [] # length of outer list o = 0 for p in points: counter = 1 max_size_y = len(I[int(p[2])]) - (window_size) max_size_x = len(I[int(p[2])][0]) - (window_size) min_size_y = window_size min_size_x = window_size if ((p[0] < max_size_y) and (min_size_y < p[0]) and \ (p[1] < max_size_x and min_size_x < p[1])): if (p[2] > 5 and p[2] < 8): cv2.circle(I_show, (int(p[1] * 2), int(p[0] * 2)), 12, (0,255,0),2) if (p[2] > 10 and p[2] < 13): cv2.circle(I_show, (int(p[1] * 4), int(p[0] * 4)), 24, (255,0,0),2) if (p[2] > 15 and p[2] < 18): cv2.circle(I_show, (int(p[1] * 8), int(p[0] * 8)), 30, (125,125,0),2) final_points.append(p) o += 1 cv2.imwrite("points.jpg", I_show) # size of all usable points o print("length of usable points, orientation", o) i = 0 for p in final_points: bins = np.zeros([36]) window_size = 16 gauss_window = h.gauss(window_size, 1.5 * p[2]) ip_window = h.create_window(I[int(p[2])], p, window_size + 2) orien_of_bin = [] # creates bin for each point hold_mag = np.empty((window_size, window_size)) hold_ori = np.empty((window_size, window_size)) for y in range(0, window_size): for x in range(0, window_size): magnitude_p = magnitude(ip_window, [y + 1, x + 1]) orientation_p = orientation(ip_window, [y + 1, x + 1]) orientation_p = math.floor(orientation_p/10.0) hold_mag[y, x] = magnitude_p hold_ori[y, x] = orientation_p hold_mag = np.multiply(hold_mag, gauss_window) hold_mag = np.reshape(hold_mag, -1) hold_ori = np.reshape(hold_ori, -1) for j in range(0, len(hold_ori)): bins[hold_ori[j]] += hold_mag[j] # index of max element in bin max_index = bins.argmax() max_val = bins[max_index] for j in range(0, 35): if (bins[j] >= max_val * 0.8): orien_of_bin.append(j) new_orien = list(orien_of_bin) cc = 0 for i in new_orien: if (i == 1): A = 0 B = bins[i] C = bins[i + 1] if (i == 35): A = bins[i-1] B = bins[i] C = 0 else: A = bins[i - 1] B = bins[i] C = bins[i + 1] a = A + (C-A)/2.0 - B b = (C-A)/2.0 c = B if (b == 0 or a == 0): toppoint = 0 else: toppoint = -b / (2 * a) degree = (toppoint * 10) + i * 10 #print(degree) if (degree < 0): degree = 360.0 + degree ret_points.append([p[0], p[1], p[2], degree]) cc += 1 if (cc == 1): break counter += 1 ret_points = np.array(ret_points) return (ret_points)
def sift_orientation(I, points, window_size): """ Input : image I, interest points, size of window Output: assigns an orientation to the interest points """ # point with orientation; [[y, x],[o1, o2, o3]] pwo = [] max_point_size_y = len(I[0]) - (window_size + 2) min_point_size_y = window_size max_point_size_x = len(I[0][0]) - (window_size + 5) min_point_size_x = window_size gauss_window = h.gauss(window_size, 1.5) hold_mag = h.create_window(I[0], [35,35], window_size) hold_ori = h.create_window(I[0], [35,35], window_size) final_points = [] orien_of_bin = [] # length of outer list o = 0 for p in points: if ((p[0] < max_point_size_y and min_point_size_y < p[0]) and \ (p[1] < max_point_size_x and min_point_size_x < p[1])): final_points.append(p) o += 1 # size of all usable points o bins = numpy.zeros([o, 1, 36]) #new 1 bins1 = numpy.zeros([o,1,36]) bins2 = numpy.zeros([o,1,36]) bins3 = numpy.zeros([o,1,36]) bins4 = numpy.zeros([o,1,36]) bins5 = numpy.zeros([o,1,36]) bins6 = numpy.zeros([o,1,36]) bins7 = numpy.zeros([o,1,36]) bins8 = numpy.zeros([o,1,36]) bins9 = numpy.zeros([o,1,36]) bins10 = numpy.zeros([o,1,36]) bins11 = numpy.zeros([o,1,36]) bins12 = numpy.zeros([o,1,36]) bins13 = numpy.zeros([o,1,36]) bins14 = numpy.zeros([o,1,36]) bins15 = numpy.zeros([o,1,36]) bins16 = numpy.zeros([o,1,36]) #new 2 i = 0 for p in final_points: # if a point is too close to the border of the image, it is discarded if ((p[0] < max_point_size_y and p[0] > min_point_size_y) and \ (p[1] < max_point_size_x and p[1] > min_point_size_x)): ip_window = h.create_window(I[p[2]], p, window_size + 2) # creates bin for each point for y in range(0, window_size): for x in range(0, window_size): magnitude_p = magnitude(ip_window, [y + 1, x + 1]) orientation_p = orientation(ip_window, [y + 1, x + 1]) orientation_p = math.floor(orientation_p/10.0) hold_mag[y][x] = magnitude_p hold_ori[y][x] = orientation_p hold_mag = numpy.multiply(hold_mag, gauss_window) # new W1 = h.create_window(hold_mag, [2,2], 4) W2 = h.create_window(hold_mag, [2,6], 4) W3 = h.create_window(hold_mag, [2,10], 4) W4 = h.create_window(hold_mag, [2,14], 4) W5 = h.create_window(hold_mag, [6,2], 4) W6 = h.create_window(hold_mag, [6,6], 4) W7 = h.create_window(hold_mag, [6,10], 4) W8 = h.create_window(hold_mag, [6,14], 4) W9 = h.create_window(hold_mag, [10,2], 4) W10 = h.create_window(hold_mag, [10,6], 4) W11 = h.create_window(hold_mag, [10,10], 4) W12 = h.create_window(hold_mag, [10,14], 4) W13 = h.create_window(hold_mag, [14,2], 4) W14 = h.create_window(hold_mag, [14,6], 4) W15 = h.create_window(hold_mag, [14,10], 4) W16 = h.create_window(hold_mag, [14,14], 4) O1 = h.create_window(hold_ori, [2,2], 4) O2 = h.create_window(hold_ori, [2,6], 4) O3 = h.create_window(hold_ori, [2,10], 4) O4 = h.create_window(hold_ori, [2,14], 4) O5 = h.create_window(hold_ori, [6,2], 4) O6 = h.create_window(hold_ori, [6,6], 4) O7 = h.create_window(hold_ori, [6,10], 4) O8 = h.create_window(hold_ori, [6,14], 4) O9 = h.create_window(hold_ori, [10,2], 4) O10 = h.create_window(hold_ori, [10,6], 4) O11 = h.create_window(hold_ori, [10,10], 4) O12 = h.create_window(hold_ori, [10,14], 4) O13 = h.create_window(hold_ori, [14,2], 4) O14 = h.create_window(hold_ori, [14,6], 4) O15 = h.create_window(hold_ori, [14,10], 4) O16 = h.create_window(hold_ori, [14,14], 4) # new hold_mag = numpy.reshape(hold_mag, -1) hold_ori = numpy.reshape(hold_ori, -1) # new W1 = numpy.reshape(W1, -1) W2 = numpy.reshape(W2, -1) W3 = numpy.reshape(W3, -1) W4 = numpy.reshape(W4, -1) W5 = numpy.reshape(W5, -1) W6 = numpy.reshape(W6, -1) W7 = numpy.reshape(W7, -1) W8 = numpy.reshape(W8, -1) W9 = numpy.reshape(W9, -1) W10 = numpy.reshape(W10, -1) W11 = numpy.reshape(W11, -1) W12 = numpy.reshape(W12, -1) W13 = numpy.reshape(W13, -1) W14 = numpy.reshape(W14, -1) W15 = numpy.reshape(W15, -1) W16 = numpy.reshape(W16, -1) O1 = numpy.reshape(O1, -1) O2 = numpy.reshape(O2, -1) O3 = numpy.reshape(O3, -1) O4 = numpy.reshape(O4, -1) O5 = numpy.reshape(O5, -1) O6 = numpy.reshape(O6, -1) O7 = numpy.reshape(O7, -1) O8 = numpy.reshape(O8, -1) O9 = numpy.reshape(O9, -1) O10 = numpy.reshape(O10, -1) O11 = numpy.reshape(O11, -1) O12 = numpy.reshape(O12, -1) O13 = numpy.reshape(O13, -1) O14 = numpy.reshape(O14, -1) O15 = numpy.reshape(O15, -1) O16 = numpy.reshape(O16, -1) for j in range(0,len(O1)): bins1[i][0][O1[j]] += W1[j] bins2[i][0][O2[j]] += W2[j] bins3[i][0][O3[j]] += W3[j] bins4[i][0][O4[j]] += W4[j] bins5[i][0][O5[j]] += W5[j] bins6[i][0][O6[j]] += W6[j] bins7[i][0][O7[j]] += W7[j] bins8[i][0][O8[j]] += W8[j] bins9[i][0][O9[j]] += W9[j] bins10[i][0][O10[j]] += W10[j] bins11[i][0][O11[j]] += W11[j] bins12[i][0][O12[j]] += W12[j] bins13[i][0][O13[j]] += W13[j] bins14[i][0][O14[j]] += W14[j] bins15[i][0][O15[j]] += W15[j] bins16[i][0][O16[j]] += W16[j] # new for j in range(0, len(hold_ori)): bins[i][0][hold_ori[j]] += hold_mag[j] hold_mag = h.create_window(I[0], [35,35], window_size) hold_ori = h.create_window(I[0], [35,35], window_size) bin_i = bins[i][0] # new bin_i_1 = bins1[i][0] bin_i_2 = bins2[i][0] bin_i_3 = bins3[i][0] bin_i_4 = bins4[i][0] bin_i_5 = bins5[i][0] bin_i_6 = bins6[i][0] bin_i_7 = bins7[i][0] bin_i_8 = bins8[i][0] bin_i_9 = bins9[i][0] bin_i_10 = bins9[i][0] bin_i_11 = bins11[i][0] bin_i_12 = bins12[i][0] bin_i_13 = bins13[i][0] bin_i_14 = bins14[i][0] bin_i_15 = bins15[i][0] bin_i_16 = bins16[i][0] max_val1 = max(bin_i_1) max_index1 = [k for k, j in enumerate(bin_i_1) if j == max_val1] max_val2 = max(bin_i_2) max_index2 = [k for k, j in enumerate(bin_i_2) if j == max_val2] max_val3 = max(bin_i_3) max_index3 = [k for k, j in enumerate(bin_i_3) if j == max_val3] max_val4 = max(bin_i_4) max_index4 = [k for k, j in enumerate(bin_i_4) if j == max_val4] max_val5 = max(bin_i_5) max_index5 = [k for k, j in enumerate(bin_i_5) if j == max_val5] max_val6 = max(bin_i_6) max_index6 = [k for k, j in enumerate(bin_i_6) if j == max_val6] max_val7 = max(bin_i_7) max_index7 = [k for k, j in enumerate(bin_i_7) if j == max_val7] max_val8 = max(bin_i_8) max_index8 = [k for k, j in enumerate(bin_i_8) if j == max_val8] max_val9 = max(bin_i_9) max_index9 = [k for k, j in enumerate(bin_i_9) if j == max_val9] max_val10 = max(bin_i_10) max_index10 = [k for k, j in enumerate(bin_i_10) if j == max_val10] max_val11 = max(bin_i_11) max_index11 = [k for k, j in enumerate(bin_i_11) if j == max_val11] max_val12 = max(bin_i_12) max_index12 = [k for k, j in enumerate(bin_i_12) if j == max_val12] max_val13 = max(bin_i_13) max_index13 = [k for k, j in enumerate(bin_i_13) if j == max_val13] max_val14 = max(bin_i_14) max_index14 = [k for k, j in enumerate(bin_i_14) if j == max_val14] max_val15 = max(bin_i_15) max_index15 = [k for k, j in enumerate(bin_i_15) if j == max_val15] max_val16 = max(bin_i_16) max_index16 = [k for k, j in enumerate(bin_i_16) if j == max_val16] # new # index of max element in bin max_index = max(bin_i) max_index = [k for k, j in enumerate(bin_i) if j == max_index] # finds points within 80% of interest point holder_bin = [] holder_bin.append(max_index5[0]) max_val = bin_i[max_index5] for j in range(0, 35): if (bin_i[j] >= max_val * 0.8 and j != max_index5[0]): holder_bin.append(j) orien_of_bin.append(holder_bin) i += 1 o = open('orientss.txt', 'w') for i in orien_of_bin: o.write(str(i) + "\n") o.close()