def test_get_gradients():
    sobel_x = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
    sobel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
    dummy_image = np.arange(48).reshape(-1, 6).astype('float32')

    true_ix = cv2.filter2D(dummy_image,
                           ddepth=-1,
                           kernel=sobel_x,
                           borderType=cv2.BORDER_CONSTANT)
    true_iy = cv2.filter2D(dummy_image,
                           ddepth=-1,
                           kernel=sobel_y,
                           borderType=cv2.BORDER_CONSTANT)

    sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
    sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])

    true_ix2 = cv2.filter2D(dummy_image,
                            ddepth=-1,
                            kernel=sobel_x,
                            borderType=cv2.BORDER_CONSTANT)
    true_iy2 = cv2.filter2D(dummy_image,
                            ddepth=-1,
                            kernel=sobel_y,
                            borderType=cv2.BORDER_CONSTANT)

    ix, iy = get_gradients(dummy_image)

    assert (np.allclose(true_ix, ix)
            and np.allclose(true_iy, iy)) or (np.allclose(true_ix2, ix)
                                              and np.allclose(true_iy2, iy))
示例#2
0
def get_features(image, x, y, feature_width):
    """
    This function returns the SIFT features computed at each of the input points
    You should code the above helper functions first, and use them below.
    You should also use your implementation of image gradients from before. 
    Hint: run get_feat_vec() with a loop
    Args:
    -   image: A numpy array of shape (m,n), the image
    -   x: A numpy array of shape (k,), the x-coordinates of interest points
    -   y: A numpy array of shape (k,), the y-coordinates of interest points
    -   feature_width: integer representing the local feature width in pixels.
            You can assume that feature_width will be a multiple of 4 (i.e. every
                cell of your local SIFT-like feature will have an integer width
                and height). This is the initial window size we examine around
                each keypoint.

    Returns:
    -   fvs: A numpy array of shape (k, feat_dim) representing all feature vectors.
            "feat_dim" is the feature_dimensionality (e.g. 128 for standard SIFT).
            These are the computed features.
    """
    assert image.ndim == 2, 'Image must be grayscale'
    #############################################################################
    # TODO: YOUR CODE HERE                                                      #                                          #
    #############################################################################

    # raise NotImplementedError('`get_features` function in ' +
    #     '`student_sift.py` needs to be implemented')

    fvs = []
    fw = int(feature_width // 2)

    dx, dy = get_gradients(image)
    magnitudes, orientations = get_magnitudes_and_orientations(dx, dy)
    for _x, _y in zip(x, y):
        fv = get_feat_vec(_x, _y, magnitudes, orientations, feature_width)
        fvs.append(fv)
    fvs = np.array(fvs)

    #############################################################################
    #                             END OF YOUR CODE                              #
    #############################################################################
    return fvs
示例#3
0
def test_get_gradients2():

  sobel_x = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
  sobel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
  dummy_image = np.array(
    [
      [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
      [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
      [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
      [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
      [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 0., 0., 0., 0., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
      [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
      [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
      [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
      [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
      [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]
    ])

  true_ix = cv2.filter2D(dummy_image, ddepth = -1, kernel = sobel_x, borderType = cv2.BORDER_CONSTANT)
  true_iy = cv2.filter2D(dummy_image, ddepth = -1, kernel = sobel_y, borderType = cv2.BORDER_CONSTANT)

  sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
  sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])

  true_ix2 = cv2.filter2D(dummy_image, ddepth = -1, kernel = sobel_x, borderType = cv2.BORDER_CONSTANT)
  true_iy2 = cv2.filter2D(dummy_image, ddepth = -1, kernel = sobel_y, borderType = cv2.BORDER_CONSTANT)

  ix, iy = get_gradients(dummy_image)

  assert (np.allclose(true_ix, ix) and np.allclose(true_iy, iy)) or (np.allclose(true_ix2, ix) and np.allclose(true_iy2, iy))
示例#4
0
def get_features(image, x, y, feature_width):
    """
    This function returns the SIFT features computed at each of the input points
    You should code the above helper functions first, and use them below.
    You should also use your implementation of image gradients from before. 

    Args:
    -   image: A numpy array of shape (m,n), the image
    -   x: A numpy array of shape (k,), the x-coordinates of interest points
    -   y: A numpy array of shape (k,), the y-coordinates of interest points
    -   feature_width: integer representing the local feature width in pixels.
            You can assume that feature_width will be a multiple of 4 (i.e. every
                cell of your local SIFT-like feature will have an integer width
                and height). This is the initial window size we examine around
                each keypoint.

    Returns:
    -   fvs: A numpy array of shape (k, feat_dim) representing all feature vectors.
            "feat_dim" is the feature_dimensionality (e.g. 128 for standard SIFT).
            These are the computed features.
    """
    assert image.ndim == 2, 'Image must be grayscale'
    #############################################################################
    # TODO: YOUR CODE HERE                                                      #
    #############################################################################
    fvs = np.zeros((x.shape[0], 128))
    ix, iy = get_gradients(image)
    mag_point, ori_point = get_magnitudes_and_orientations(ix, iy)
    for ind in range(0, x.shape[0]):
        fvs[ind] = get_feat_vec(x[ind], y[ind], mag_point, ori_point,
                                feature_width)

    #############################################################################
    #                             END OF YOUR CODE                              #
    #############################################################################
    return fvs