linewidth=0,
    s=2,  # small points, w/o borders
    c=range(len(contour)))  # continuous coloring (so that plots match)

plt.scatter(0, 0)

plt.title('in Cartesian Coordinates')

plt.grid()

plt.show()
# check a few scikitlearn image feature extractions, if they can help us

from skimage.feature import corner_harris, corner_subpix, corner_peaks, CENSURE

detector = CENSURE()

detector.detect(img)

coords = corner_peaks(corner_harris(img), min_distance=5)

coords_subpix = corner_subpix(img, coords, window_size=13)

plt.subplot(121)

plt.title('CENSURE feature detection')

plt.imshow(img, cmap='Set3')

plt.scatter(detector.keypoints[:, 1],
            detector.keypoints[:, 0],
Esempio n. 2
0
'''
    Author: Mahnoor Anjum
    Description:
        Detection
'''
from skimage import data
from skimage import transform
from skimage.feature import CENSURE
from skimage.color import rgb2gray
import cv2

import matplotlib.pyplot as plt

img_orig = cv2.imread('data/original.jpg', 0)

detector = CENSURE(non_max_threshold=0.05, line_threshold=20)

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(12, 6))

detector.detect(img_orig)

ax.imshow(img_orig, cmap=plt.cm.gray)
ax.scatter(detector.keypoints[:, 1],
           detector.keypoints[:, 0],
           2**detector.scales,
           facecolors='none',
           edgecolors='r')
ax.set_title("Censure")
plt.tight_layout()
plt.show()
ax[0].set_title("Original")
ax[1].imshow(gray2, cmap=plt.cm.gray)
ax[1].plot(coords2[:, 1],
           coords2[:, 0],
           color='cyan',
           marker='o',
           linestyle='None',
           markersize=6)
ax[1].plot(coords_subpix2[:, 1], coords_subpix2[:, 0], '+r', markersize=15)
ax[1].set_title("Template")
fig.tight_layout()
plt.show()

# Censure Keypoints

censure = CENSURE()
censure.detect(gray1)
kp1 = censure.keypoints
censure.detect(gray2)
kp2 = censure.keypoints

fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes.ravel()
ax[0].imshow(gray1, cmap=plt.cm.gray)
ax[0].plot(kp1[:, 1],
           kp1[:, 0],
           color='cyan',
           marker='o',
           linestyle='None',
           markersize=6)
ax[0].set_title("Original")
Esempio n. 4
0
def test_censure_on_rectangular_images():
    """Censure feature detector should work on 2D image of any shape."""
    rect_image = np.random.rand(300, 200)
    square_image = np.random.rand(200, 200)
    CENSURE().detect((square_image))
    CENSURE().detect((rect_image))
Esempio n. 5
0
def test_keypoints_censure_color_image_unsupported_error():
    """Censure keypoints can be extracted from gray-scale images only."""
    assert_raises(ValueError, CENSURE().detect, np.zeros((20, 20, 3)))
