Exemplo n.º 1
0
def ptest(d, ns):
    sift = SIFT(debug=d)

    print("SIFT precision test begins")
    print("preprocessing....")
    im = cv2.imread("target.jpg", 0)
    dp1, pos1 = sift.process(im, draw_keypoints=False)
    print("done")

    for name, im_test, validate in testset_generator(im):
        filename = os.path.join("precision_test", "match_%s.jpg" % name)
        cv2.imwrite(os.path.join("precision_test", "test_%s.jpg" % name),
                    im_test)

        t0 = time.time()
        matches = sift.match(im,
                             im_test,
                             draw_matches=not ns,
                             match_filename=filename,
                             descriptors1=dp1,
                             positions1=pos1)
        t = time.time() - t0

        prec = precision_test([[p1, p2] for p1, p2, _ in matches], validate)

        print("%d matches found for '%s'." % (len(matches), name))
        print("precision: %.5f" % prec)
        print("time consumed: %.3f sec" % t)
        if not ns:
            print("Matching result written to '%s'." % filename)
Exemplo n.º 2
0
def test():
    from sift import SIFT
    from skimage.io import imread, imsave
    import loader
    import matplotlib.pyplot as plt

    # img = 5*np.ones((50,40))
    # img[:25,:25] = 20.

    # img = imread('./examples/jinco2.jpg') # load jinco
    # data = './data/ykdelB_26906_19066'
    # data = './data/Haywrd_15302_19031'
    # testloader = loader.setup_testloader(data, args)
    # dataset = testloader.dataset
    # # print('num train data:', len(dataset))
    # ndataset = len(dataset)
    # img = dataset.get_image(20)
    # imsave('./examples/Haywrd_15302_19031_example.png', img)

    img = imread('./examples/Haywrd_15302_19031_example.png')  # load jinco

    # print('image shape', img.shape)
    desc, kp = detect_and_extract(img, Mmax=4, threshold=5., duplicate=False)

    sift_detector = SIFT(img, num_octave=8)
    kp_sift, desc_sift = sift_detector.compute()

    f, ax = plt.subplots(1, 2, figsize=(15, 15))
    ax[0].imshow(img)
    ax[0].scatter(kp[:, 0], kp[:, 1], c='r', s=2.5)
    ax[0].axis('off')
    ax[1].imshow(img)
    ax[1].scatter(kp_sift[:, 0], kp_sift[:, 1], c='r', s=2.5)
    ax[1].axis('off')
    plt.show()
Exemplo n.º 3
0
def rtest(d, ns):
    sift = SIFT(debug=d)

    print("SIFT resolution test begins.")

    print("preprocessing target....")
    im = cv2.imread("target.jpg", 0)
    dp1, pos1 = sift.process(im, draw_keypoints=False)
    precompute = []

    m1, m2 = ('nofile', 0), ('nofile', 0)
    print("done")

    print("preprocessing dataset....")
    t = 0
    for name, im_test in dataset_generator(dir_name="dataset"):
        t0 = time.time()

        dp2, pos2 = sift.process(im_test, draw_keypoints=False)
        precompute.append((dp2, pos2, name, im_test))

        t1 = time.time()
        t += t1 - t0
        print("'%s' done in %.5f secs" % (name, t1 - t0))
    print("done %d imgs in %.5f secs, aver %.5f secs" %
          (len(precompute), t, t / len(precompute)))

    print("matching ....")
    t0 = time.time()
    for dp2, pos2, name, im_test in precompute:
        filename = os.path.join("resolution_test", "match_%s" %
                                name)  # `name` should already end with '.jpg'

        m = sift.match(im,
                       im_test,
                       draw_matches=not ns,
                       match_filename=filename,
                       descriptors1=dp1,
                       positions1=pos1,
                       descriptors2=dp2,
                       positions2=pos2)

        if len(m) > m1[1]:
            m1, m2 = (name, len(m)), m1
        elif len(m) > m2[1]:
            m2 = (name, len(m))

        print("%d matches found for '%s'." % (len(m), name))
        if not ns:
            print("Matching result written to '%s'." % filename)
    t1 = time.time()
    print("done %d matches in %.5f secs" % (len(precompute), t1 - t0))

    resol = (1 - m2[1] / m1[1]) * (1 - 1 / m1[1])

    print("\n'%s' best matched with %d matches" % m1)
    print("'%s' with %d matches follows" % m2)
    print("Confidence : %.2f%%" % (resol * 100))
    def perform_feature_model(self, feature):
        if feature == 'sift':
            sift = SIFT(self.DATABASE_IMAGES_PATH)
            sift.read_and_clusterize(num_cluster=150)
            feature_vectors = sift.calculate_centroids_histogram()
        else:
            histogram_of_gradients = HistogramOfGradients(self.DATABASE_IMAGES_PATH)
            feature_vectors = histogram_of_gradients.get_image_vectors()

        self.database_connection.create_feature_model_table(feature)
        self.database_connection.insert_feature_data(feature, feature_vectors)
