def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN x, y = f.pt t1Mx = transformations.get_trans_mx(np.array([-x, -y, 0])) rotMx = transformations.get_rot_mx(0, 0, -f.angle / 180 * np.pi) scaleMx = transformations.get_scale_mx(0.2, 0.2, 0) t2Mx = transformations.get_trans_mx(np.array([4, 4, 0])) transMx = np.dot(np.dot(np.dot(t2Mx, scaleMx), rotMx), t1Mx)[:-1, :] transMx = np.delete(transMx, 2, axis=0) transMx = np.delete(transMx, 2, axis=1) # print(transMx) # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN std = np.std(destImage) if std < 1e-5: desc[i] = np.zeros((1, windowSize * windowSize)) else: desc[i] = ((destImage - np.mean(destImage)) / std).reshape( (1, windowSize * windowSize)) # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) x, y = f.pt # TODO-BLOCK-BEGIN angle = np.deg2rad(f.angle) #= orientationImage[y][x] response = f.response #= harrisImage[y][x] T1 = transformations.get_trans_mx(np.array([-x, -y, 0])) R = transformations.get_rot_mx(0, 0, -angle) S = transformations.get_scale_mx(.2, .2, 0) T2 = transformations.get_trans_mx(np.array([4, 4, 0])) four_x_four = np.dot(np.dot(np.dot(T2, S), R), T1) transMx = four_x_four[0:2, [0, 1, 3]] # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is negligibly small (which we # define as less than 1e-10) then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN #print (np.linalg.norm(destImage)) std = np.std(destImage) if std**2 < 1e-10: desc[i] = (np.zeros(np.shape(destImage))).flatten() else: desc[i] = ((destImage - np.mean(destImage)) / std).flatten() # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) trans1 = transformations.get_trans_mx( np.array([-f.pt[0], -f.pt[1], 0])) rotate = transformations.get_rot_mx(0, 0, -np.radians(f.angle)) scale = transformations.get_scale_mx(0.2, 0.2, 1) trans2 = transformations.get_trans_mx(np.array([4, 4, 0])) transMx = np.dot(trans2, np.dot(scale, np.dot(rotate, trans1)))[:2, (0, 1, 3)] # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. destImage = destImage.flatten() std = np.std(destImage) if std < 1e-5: continue dest_mean = np.mean(destImage) std_dest = np.std(destImage) desc[i, :] = (destImage - dest_mean) / std_dest return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): transMx = np.zeros((2, 3)) # TODO 5: Compute the transform as described by the feature # location/orientation and store in 'transMx.' You will need # to compute the transform from each pixel in the 40x40 rotated # window surrounding the feature to the appropriate pixels in # the 8x8 feature descriptor image. 'transformations.py' has # helper functions that might be useful # Note: use grayImage to compute features on, not the input image # TODO-BLOCK-BEGIN T1 = transformations.get_trans_mx(np.array([-f.pt[0], -f.pt[1], 0])) R = transformations.get_rot_mx(0, 0, np.radians(-f.angle)) S = transformations.get_scale_mx(1/5, 1/5, 1) T2 = transformations.get_trans_mx(np.array([4, 4, 0])) transMx = T2 @ S @ R @ T1 transMx = np.delete(transMx, [2, 3], axis=0) transMx = np.delete(transMx, 2, axis=1) # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is negligibly small (which we # define as less than 1e-10) then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN if np.var(destImage) < 1e-10: desc[i] = np.zeros(windowSize * windowSize) else: destImage -= np.mean(destImage) destImage /= np.std(destImage) desc[i] = destImage.flatten() # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): # raise NotImplementedError('NOT IMPLEMENTED') image = image.astype(np.float32) image /= 255. grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) windowSize = 8 cellSize = 2 binSize = 90 numBin = 360 // binSize desc = np.zeros( (len(keypoints), ((windowSize**2) // (cellSize**2) * numBin))) # quantized orientation for i, f in enumerate(keypoints): transMx = np.zeros((2, 3)) x, y = int(f.pt[0]), int(f.pt[1]) T1 = transformations.get_trans_mx(np.array([-x, -y, 0])) R = transformations.get_rot_mx(0, 0, np.radians(-f.angle)) S = transformations.get_scale_mx(0.2, 0.2, 0) T2 = transformations.get_trans_mx(np.array([4, 4, 0])) transMx = np.dot(T2, np.dot(S, np.dot(R, T1))) transMx = np.delete(transMx, 2, 1)[0:2, 0:3] destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) destImage = destImage - np.mean(destImage) if np.var(destImage) < 10**-10: destImage = np.zeros([windowSize, windowSize]) else: destImage = (destImage / np.std(destImage)) orientationImage = np.zeros((windowSize, windowSize), dtype=float) Ix = ndimage.sobel(destImage, 0) Iy = ndimage.sobel(destImage, 1) orientationImage = np.degrees(np.arctan2(Iy, Ix)) + 179 gradientMagnitude = np.sqrt(Ix**2 + Iy**2) patchBin = [] # Iterate cells for xCell in range(0, windowSize, cellSize): for yCell in range(0, windowSize, cellSize): cellBin = np.zeros(numBin) # Iterate pixels in a cell for xPixel in range(cellSize): for yPixel in range(cellSize): x = xCell + xPixel y = yCell + yPixel binIdx = int(orientationImage[x][y] // binSize) cellBin[binIdx] += gradientMagnitude[x][y] patchBin = np.append(patchBin, cellBin) desc[i] = patchBin return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: Descriptor numpy array, dimensions: keypoint number x feature descriptor dimension ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 10 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 3) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN x, y = f.pt T1 = transformations.get_trans_mx(np.array((-x, -y, 0))) R = transformations.get_rot_mx(0, 0, np.radians(-f.angle)) S = transformations.get_scale_mx(.3, .3, 0) T2 = transformations.get_trans_mx( np.array((windowSize / 2, windowSize / 2, 0))) tmp = np.dot(T2, np.dot(S, np.dot(R, T1))) tmp = tmp[:2, (0, 1, 3)] #print(tmp) transMx = tmp # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN flat = destImage.flatten() if flat.std() < 1e-5: continue flat = (flat - flat.mean()) / flat.std() desc[i, :] = flat # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) cnt = 0 for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. T2 = transformations.get_trans_mx( np.array([windowSize / 2, windowSize / 2, 0])) S = transformations.get_scale_mx(0.2, 0.2, 1) R = transformations.get_rot_mx(0, 0, -np.radians(f.angle)) T1 = transformations.get_trans_mx(np.array([-f.pt[0], -f.pt[1], 0])) #T2 * S * R * T1 transMx = np.dot(np.dot(np.dot(T2, S), R), T1) transMx = np.delete(transMx, 2, axis=1)[:2] # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. target = destImage[:8, :8] target = target - np.mean(target) # numeric issue, set a very small threshold instead of zero if np.std(target) <= 10**(-5): desc[i, :] = np.zeros((windowSize * windowSize, )) else: target = target / np.std(target) desc[i, :] = target.reshape(windowSize * windowSize) return desc
def get_transformation_matrix(tl, bl, tr, theta): """Returns transformation matrix that translates center to (0,0) and rotates image CCW by orientation theta. Then scales to be unit square centered at (0,0)""" # Calculates the center gps coordinates of the image/rectangle center_x = (bl[0] + tr[0])/2 center_y = (bl[1] + tr[1])/2 # Calculates the width and height in gps coordinates of the image/rectangle len_x = math.sqrt(math.pow(tl[0] - tr[0], 2) + math.pow(tl[1] - tr[1], 2)) len_y = math.sqrt(math.pow(tl[0] - bl[0], 2) + math.pow(tl[1] - bl[1], 2)) # Translation, Rotation, Scale matrix calculations trans_vec = np.array([-center_x, -center_y]) trans_mx = transformations.get_trans_mx(trans_vec) rot_mx = transformations.get_rot_mx(theta) scale_mx = transformations.get_scale_mx(1/len_x, 1/len_y) return np.matmul(scale_mx, np.matmul(rot_mx, trans_mx))
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): transMx = np.zeros((2, 3)) t1_x = - f.pt[0] t1_y = - f.pt[1] t1_mat = transformations.get_trans_mx(np.array([t1_x, t1_y, 0]))[(0, 1, 3), :][:, (0, 1, 3)] rot_mat = transformations.get_rot_mx(0, 0, - np.deg2rad(f.angle))[(0, 1, 3), :][:, (0, 1, 3)] s_mat = transformations.get_scale_mx(0.2, 0.2, 0)[(0, 1, 3), :][:, (0, 1, 3)] t2_mat = transformations.get_trans_mx(np.array([4, 4, 0]))[(0, 1, 3), :][:, (0, 1, 3)] transMx = (np.matmul(t2_mat, np.matmul(s_mat, np.matmul(rot_mat, t1_mat)))) transMx = transMx[:2] # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) std = np.std(destImage) if std < 10 ** (-5): desc[i] = 0 else: destImage -= np.mean(destImage) destImage /= std desc[i] = np.reshape(destImage, (windowSize**2)) return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) desc = np.zeros((len(keypoints), 8 * 8)) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) #print( transMx, transMx.shape) # TODO-BLOCK-BEGIN trans_matrix = get_trans_mx( np.array([-int(f.pt[0]), -int(f.pt[1]), 0])) rotation_mat = get_rot_mx(0, 0, -1 * np.radians(f.angle)) scale_mat = get_scale_mx(0.2, 0.2, 1) trans_matrix_2 = get_trans_mx(np.array([4, 4, 0])) final_trans_mat = np.dot( trans_matrix_2, np.dot(scale_mat, np.dot(rotation_mat, trans_matrix))) # if f.angle > 0.0: # print(final_trans_mat) transMx = final_trans_mat[:2, (0, 1, 3)] # if f.angle > 0.0: # print(final_trans_mat,f.pt[0],f.pt[1],transMx) # raise Exception("TODO 5: in features.py not implemented") # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is negligibly small (which we # define as less than 1e-10) then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN destImage = destImage.reshape([1, 64]) mean = np.mean(destImage) std = np.std(destImage) # #norm #destImage=destImage-mean # temp_desc = destImage.reshape([1,64]) # std = np.std(temp_desc) # temp_desc-=np.mean(temp_desc) # if f.angle > 0.0: # print(destImage,transMx) if std < 1e-5: destImage *= 0 desc[i, :] = destImage #continue else: desc[i, :] = (destImage - mean) / std #temp_desc = temp_desc/std #desc[i] = destImage #raise Exception("TODO 6: in features.py not implemented") # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN vector_x = f.pt[0] vector_y = f.pt[1] vector_z = 0 vector = np.array([-vector_x, -vector_y, -vector_z]) trans1 = transformations.get_trans_mx(vector) rot = transformations.get_rot_mx(0, 0, -f.angle / 180 * np.pi) scale = transformations.get_scale_mx(.2, .2, 1) trans2 = transformations.get_trans_mx(np.array([4, 4, 0])) temp = np.dot(trans2, np.dot(scale, np.dot(rot, trans1))) transMx = np.array([[temp[0][0], temp[0][1], temp[0][3]], [temp[1][0], temp[1][1], temp[1][3]]]) # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN destImage = destImage - destImage.mean() stdDev = destImage.std() if stdDev < 1e-5: destImage = np.zeros((windowSize, windowSize)) else: destImage = destImage / stdDev destImage = destImage.flatten() desc[i] = destImage # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN x, y = f.pt x, y = int(x), int(y) T1 = transformations.get_trans_mx(np.array([-x, -y, 0])) R = transformations.get_rot_mx(0, 0, -f.angle * np.pi / 180) S = transformations.get_scale_mx(0.2, 0.2, 1) T2 = transformations.get_trans_mx(np.array([4, 4, 0])) transMx3D = T2 @ R @ S @ T1 transMx[:, 0:2] = transMx3D[:2, :2] transMx[:, 2] = transMx3D[:2, 3] #raise Exception("TODO in features.py not implemented") # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN if np.std(destImage) <= 1e-5: desc[i] = np.zeros_like(destImage).flatten() else: destImage = (destImage - np.mean(destImage)) / np.std(destImage) desc[i] = destImage.flatten() #raise Exception("TODO in features.py not implemented") # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN rot_mx = transformations.get_rot_mx(0, 0, -np.radians(f.angle)) trs_mx = transformations.get_trans_mx( np.array([-f.pt[0], -f.pt[1], 0])) scale_mx = transformations.get_scale_mx(0.2, 0.2, 1.0) trs2_mx = transformations.get_trans_mx(np.array([4.0, 4.0, 0])) temp0 = np.dot(trs2_mx, np.dot(scale_mx, np.dot(rot_mx, trs_mx))) transMx[:, 0:2] = temp0[0:2, 0:2] transMx[:, 2] = temp0[0:2, 3] #raise Exception("TODO in features.py not implemented") # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is negligibly small (which we # define as less than 1e-10) then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN destImage = destImage - destImage.mean() if destImage.std() >= 1e-5: destImage = destImage / destImage.std() else: destImage = np.zeros_like(destImage) #raise Exception("TODO in features.py not implemented") # TODO-BLOCK-END desc[i] = destImage.flatten() return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' # Cast to float image = image.astype(np.float32) # Convert to BGR scale image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pindex_xel in the 40x40 rotated window surrounding # the feature to the appropriate pindex_xels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN vector = np.array([-1 * f.pt[0], -1 * f.pt[1], 0]) # Inputs translation vector represented by 1D numpy array wand outputs 4x4 numpy array with 3D translation translation_1 = transformations.get_trans_mx(vector) # Inputs angles in x,y,z orientations and ouputs 4x4 numpy array with 3D rotation rotation = transformations.get_rot_mx(0, 0, -f.angle / 180 * np.pi) # Inputs scaling in x,y,z, directions and puts 4x4 numpy array with 3D scaling scale = transformations.get_scale_mx(0.2, 0.2, 1) translation_2 = transformations.get_trans_mx(np.array([4, 4, 0])) temp = np.dot(translation_2, np.dot(scale, np.dot(rotation, translation_1))) transMx = np.array([[temp[0][0], temp[0][1], temp[0][3]], [temp[1][0], temp[1][1], temp[1][3]]]) # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrindex_x destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is negligibly small (which we # define as less than 1e-10) then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN if destImage.std() < 1e-10: destImage = np.zeros((windowSize, windowSize)) else: destImage = (destImage - destImage.mean()) / destImage.std() destImage = destImage.flatten() desc[i] = destImage # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN x, y = f.pt t1Mx = transformations.get_trans_mx(np.array([-x, -y, 0.0])) r1Mx = transformations.get_rot_mx(0.0, 0.0, -np.radians(f.angle)) s1Mx = transformations.get_scale_mx(0.2, 0.2, 1.0) t2Mx = transformations.get_trans_mx(np.array([4.0, 4.0, 0.0])) computedMx = np.dot(t2Mx, np.dot(s1Mx, np.dot(r1Mx, t1Mx))) transMx[0:2, 0:2] = computedMx[0:2, 0:2] transMx[0, 2] = computedMx[0, 3] transMx[1, 2] = computedMx[1, 3] # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN variance = np.var(destImage) std = np.std(destImage) if (variance < 10.0**(-10.0)): normIm = np.zeros((1, 64)) else: normIm = np.divide(1.0 * (destImage - np.mean(destImage)), std) desc[i] = normIm.flatten() # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN x, y = f.pt transMt1 = transformations.get_trans_mx(np.array([-x, -y, 0])) angle = f.angle / 180. * np.pi transMr = transformations.get_rot_mx(0, 0, -angle) transMs = transformations.get_scale_mx(1. / 5, 1. / 5, 1) transMt2 = transformations.get_trans_mx(np.array([4, 4, 0])) transMx1 = np.dot(transMr, transMt1) transMx1 = np.dot(transMs, transMx1) transMx1 = np.dot(transMt2, transMx1) transMx[:, :2] = transMx1[:2, :2] transMx[:, 2] = transMx1[:2, 3] # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN # if destImage[0, 0] != 0: # print destImage mean = destImage.mean() destImage = destImage - mean std = destImage.std() # if mean != 0: # print mean # print "mean above std down" # print std if std > 1e-5: destImage = destImage / std else: destImage = np.zeros((8, 8), dtype=np.float32) for m in xrange(8): for n in xrange(8): desc[i, 8 * n + m] = destImage[n, m] # TODO-BLOCK-END return desc
def describeFeatures(self, image, keypoints): ''' Input: image -- BGR image with values between [0, 255] keypoints -- the detected features, we have to compute the feature descriptors at the specified coordinates Output: desc -- K x W^2 numpy array, where K is the number of keypoints and W is the window size ''' image = image.astype(np.float32) image /= 255. # This image represents the window around the feature you need to # compute to store as the feature descriptor (row-major) windowSize = 8 desc = np.zeros((len(keypoints), windowSize * windowSize)) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayImage = ndimage.gaussian_filter(grayImage, 0.5) for i, f in enumerate(keypoints): # TODO 5: Compute the transform as described by the feature # location/orientation. You will need to compute the transform # from each pixel in the 40x40 rotated window surrounding # the feature to the appropriate pixels in the 8x8 feature # descriptor image. transMx = np.zeros((2, 3)) # TODO-BLOCK-BEGIN # T1 (x, y) = f.pt T1 = transformations.get_trans_mx(np.array([-x, -y, 0])) # R angle_x = 0 angle_y = 0 angle_z = -f.angle / 180 * np.pi R = transformations.get_rot_mx(angle_x, angle_y, angle_z) # S S = transformations.get_scale_mx(0.2, 0.2, 1.0) # T2 T2 = transformations.get_trans_mx(np.array([4.0, 4.0, 0])) trans = np.dot(T2, np.dot(S, np.dot(R, T1))) transMx[0][0] = trans[0][0] transMx[0][1] = trans[0][1] transMx[0][2] = trans[0][3] transMx[1][0] = trans[1][0] transMx[1][1] = trans[1][1] transMx[1][2] = trans[1][3] # TODO-BLOCK-END # Call the warp affine function to do the mapping # It expects a 2x3 matrix destImage = cv2.warpAffine(grayImage, transMx, (windowSize, windowSize), flags=cv2.INTER_LINEAR) # TODO 6: Normalize the descriptor to have zero mean and unit # variance. If the variance is zero then set the descriptor # vector to zero. Lastly, write the vector to desc. # TODO-BLOCK-BEGIN destImage = destImage - destImage.mean() if float(destImage.std()) >= 1e-5: vec = destImage / destImage.std() desc[i] = vec.flatten() else: desc[i] = np.zeros(windowSize * windowSize) # TODO-BLOCK-END return desc
#!/usr/bin/env python3 import numpy as np import sys, os, imp import cv2 import transformations import traceback from features import * from scipy import signal if __name__ == "__main__": #print ("myTest works") #HKD = HarrisKeypointDetector() #print(ndimage.filters.gaussian_filter(25, .5)) #print (HKD.computeHarrisValues(np.array([[1,1,1],[0,0,0],[1,1,1]]))) #m_image = np.array([[1,0,0],[0,0,0],[0,0,0]]) #HKD = HarrisKeypointDetector() #maxes = HKD.computeLocalMaxima(m_image) #print (maxes) # #print(HKD.detectKeypoints(np.array([[[1,1,1],[1,1,1],[1,1,1]],[[0,0,0],[0,0,0],[0,0,0]],[[1,1,1],[1,1,1],[1,1,1]]]))) x = 40 y = 40 angle = 2 T1 = transformations.get_trans_mx(np.array([-x, -y, 0])) R = transformations.get_rot_mx(0, 0, -angle) S = transformations.get_scale_mx(.2, .2, 0) T2 = transformations.get_trans_mx(np.array([4, -4, 0])) four_x_four = np.dot(np.dot(np.dot(T2, S), R), T1) transMx = four_x_four[0:2, [0, 1, 3]] print(transMx)