Esempio n. 6
0
def display_features(orig_img,
                     dsae_feats=None,
                     dsae_duration=None,
                     other_feats=[
                         'corner_harris', 'corner_fast', 'CENSURE_STAR',
                         'CENSURE_Octagon', 'CENSURE_DoB', 'ORB', 'daisy'
                     ]):
    """
    display the original image, optionally displaying the identified features over it

    Input:
    - orig_img
        original image
    - dsae_feats
        2d array of spatial feature coordinates of shape (nb_feats,2)
        Note, the dimension length 2 has its first element representing the i coordinate and
        its second element representing the j coordinate
    - dsea_duration
        float: the amount of time it took to preprocess the image and produce the predicted encoding (features)
    - other_feats
        list of strings: a list of the other types of feature detectors to visually compare with ours
    Returns:
    """
    nimgs = len(other_feats) + 1
    nrows = 2
    ncols = nimgs // 2
    if nimgs % 2 != 0:
        ncols += 1
    if ncols == 1:
        ncols += 1
    assert (nrows * ncols >= nimgs)
    print('nrows:%d\tncols:%d' % (nrows, ncols))
    fig, ax = plt.subplots(figsize=(20, 10), nrows=nrows, ncols=ncols)
    row, col = 0, 0
    gray_img = rgb2gray(orig_img)
    dsae_feats_orig = dsae_feats.copy()
    if orig_img.shape[0] == 3:
        H = orig_img.shape[1]
        W = orig_img.shape[2]
    else:
        H = orig_img.shape[0]
        W = orig_img.shape[1]

    # dsae_feats_orig[:,0] *= H
    # dsae_feats_orig[:,1] *= W

    # plt.imshow(orig_img)
    ax[row, col].imshow(orig_img)
    if dsae_feats is not None:
        extractor = 'DSAE_VGG (ours)'
        # plt.scatter(x=dsae_feats_orig[:,1], y=dsae_feats_orig[:,0], c='r', s=15)
        ax[row, col].scatter(x=dsae_feats_orig[:, 1],
                             y=dsae_feats_orig[:, 0],
                             c='r',
                             s=15)
        ax[row, col].text(0,
                          0,
                          extractor,
                          color='r',
                          fontsize=25,
                          fontweight='bold')
        print('dsae_duration:')
        print(dsae_duration)
        # sys.exit(1)
        if dsae_duration:
            ax[row, col].text(0,
                              15,
                              '%s ms' % dsae_duration,
                              color='b',
                              fontsize=15)
            print('%s: %f ms' % (extractor, dsae_duration))
    # plt.show()
    row, col = inc_row_col(row, col, nrows, ncols)

    if 'ORB' in other_feats:
        extractor = 'ORB'
        descriptor_extractor = ORB(n_keypoints=256)
        start = time.time()
        descriptor_extractor.detect_and_extract(gray_img)
        orb_feats = descriptor_extractor.keypoints
        end = time.time()
        duration = (end - start) * 1000.  # milliseconds now
        # descriptors1 = descriptor_extractor.descriptors
        ax[row, col].imshow(orig_img)
        ax[row, col].scatter(x=orb_feats[:, 1], y=orb_feats[:, 0], c='b', s=15)
        ax[row, col].text(0,
                          0,
                          extractor,
                          color='b',
                          fontsize=25,
                          fontweight='bold')
        print('H', H)
        ax[row, col].text(0,
                          H + 80,
                          '%s ms' % duration,
                          color='b',
                          fontsize=15)
        print('%s: %f ms' % (extractor, duration))
        row, col = inc_row_col(row, col, nrows, ncols)
    if 'blob_dog' in other_feats:
        extractor = 'blob_dog'
        start = time.time()
        blob_dog_feats = blob_dog(gray_img)
        end = time.time()
        duration = (end - start) * 1000.  # milliseconds now
        ax[row, col].imshow(orig_img)
        ax[row, col].scatter(x=blob_dog_feats[:, 1],
                             y=blob_dog_feats[:, 0],
                             c='g',
                             s=15)
        ax[row, col].text(0,
                          H + 80,
                          '%s ms' % duration,
                          color='g',
                          fontsize=15)
        print('%s: %f ms' % (extractor, duration))
        row, col = inc_row_col(row, col, nrows, ncols)
    if 'corner_fast' in other_feats:
        extractor = 'corner_fast'
        start = time.time()
        corner_fast_feats = corner_peaks(corner_fast(gray_img))
        end = time.time()
        duration = (end - start) * 1000.  # milliseconds now
        ax[row, col].imshow(orig_img)
        ax[row, col].scatter(x=corner_fast_feats[:, 1],
                             y=corner_fast_feats[:, 0],
                             c='m',
                             s=15)
        ax[row, col].text(0,
                          0,
                          'corner_fast',
                          color='m',
                          fontsize=25,
                          fontweight='bold')
        ax[row, col].text(0,
                          H + 80,
                          '%s ms' % duration,
                          color='m',
                          fontsize=15)
        print('%s: %f ms' % (extractor, duration))
        row, col = inc_row_col(row, col, nrows, ncols)
    if 'corner_harris' in other_feats:
        extractor = 'corner_harris'
        start = time.time()
        corner_harris_feats = corner_peaks(corner_harris(gray_img))
        end = time.time()
        duration = (end - start) * 1000.  # milliseconds now
        ax[row, col].imshow(orig_img)
        ax[row, col].scatter(x=corner_harris_feats[:, 1],
                             y=corner_harris_feats[:, 0],
                             c='m',
                             s=15)
        ax[row, col].text(0,
                          0,
                          'corner_harris',
                          color='m',
                          fontsize=25,
                          fontweight='bold')
        ax[row, col].text(0,
                          H + 80,
                          '%s ms' % duration,
                          color='m',
                          fontsize=15)
        print('%s: %f ms' % (extractor, duration))
        row, col = inc_row_col(row, col, nrows, ncols)
    if 'CENSURE_STAR' in other_feats:
        extractor = 'CENSURE_STAR'
        censure = CENSURE(mode='STAR')
        start = time.time()
        censure.detect(gray_img)
        censure_star_keypoints = censure.keypoints
        end = time.time()
        duration = (end - start) * 1000.  # milliseconds now
        ax[row, col].imshow(orig_img)
        ax[row, col].scatter(x=censure_star_keypoints[:, 1],
                             y=censure_star_keypoints[:, 0],
                             c='k',
                             s=15)
        ax[row, col].text(0,
                          0,
                          'CENSURE_STAR',
                          color='k',
                          fontsize=25,
                          fontweight='bold')
        ax[row, col].text(0,
                          H + 80,
                          '%s ms' % duration,
                          color='k',
                          fontsize=15)
        print('%s: %f ms' % (extractor, duration))
        row, col = inc_row_col(row, col, nrows, ncols)
    if 'CENSURE_Octagon' in other_feats:
        extractor = 'CENSURE_Octagon'
        censure = CENSURE(mode='Octagon')
        start = time.time()
        censure.detect(gray_img)
        censure_oct_keypoints = censure.keypoints
        end = time.time()
        duration = (end - start) * 1000.  # milliseconds now
        ax[row, col].imshow(orig_img)
        ax[row, col].scatter(x=censure_oct_keypoints[:, 1],
                             y=censure_oct_keypoints[:, 0],
                             c='k',
                             s=15)
        ax[row, col].text(0,
                          0,
                          'CENSURE_Octagon',
                          color='k',
                          fontsize=25,
                          fontweight='bold')
        ax[row, col].text(0,
                          H + 80,
                          '%s ms' % duration,
                          color='k',
                          fontsize=15)
        print('%s: %f ms' % (extractor, duration))
        row, col = inc_row_col(row, col, nrows, ncols)
    if 'CENSURE_DoB' in other_feats:
        extractor = 'CENSURE_DoB'
        censure = CENSURE(mode='DoB')
        start = time.time()
        censure.detect(gray_img)
        censure_dob_keypoints = censure.keypoints
        end = time.time()
        duration = (end - start) * 1000.  # milliseconds now
        ax[row, col].imshow(orig_img)
        ax[row, col].scatter(x=censure_dob_keypoints[:, 1],
                             y=censure_dob_keypoints[:, 0],
                             c='k',
                             s=15)
        ax[row, col].text(0,
                          0,
                          'CENSURE_DoB',
                          color='k',
                          fontsize=25,
                          fontweight='bold')
        ax[row, col].text(0,
                          H + 80,
                          '%s ms' % duration,
                          color='k',
                          fontsize=15)
        print('%s: %f ms' % (extractor, duration))
        row, col = inc_row_col(row, col, nrows, ncols)
    if 'daisy' in other_feats:  # SIFT-like feature descriptor
        extractor = 'daisy'
        start = time.time()
        # descs, descs_img = daisy(gray_img, visualize=True)

        descs, descs_img = daisy(gray_img,
                                 step=50,
                                 radius=25,
                                 rings=2,
                                 histograms=6,
                                 orientations=8,
                                 visualize=True)
        end = time.time()
        duration = (end - start) * 1000.  # milliseconds now
        ax[row, col].imshow(orig_img)
        ax[row, col].imshow(descs_img)
        ax[row, col].text(0,
                          0,
                          'Daisy',
                          color='w',
                          fontsize=25,
                          fontweight='bold')
        ax[row, col].text(0,
                          H + 80,
                          '%s ms' % duration,
                          color='w',
                          fontsize=15)
        print('%s: %f ms' % (extractor, duration))
        row, col = inc_row_col(row, col, nrows, ncols)
    plt.show()
Esempio n. 7
0
def test_keypoints_censure_scale_range_error():
    """Difference between the the max_scale and min_scale parameters in
    keypoints_censure should be greater than or equal to two."""
    with testing.raises(ValueError):
        CENSURE(min_scale=1, max_scale=2)
Esempio n. 8
0
def test_keypoints_censure_mode_validity_error():
    """Mode argument in keypoints_censure can be either DoB, Octagon or
    STAR."""
    with testing.raises(ValueError):
        CENSURE(mode='dummy')