Exemplo n.º 5
0
def extract_or_load_features(im, kp_fname, feat_fname):
    if isfile(kp_fname) and isfile(feat_fname):
        #return pickle.load(open(kp_fname, 'rb'))[0], pickle.load(open(feat_fname, 'rb'))[0]
        kps, feats = pickle.load(open(kp_fname, 'rb')), pickle.load(open(feat_fname, 'rb'))
        kps = np.concatenate(kps, axis=0)
        feats = np.concatenate(feats, axis=0)
        return kps, feats

    detector = SIFT(im)
    _ = detector.get_features()
    pickle.dump(detector.kp_pyr, open(kp_fname, 'wb'))
    pickle.dump(detector.feats, open(feat_fname, 'wb'))
    return np.concatenate(detector.kp_pyr, axis=0), np.concatenate(detector.feats, axis=0)
    def predict(self, X, unflatten=False):
        assert X.ndim == 4
        print("Extracting SIFT features")
        n = X.shape[0]
        ret = []

        for i in tqdm(range(n)):
            sift = SIFT(self.nfeatures, self.noctave_layers, self.contrast_threshold, self.edge_threshold, self.sigma)
            ret.append(sift.calc_features_for_image(X[i,:,:,:], unflatten))
        
        if not unflatten:
            return numpy.array(ret)
        return ret
 def perform_classification_feature_model(self, feature, path, cluster_count):
     self.database_connection.create_feature_model_table(feature)
     if "histogram_of_gradients" in feature:
         histogram_of_gradients = HistogramOfGradients(path)
         feature_vectors = histogram_of_gradients.get_image_vectors()
     elif "local_binary_pattern" in feature:
         local_binary_pattern = LocalBinaryPattern(path)
         feature_vectors = local_binary_pattern.get_image_vectors()
     elif "sift" in feature:
         sift = SIFT(path)
         sift.read_and_clusterize(num_cluster=int(cluster_count))
         feature_vectors = sift.calculate_centroids_histogram()
     self.database_connection.insert_feature_data(feature, feature_vectors)
Exemplo n.º 8
0
def main(argv):
    sigma, k = parse_arguments(argv)
    sift = SIFT(sigma, k)
    print('Welcome to the testing of SIFT method!')
    q = 'r'
    while q == 'r':
        q = input('Would you like to run sift in silent mode? \'y or n\': ')
        while q != 'y' and q != 'n':
            q = input('Answer by y or n please: ')
        silent = (q == 'y')
        path_train = input('enter path to train image: ')
        img = cv2.imread(path_train, 0)
        print('might take some time... Don\' Worry!')
        kp_train, desc_train = sift.extract_features(img, silent)
        q = 't'
        while q == 't':
            path_test = input('enter path to test image: ')
            img = cv2.imread(path_test, 0)
            print('still computing...')
            kp_test, desc_test = sift.extract_features(img, silent)
Exemplo n.º 9
0
if __name__ == '__main__':
    num_img = 3

    kp_pyrs = []
    ims = []

    im = imread('C:/Users/Shantanu/img2.jpg')
    im = cv2.resize(im, (1268, 720))
    ims.append(im)

    if isfile('C:/Users/Shantanu/kp_pyr.pkl'):
        kp_pyrs.append(pickle.load(open('C:/Users/Shantanu/kp_pyr.pkl', 'rb')))

    print('Performing SIFT on image')

    sift_detector = SIFT(im)
    _ = sift_detector.get_features()
    kp_pyrs.append(sift_detector.kp_pyr)

    pickle.dump(sift_detector.kp_pyr, open('C:/Users/Shantanu/kp_pyr.pkl',
                                           'wb'))
    pickle.dump(sift_detector.feats,
                open('C:/Users/Shantanu/feat_pyr.pkl', 'wb'))

    import matplotlib.pyplot as plt

    _, ax = plt.subplots(1, 1)
    ax.imshow(ims[0])

    kps = kp_pyrs[0][0] * (2**0)
    ax.scatter(kps[:, 0], kps[:, 1], c='b', s=2)
Exemplo n.º 10
0
from lbp import LocalBinaryPatterns
from hog import HistgoramOrientedGradient
from sift import SIFT
from color import ColorHistogram
from haralick import Haralick
from occurrence import GreyCoMatrix
from sobel import Sobel
from coprop import GreyCoprops

import numpy as np
import os
import csv

lbp = LocalBinaryPatterns(8, 1)
hog = HistgoramOrientedGradient()
sift = SIFT()
color = ColorHistogram()
haralick = Haralick()
glcm = GreyCoMatrix()
sobel = Sobel()
coprop = GreyCoprops([1, 2], [0, np.pi / 2, np.pi, 3 * np.pi / 4])


def load_from_csv(csv_path):
    """
    every row include two column : image_path and image label
    """
    images_path = []
    labels = []
    with open(csv_path, newline='') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
Exemplo n.º 11
0
def save_samples(all_pos_samples, all_neg_samples):
    for i, image in enumerate(all_pos_samples):
        cv2.imwrite(os.path.join(POS_SAMPLE_PATH, "pos_%07d.png" % i), image)
    for i, image in enumerate(all_neg_samples):
        cv2.imwrite(os.path.join(NEG_SAMPLE_PATH, "neg_%07d.png" % i), image)


####################################################################

if __name__ == '__main__':
    # Record variables
    all_pos_samples = []
    all_neg_samples = []

    # Set up tracker.
    sift = SIFT(ROI, TEMPLATE_PATH)
    tracker = creat_tracker(tracker_type)

    # Read video
    files = glob.glob(IMAGE_PATH)
    assert len(files) > 0

    _, path_and_file = os.path.splitdrive(files[0])
    path, file = os.path.split(path_and_file)

    video = Video(files, FILE_FORMAT)
    frame_num = video.getFrameNumber()
    frames_counter = 0

    # Record variables
    video_name = path.split('/')[-1] + "_Samples_" + tracker_type + ".mp4"
Exemplo n.º 12
0
    # find the best parameter for run2
    # for size in [32*2**i for i in range(0, 4)] + [None]:
    #     for gap in [4*_ for _ in range(1, 3)]:
    #         patch_size = gap * 2
    #         for cluster_num in [200*_ for _ in range(1, 6)]:
    #             try:
    #                 bag = WordsBag(patch_gap=gap, patch_size=patch_size, cluster_num=cluster_num,
    #                                output_patch_image=False, mini_patch=True, pic_size=size,
    #                                force_generate_again=True)
    #                 bag.train()
    #                 mark = bag.test_model()
    #             except Exception as e:
    #                 continue
    #
    # # find the best parameter for run3:
    for cluster in [100*i for i in range(1, 11)]:
        try:
            sift = SIFT(cluster_num=cluster, mini_patch=True, force_generate_again=True)
            sift.train()
            mark = sift.test_model()
        except Exception as e:
            continue

    # find the best parameter for gist
    # for img_size in [32*i for i in range(1, 9)]:
    #     gist = GIST(pic_size=(img_size, img_size))
    #     gist.train()
    #     gist.test_model()

Exemplo n.º 13
0
    #returning array(x1, y1, x2, y2)
    def generate_matches(self):

        perpective_matrix = self.find_matrix()
        new_img = cv2.warpPerspective(self.img, perpective_matrix,
                                      self.mapped_to_img.shape[::-1])

        return self.get_matches(perpective_matrix), new_img


#__my own version__
img = cv2.imread(sys.argv[1], cv2.IMREAD_GRAYSCALE)
changed_img = cv2.imread(sys.argv[2], cv2.IMREAD_GRAYSCALE)

empty_sift_inst = SIFT(is_static=True)

# img_features, _ = SIFT(img).get_features()
# changed_img_features, _ = SIFT(changed_img).get_features()

# #_original_sift_
sift = cv2.xfeatures2d.SIFT_create(contrastThreshold=0.1)
kp, des = sift.detectAndCompute(img, None)
kp_changed, des_changed = sift.detectAndCompute(changed_img, None)
img_features = np.zeros(shape=(len(des), 4), dtype=np.object)
changed_img_features = np.zeros(shape=(len(des_changed), 4), dtype=np.object)

for i in range(img_features.shape[0]):
    img_features[i, :] = kp[i].pt[1], kp[i].pt[0], kp[i].size, des[i].reshape(
        1, -1)
for i in range(changed_img_features.shape[0]):
###################### DEFINING DATA PATH ######################
'''

-> Please change the following paths.

-> The "writeImg" option in sift.getPoints is to save images as processed
   by the SIFT function. Please update the "writeDir" option to change the path
   to save the processed images.

'''

dataDir = '../data'
train_fldr = 'train_imgs1'
resultFile = 'results.csv'

###################### GETTING POINTS AND SAVING RESULTS ######################

imgNames = os.listdir('{}/{}'.format(dataDir, train_fldr))

sift = SIFT(alphaMin=0.2, alphaMax=0.45)
saveFile = open('{}/{}'.format(dataDir, resultFile), 'w')

for name in tqdm(imgNames[0:1]):
    matchPoints = sift.getPoints('{}/{}/{}'.format(dataDir, train_fldr, name),
                                 '{}/templates'.format(dataDir),
                                 writeImg=True,
                                 writeDir='../data/results/')
    saveFile.write(str(name) + ',' + str(matchPoints) + '\n')

saveFile.close()
Exemplo n.º 15
0
def main():
    args = parse_args()

    train_image_dir_path = args.data

    model = {}

    image_size = (640, 360)
    keypoint_image_border_size = 10
    max_keypoint_count = 512

    model["image_size"] = (640, 360)
    model["keypoint_image_border_size"] = keypoint_image_border_size
    model["max_keypoint_count"] = max_keypoint_count

    if args.local_feature == "SIFT":
        ldescriptor_length = 128
        sift_contrast_threshold = 0.04
        sift_edge_threshold = 10
        local_feature = SIFT(
            image_size=image_size,
            keypoint_image_border_size=keypoint_image_border_size,
            max_keypoint_count=max_keypoint_count,
            ldescriptor_length=ldescriptor_length,
            contrast_threshold=sift_contrast_threshold,
            edge_threshold=sift_edge_threshold)
        model["local_feature"] = "SIFT"
        model["ldescriptor_length"] = ldescriptor_length
        model["sift_contrast_threshold"] = sift_contrast_threshold
        model["sift_edge_threshold"] = sift_edge_threshold
    elif args.local_feature == "SURF":
        ldescriptor_length = 128
        surf_hessian_threshold = 400.0
        surf_extended = True
        surf_upright = True
        local_feature = SURF(
            image_size=image_size,
            keypoint_image_border_size=keypoint_image_border_size,
            max_keypoint_count=max_keypoint_count,
            ldescriptor_length=ldescriptor_length,
            hessian_threshold=surf_hessian_threshold,
            extended=surf_extended,
            upright=surf_upright)
        model["local_feature"] = "SURF"
        model["ldescriptor_length"] = ldescriptor_length
        model["surf_hessian_threshold"] = surf_hessian_threshold
        model["surf_extended"] = surf_extended
        model["surf_upright"] = surf_upright
    elif args.local_feature == "VGG":
        ldescriptor_length = 120
        vgg_use_scale_orientation = False
        local_feature = VGG(
            image_size=image_size,
            keypoint_image_border_size=keypoint_image_border_size,
            max_keypoint_count=max_keypoint_count,
            ldescriptor_length=ldescriptor_length,
            use_scale_orientation=vgg_use_scale_orientation)
        model["local_feature"] = "VGG"
        model["ldescriptor_length"] = ldescriptor_length
        model["vgg_use_scale_orientation"] = vgg_use_scale_orientation
    elif args.local_feature == "SP":
        ldescriptor_length = 256
        sp_weights_path = os.path.join(args.out_dir_path, "superpoint_v1.pth")
        local_feature = SuperPointLocalFeature(
            image_size=image_size,
            keypoint_image_border_size=keypoint_image_border_size,
            max_keypoint_count=max_keypoint_count,
            ldescriptor_length=ldescriptor_length,
            weights_path=sp_weights_path)
        model["local_feature"] = "SP"
        model["ldescriptor_length"] = ldescriptor_length
        model["sp_weights_path"] = sp_weights_path
    else:
        raise ValueError("local_feature")

    lcluster_centers = calc_lcluster_centers(
        train_image_dir_path=train_image_dir_path, local_feature=local_feature)
    model["lcluster_centers"] = lcluster_centers

    global_feature = VLAD(lcluster_centers=lcluster_centers)

    pca_mean, pca_eigenvectors = calc_pca(
        train_image_dir_path=train_image_dir_path,
        local_feature=local_feature,
        global_feature=global_feature)
    model["pca_mean"] = pca_mean
    model["pca_eigenvectors"] = pca_eigenvectors

    model_file_path = os.path.join(args.out_dir_path, args.model_file_name)
    np.savez_compressed(model_file_path, **model)
Exemplo n.º 16
0
def main():
    args = parse_args()

    model_file_path = os.path.join(args.model_dir_path, args.model_file_name)
    model = dict(np.load(model_file_path))

    image_size = tuple(model["image_size"])
    keypoint_image_border_size = int(model["keypoint_image_border_size"])
    max_keypoint_count = int(model["max_keypoint_count"])
    ldescriptor_length = int(model["ldescriptor_length"])

    if str(model["local_feature"]) == "SIFT":
        sift_contrast_threshold = float(model["sift_contrast_threshold"])
        sift_edge_threshold = int(model["sift_edge_threshold"])
        local_feature = SIFT(
            image_size=image_size,
            keypoint_image_border_size=keypoint_image_border_size,
            max_keypoint_count=max_keypoint_count,
            ldescriptor_length=ldescriptor_length,
            contrast_threshold=sift_contrast_threshold,
            edge_threshold=sift_edge_threshold)
    elif str(model["local_feature"]) == "SURF":
        surf_hessian_threshold = float(model["surf_hessian_threshold"])
        surf_extended = bool(model["surf_extended"])
        surf_upright = bool(model["surf_upright"])
        local_feature = SURF(
            image_size=image_size,
            keypoint_image_border_size=keypoint_image_border_size,
            max_keypoint_count=max_keypoint_count,
            ldescriptor_length=ldescriptor_length,
            hessian_threshold=surf_hessian_threshold,
            extended=surf_extended,
            upright=surf_upright)
    elif str(model["local_feature"]) == "VGG":
        vgg_use_scale_orientation = bool(model["vgg_use_scale_orientation"])
        local_feature = VGG(
            image_size=image_size,
            keypoint_image_border_size=keypoint_image_border_size,
            max_keypoint_count=max_keypoint_count,
            ldescriptor_length=ldescriptor_length,
            use_scale_orientation=vgg_use_scale_orientation)
    elif str(model["local_feature"]) == "SP":
        sp_weights_path = str(model["sp_weights_path"])
        local_feature = SuperPointLocalFeature(
            image_size=image_size,
            keypoint_image_border_size=keypoint_image_border_size,
            max_keypoint_count=max_keypoint_count,
            ldescriptor_length=ldescriptor_length,
            weights_path=sp_weights_path)
    else:
        raise ValueError("local_feature")

    lcluster_centers = model["lcluster_centers"]

    global_feature = VLAD(lcluster_centers=lcluster_centers)

    pca_mean = model["pca_mean"]
    pca_eigenvectors = model["pca_eigenvectors"]

    img_desc_calc = PCAGlobalDescriptor(local_feature=local_feature,
                                        global_feature=global_feature,
                                        pca_mean=pca_mean,
                                        pca_eigenvectors=pca_eigenvectors)

    image_file_path = os.path.join(args.data, args.image_file_name)
    image = cv2.imread(filename=image_file_path, flags=0)
    image = cv2.resize(image, local_feature.image_size)
    descr = img_desc_calc.calc_descriptor(image)
    print(descr)