def test_interest_points_descriptor_only(): np.random.seed(22) f = np.random.rand(256,256)*230 f = f.astype(np.uint8) full = surf.surf(f, 6, 24, 1) only = surf.surf(f, 6, 24, 1, descriptor_only=True) assert full.size > only.size
def test_determinant_zero(): img = mahotas.imread(path.join( path.abspath(path.dirname(__file__)), 'data', 'determinant_zero.png')) points = surf(img, threshold=.0) assert type(points) == np.ndarray
def createMasterList(): masterfile = open("metadata/master-data.txt",'w') f = [] mypath = "images/1fps" f = [] for i in range(309): f.append('x'+str(i+1)+'.jpg') print "Reading Files..." for image in f: imgname = 'images/1fps/'+image img = mahotas.imread(imgname, as_grey=True) # input image #extract description of all points of interest spoints = surf.surf(img, nr_octaves=4, nr_scales=6, initial_step_size=1, threshold=0.1, max_points=30, descriptor_only=True) info = "" #string for each image information newList = [sum(attr)/len(attr) for attr in zip(*spoints)] for i in range(len(newList)): info+=str(i+1)+":"+str(newList[i])+" " info+="\n" masterfile.write(info) masterfile.close() print "Completed master feature list..."
def processAttributes_surf(filePattern): targets_data = [] surf_features = [] counter = 0 for f in glob.glob(filePattern): counter+=1 print 'Reading image: ', counter, f target = 1 if 'cat' in f else 0 targets_data.append(target) image = mh.imread(f, as_grey=True) surf_features.append(surf.surf(image)[:, 5:]) X_train_surf_features = np.concatenate(surf_features) # Clusters n_clusters = 300 print 'Clustering', len(X_train_surf_features), 'features' estimator = MiniBatchKMeans(n_clusters=n_clusters) estimator.fit_transform(X_train_surf_features) x_data = [] for instance in surf_features: clusters = estimator.predict(instance) features = np.bincount(clusters) if len(features) < n_clusters: features = np.append(features, np.zeros((1, n_clusters-len(features)))) x_data.append(features) return x_data, targets_data
def test_show_surf(): np.random.seed(22) f = np.random.rand(256,256)*230 f = f.astype(np.uint8) spoints = surf.surf(f, 6, 24, 1) f2 = surf.show_surf(f, spoints) assert f2.shape == (f.shape + (3,))
def catsAnddogs(): import numpy as np import mahotas as mh from mahotas.features import surf from sklearn.linear_model import LogisticRegression import glob from sklearn.cluster import MiniBatchKMeans all_instance_filenames = [] all_instance_targets = [] for f in glob.glob('./data/train/*.jpg'): target = 1 if 'cat' in f else 0 all_instance_filenames.append(f) all_instance_targets.append(target) surf_features = [] counter = 0 for f in all_instance_filenames: print 'reading image:',f image = mh.imread(f,as_grey=True) surf_features.append(surf.surf(image)[:,5:]) train_len = int(len(all_instance_filenames)*.6) X_train_surf_features = np.concatenate(surf_features[:train_len]) X_test_surf_features = np.concatenate(surf_features[train_len:]) y_train = all_instance_targets[:train_len] y_test = all_instance_targets[train_len:] n_clusters = 300 print 'Clustering', len(X_train_surf_features), 'features' estimator = MiniBatchKMeans(n_clusters=n_clusters) estimator.fit_transform(X_train_surf_features) X_train = [] for instance in surf_features[:train_len]: clusters = estimator.predict(instance) features = np.bincount(clustes) if len(features) < n_clusters: features = np.append(features,np.zeros((1,n_clusters-len(features)))) X_train.append(features) X_test = [] for instance in surf_features[train_len:]: clusters = estimator.predict(instance) features = np.bincount(clustes) if len(features) < n_clusters: features = np.append(features,np.zeros((1,n_clusters-len(features)))) X_test.append(features) clf = LogisticRegression(C=0.001,penalty='l2') clf.fit_transform(X_train,y_train) predictions = clf.predict(X_test) print classification_report(y_test,predictions) print 'precision:', precision_score(y_test,predictions) print 'recall:', recall_score(y_test,predictions) print 'accuracy:', accuracy_score(y_test,predictions)
def get_visual_words(images, k): """ Given a list of image files, finds local features using SURF. The local features are then partitioned into k clusters, where each cluster represents a visual word. We then extract a bag of visual words for each image, which represents the feature set we can feed into a multinomial logistic classifier. Keyword arguments: images (list) - a list of strings representing the paths of training images k (int) - the number of visual words to create. This should be between 256-1024, depending on the number of images used. Returns: A numpy array of numpy arrays. Each element array consists of the bag of visual words for a particular image. """ print("Getting surf descriptors...") alldescriptors = [] for image in images: image = mh.imread(image, as_grey=True) image = image.astype(np.uint8) alldescriptors.append(surf.surf(image, descriptor_only=True)) # alldescriptors is a list of numpy arrays # use a smaller sample of descriptors for speed # first get all descriptors into a single array, # then take every 32nd vector. concatenated = np.concatenate(alldescriptors) concatenated = concatenated[::34] print('Done getting all surf descriptors.') print('Creating visual words...') km = KMeans(k) km.fit(concatenated) surf_features = [] for descriptor in alldescriptors: predicted = km.predict(descriptor) # for each image, predicted is an array of # integers representing to which cluster each # descriptor of said image belongs. # [np.sum(predicted == i) for i in xrange(k)] is an array # of length k that represents the bag of visual words for the image surf_features.append( np.array([np.sum(predicted == i) for i in xrange(k)]) ) visual_words = np.array(surf_features) print("Done creating the bags of visual words.") return visual_words
def test_interest_points_descriptors(): np.random.seed(22) f = np.random.rand(256,256)*230 f = f.astype(np.uint8) fi = surf.integral(f) spoints = surf.surf(f, 6, 24, 1) for arr, is_integral in zip([f,fi], [False, True]): points = surf.interest_points(arr, 6, 24, 1, is_integral=is_integral) points = list(points) points.sort(key=(lambda p: -p[3])) points = np.array(points, dtype=np.float64) descs = surf.descriptors(arr, points, is_integral) assert np.all(descs[:len(spoints)] == spoints)
def test_surf_guassians(): f = np.zeros((1024,1024)) Y,X = np.indices(f.shape) Y -= 768 X -= 768 f += 120*np.exp(-Y**2/2048.-X**2/480.) Y += 512 X += 512 f += 120*np.exp(-Y**2/2048.-X**2/480.) spoints = surf.surf(f, 1, 24, 2) YX = np.array([spoints[:,0],spoints[:,1]]).T is_256 = False is_768 = False for y,x in YX: if (np.abs(y-256) < 8 and np.abs(x-256) < 8): is_256 = True if (np.abs(y-768) < 8 and np.abs(x-768) < 8): is_768 = True assert is_256 assert is_768
def prepare(self,data): ftrs = [] for ind,datum in enumerate(data): img = datum.bwimage() spoints = surf.surf(img,descriptor_only=True,max_points=self._maxFeatures) #spoints = np.hstack((spoints,np.tile(ind,[spoints.shape[0],1]))) ftrs.append(spoints) datum.ext['bagofvisualwords'] = dict(features=spoints) logging.info("found %d features in image %d/%d" %(spoints.shape[0],ind,len(data))) #ftrs = np.vstack(ftrs) #ftrs = np.dstack(ftrs) #logging.info("found %d features in total"%ftrs.shape[0]) logging.info("running kmeans for %d clusters" % self._vocabularySize) self.estimator = Pipeline([ ('kmeans',VisualWordsEstimator(self._vocabularySize)), ('wrd',type("Wordizer",(object,),{"transform":lambda self,X,y=None : [" ".join(np.vectorize(str)(x)) for x in X],"fit":lambda self,X,y=None:self})()), ('vect', CountVectorizer(token_pattern='\\b\\d+\\b')), ('tfidf', TfidfTransformer()), ('pred',type("Predictor",(object,),{"transform":lambda self,X,y=None : X,"fit":lambda self,X,y=None:self,"predict":lambda self,X:X})()), ]) self.estimator.fit(ftrs) logging.info("kmeans done")
def surf_model(dirs): # Build local features based on Speeded Up Robust Feature (SURF) descriptors = [] # store local feature descriptors for idir in range(len(dirs)): files = [name for name in os.listdir(dirs[idir]) if os.path.isfile(os.path.join(dirs[idir], name))] for ifile in range(len(files)): if files[ifile][-3:] != 'jpg': # ignore non-image files continue image = mh.imread(dirs[idir]+files[ifile]).astype(np.uint8) # read image if (len(image.shape) == 3): # convert to gray if colored image = mh.colors.rgb2gray(image, dtype=np.uint8) descriptors.append(surf.surf(image, descriptor_only=True)) # Use select one every 32 features from all descriptors concat = np.concatenate(descriptors) con32 = concat[::32] # Cluster descriptors into 256 clusters k = 256 km = KMeans(k) km.fit(con32) # training KMeans model # Build the characteristic vector for each image # in the clustering space features = [] for d in descriptors: cls = km.predict(d) tmp = np.array([np.sum(cls == ci) for ci in range(k)]) features.append(tmp) # Return feature vectors features = np.array(features) return features
# -*- coding: utf-8 -* # mahotasによるSURF抽出 import numpy as np import cv2 from mahotas.features import surf img = cv2.imread("image/lena.jpg", 2) # 画像の読み込み img = cv2.resize(img, (100, 100)) # 画像のリサイズ(正規化のため) descriptors = surf.surf(img, descriptor_only=True) # descriptorsの抽出 print("number of SURF: %s" % len(descriptors)) maxi = descriptors.max() # histogram作成のための最大値と最小値 mini = descriptors.min() for d in descriptors: # 各特徴量をhistogram化 hist, label = np.histogram(descriptors, bins=10, range=(mini, maxi), density=True) print(hist)
hamming = 0 for i in range(len(vector1)): if vector1[i]!=vector2[i]: hamming+=1 return hamming if __name__ == "__main__": imgname = argv[-1] clf = trainData() img = mahotas.imread(imgname, as_grey=True) # input image #extract description of all points of interest spoints = surf.surf(img, nr_octaves=4, nr_scales=6, initial_step_size=1, threshold=0.1, max_points=30, descriptor_only=True) info = "" #string for each image information newList = [sum(attr)/len(attr) for attr in zip(*spoints)] for i in range(len(newList)): vectorinfo="" for j in range(len(clf)): vectorinfo+=str(int((clf[j].predict([newList]))[0]))+ " " vectorinfo+=" " vectorinfo=vectorinfo.split() results = hammingDistance(vectorinfo) print results
# Abro la imagen en escala de grises image = mahotas.imread(filename, as_grey=True) if (args.featuresGray != ""): # Extraer histograma de grises gray_hist, bins = np.histogram(image.flatten(), 256, [0, 256]) gray_features[imagename] = gray_hist # if (args.featuresLbp != ""): # Extraer lbp (sobre la misma imagen del anterior, en escala de grises) radius = 3 points = 4 * radius # Number of points to be considered as neighbourers lbp_hist = lbp.lbp(image, radius, points, ignore_zeros=False) lbp_features[imagename] = lbp_hist # if (args.featuresSurf != ""): # Extraer surf (sobre la misma imagen del anterior, en escala de grises) surf_features = surf.surf(image)[:, 5:] surf_all_hist[imagename] = surf_features # # Abro la imagen en colores image = mahotas.imread(filename, as_grey=False) # if (args.featuresColor != ""): # Extraer histograma de colores color_hist, bins = np.histogram(image.flatten(), 256, [0, 256]) color_features[imagename] = color_hist # if (args.featuresHara != ""): # Extraer histograma haraclick (sobre la misma imagen del anterior, en colores) hara_hist = mahotas.features.haralick(image).mean(0) hara_features[imagename] = hara_hist #------------------------------------------------------------------------------------------------------------------ if (args.featuresSurf != ""): # Clusterizo las features de 'surf'
print('Descriptors done') k = 256 km = KMeans(k) concatenated = np.loadtxt("test_SURF_concatenated.zat", delimiter=",") #concatenated = np.concatenate(alldescriptors) #concatenated = concatenated[::64] print('k-meaning...') km.fit(concatenated) features = [] basedir = 'test_dogs_vs_cats' images = glob('{}/*.jpg'.format(basedir)) ims = len(images) #for im in images: for i in range(1, ims + 1): im = 'test_dogs_vs_cats/' + str(i) + '.jpg' print('processing ' + str(im) + ' ...') im = mh.imread(im, as_grey=1) im = im.astype(np.uint8) d = surf.surf(im, descriptor_only=True) c = km.predict(d) features.append(np.array([np.sum(c == i) for i in xrange(k)])) features = np.array(features) np.savetxt("test_K-MEANS-ON_TRAINSET_featuresDogsCatsSURF.zat", features, delimiter=",") np.savetxt("test_SURF_concatenated2.zat", concatenated, delimiter=",")
clf = Pipeline([('preproc', StandardScaler()), ('classifier', grid)]) cv = cross_validation.KFold(len(features), 5, shuffle=True, random_state=123) scores = cross_validation.cross_val_score( clf, features, labels, cv=cv) print('Accuracy: {:.1%}'.format(scores.mean())) from mahotas.features import surf image = mh.demos.load('lena') image = mh.colors.rgb2gray(image, dtype=np.uint8) descriptors = surf.surf(image, descriptor_only=True) from mahotas.features import surf descriptors = surf.dense(image, spacing=16) alldescriptors = [] for im in images: im = mh.imread(im, as_grey=True) im = im.astype(np.uint8) alldescriptors.append(surf.dense(image, spacing=16)) # 하나의 배열에서 모든 디스크립터를 구한다 concatenated = np.concatenate(alldescriptors) print('Number of descriptors: {}'.format( len(concatenated))) # 64번째 벡터를 사용 concatenated = concatenated[::64] from sklearn.cluster import KMeans # FIXME CAPITALIZATION
print(fd_cat, fd_cat.shape) # # Localized feature extraction # # In[13]: from mahotas.features import surf import mahotas as mh cat_mh = mh.colors.rgb2gray(cat) dog_mh = mh.colors.rgb2gray(dog) cat_surf = surf.surf(cat_mh, nr_octaves=8, nr_scales=16, initial_step_size=1, threshold=0.1, max_points=50) dog_surf = surf.surf(dog_mh, nr_octaves=8, nr_scales=16, initial_step_size=1, threshold=0.1, max_points=54) fig = plt.figure(figsize=(10, 4)) ax1 = fig.add_subplot(1, 2, 1) ax1.imshow(surf.show_surf(cat_mh, cat_surf)) ax2 = fig.add_subplot(1, 2, 2) ax2.imshow(surf.show_surf(dog_mh, dog_surf))
from __future__ import print_function import mahotas.polygon from pylab import imshow, show import numpy as np from mahotas.features import surf f = np.zeros((1024,1024)) Y,X = np.indices(f.shape) Y -= 768 X -= 768 f += 120*np.exp(-Y**2/2048.-X**2/480.) Y += 512 X += 512 rho = .7 f += 120*np.exp(-1./( 2*(1-rho**2)) *( Y**2/32/32.+X**2/24/24. + 2*rho*X*Y/32./24.)) fi = surf.integral(f.copy()) spoints = surf.surf(f, 6, 24, 1) f2 = surf.show_surf(f, spoints) imshow(f2) show()
from sklearn.cluster import MiniBatchKMeans import os import glob all_instance_filenames = [] all_instance_targets = [] for f in glob.glob('data/PetImages/*/*.jpg'): target = 1 if 'Dog' in os.path.split(f)[0] else 0 all_instance_filenames.append(f) all_instance_targets.append(target) surf_features = [] for f in all_instance_filenames: image = mh.imread(f, as_grey=True) surf_features.append(surf.surf(image)[:, 5:]) train_len = int(len(all_instance_filenames) * .90) X_train_surf_features = np.concatenate(surf_features[:train_len]) X_test_surf_feautres = np.concatenate(surf_features[train_len:]) y_train = all_instance_targets[:train_len] y_test = all_instance_targets[train_len:] n_clusters = 300 estimator = MiniBatchKMeans(n_clusters=n_clusters) estimator.fit_transform(X_train_surf_features) X_train = [] for instance in surf_features[:train_len]: clusters = estimator.predict(instance) features = np.bincount(clusters)
import sys from os import path images = ['./1.JPG', './2.JPG', './3.JPG'] #images = ['./3.JPG'] debug = True if len(sys.argv) < 2 else False repeat = 1 if debug else 20 for image in images: f = mh.imread(image, as_grey=True) f = f.astype(np.uint8) for i in range(repeat): spoints = surf.surf(f=f, nr_octaves=4, nr_scales=6, initial_step_size=2, threshold=0.99, max_points=24, descriptor_only=False) if debug: print("Nr points:", len(spoints)) values = np.zeros(100) colors = np.array([(255, 0, 0)]) f2 = surf.show_surf(f, spoints[:100], values, colors) imshow(f2) show()
import cv2 import numpy as np from mahotas.features import surf import milk img = cv2.imread('img_07473.jpg') gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #img = cv2.imread('fly.png',0) # Create SURF object. You can specify params here or later. # Here I set Hessian Threshold to 400 #surf = cv2.SURF(400) #surf = surf.surf(400) # Find keypoints and descriptors directly #kp, des = surf.detectAndCompute(img,None) des = surf.surf(gray) kp = surf.interest_points(gray, threshold=10) print(len(kp)) #img2 = cv2.drawKeypoints(img,kp,None,(255,0,0),4) f2 = surf.show_surf(img, kp[:100], ) cv2.imwrite('surf_keypoints.jpg',img2)
target = 1 if 'cat' in f else 0 all_instance_filenames.append(f) all_instance_targets.append(target) #print f, target #sys.exit surf_features = [] counter = 0 for f in all_instance_filenames: counter+=1 #print 'Reading image: ', counter, f print counter image = mh.imread(f, as_grey=True) surf_features.append(surf.surf(image)[:, 5:]) #print surf.surf(image) #print len(surf_features[0]) # a = np.asarray(surf_features) a.tofile('surf_features.csv', sep=',', format='%10.5f') # train_len = int(len(all_instance_filenames) * .70) X_train_surf_features = np.concatenate(surf_features[:train_len]) X_test_surf_feautres = np.concatenate(surf_features[train_len:]) y_train = all_instance_targets[:train_len] y_test = all_instance_targets[train_len:]
import cv2 import numpy as np from mahotas.features import surf import milk img = cv2.imread('img_07473.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #f = mh.demos.load('luispedro', as_grey=True) f = gray.astype(np.uint8) spoints = surf.surf(f, nr_octaves=4, nr_scales=6, initial_step_size=2) print("Nr points:", len(spoints)) try: import milk descrs = spoints[:, 5:] k = 5 values, _ = milk.kmeans(descrs, k) colors = np.array([(255 - 52 * i, 25 + 52 * i, 37**i % 101) for i in range(k)]) except: values = np.zeros(100) colors = np.array([(255, 0, 0)]) f2 = surf.show_surf(f, spoints[:100], values, colors) cv2.imwrite('sift_keypoints.jpg', f2)
def test_3d_image(): surf.surf(np.arange(8*8*16).reshape((16,8,8)), 6, 24, 1)
def SIFT_SURF(): import mahotas as mh from mahotas.features import surf image = mh.imread('data/malpa.png', as_grey=True) print 'The 1st SURF descriptor is:\n', surf.surf(image)[0] print 'Extracted %s SURF descriptors' % len(surf.surf([image]))
mandrill = io.imread('image_detect.png') mandrill = equalize_hist(rgb2gray(mandrill)) corners = corner_peaks(corner_harris(mandrill), min_distance=2) show_corners(corners, mandrill) # advanced image featurization methods # i) Scale-Invariant Feature Transform (SIFT) # describes edges and corners # ii) Speeded-Up Robust Features (SURF) # descriptions that are invariant of scale, orientation, and illumination import mahotas as mh from mahotas.features import surf # import utilities image = mh.imread('image_detect_2.jpg') image = image[:, :, 0] # convert to grayscale surf_descriptor_1 = surf.surf(image)[0] num_descriptors = len(surf.surf(image)) # a SURF demo # DATA STANDARDIZATION # N(0,1) from sklearn import preprocessing import numpy as np X = np.array([[0., 0., 5., 13., 9., 1.], [0., 0., 13., 15., 10., 15.], [0., 3., 15., 2., 0., 11.]]) X_norm = preprocessing.scale(X) # [[ 0. -0.70710678 -1.38873015 0.52489066 0.59299945 -1.35873244] # [ 0. -0.70710678 0.46291005 0.87481777 0.81537425 1.01904933] # [ 0. 1.41421356 0.9258201 -1.39970842 -1.4083737 0.33968311]]
from __future__ import print_function import numpy as np import mahotas as mh from mahotas.features import surf from pylab import * from os import path f = mh.demos.load('luispedro', as_grey=True) f = f.astype(np.uint8) spoints = surf.surf(f, 4, 6, 2) print("Nr points:", len(spoints)) try: import milk descrs = spoints[:,5:] k = 5 values, _ =milk.kmeans(descrs, k) colors = np.array([(255-52*i,25+52*i,37**i % 101) for i in range(k)]) except: values = np.zeros(100) colors = np.array([(255,0,0)]) f2 = surf.show_surf(f, spoints[:100], values, colors) imshow(f2) show()
grid = GridSearchCV(LogisticRegression(), param_grid={'C' : C_range}) clf = Pipeline([('preproc', StandardScaler()), ('classifier', grid)]) cv = cross_validation.KFold(len(features), 5, shuffle=True, random_state=123) scores = cross_validation.cross_val_score( clf, features, labels, cv=cv) print('Accuracy: {:.1%}'.format(scores.mean())) from mahotas.features import surf image = mh.demos.load('lena') image = mh.colors.rgb2gray(image, dtype=np.uint8) descriptors = surf.surf(image, descriptor_only=True) from mahotas.features import surf descriptors = surf.dense(image, spacing=16) alldescriptors = [] for im in images: im = mh.imread(im, as_grey=True) im = im.astype(np.uint8) alldescriptors.append(surf.dense(image, spacing=16)) # get all descriptors into a single array concatenated = np.concatenate(alldescriptors) print(concatenated.shape) print('Number of descriptors: {}'.format( len(concatenated))) # use only every 64th vector concatenated = concatenated[::64]
mandrill = io.imread('image_detect.png') mandrill = equalize_hist(rgb2gray(mandrill)) corners = corner_peaks(corner_harris(mandrill),min_distance=2) show_corners(corners,mandrill) # advanced image featurization methods # i) Scale-Invariant Feature Transform (SIFT) # describes edges and corners # ii) Speeded-Up Robust Features (SURF) # descriptions that are invariant of scale, orientation, and illumination import mahotas as mh from mahotas.features import surf # import utilities image = mh.imread('image_detect_2.jpg') image = image[:,:,0] # convert to grayscale surf_descriptor_1 = surf.surf(image)[0] num_descriptors = len(surf.surf(image)) # a SURF demo # DATA STANDARDIZATION # N(0,1) from sklearn import preprocessing import numpy as np X = np.array([ [0.,0.,5.,13.,9.,1.], [0.,0.,13.,15.,10.,15.], [0.,3.,15.,2.,0.,11.] ]) X_norm = preprocessing.scale(X) # [[ 0. -0.70710678 -1.38873015 0.52489066 0.59299945 -1.35873244]
import mahotas as mh from glob import glob from mahotas.features import surf from sklearn.cluster import KMeans from sklearn.feature_extraction.text import TfidfTransformer picture_category_num = 10 feature_category_num = 512 # image surf images = glob('./*.jpg') alldescriptors = [] for im in images: im = mh.imread(im, as_grey=True) im = im.astype(np.uint8) alldescriptors.append(surf.surf(im, descriptor_only=True)) # image surf -> basic feature concatenated = np.concatenate(alldescriptors) km = KMeans(feature_category_num) km.fit(concatenated) # image surf and basic feature -> features features = [] for d in alldescriptors: c = km.predict(d) features.append( np.array([np.sum(c == ci) for ci in range(feature_category_num)])) features = np.array(features) # features -> tfidf
from __future__ import print_function import numpy as np import mahotas as mh from mahotas.features import surf from matplotlib import pyplot as plt from os import path f = mh.demos.load('luispedro', as_grey=True) f = f.astype(np.uint8) spoints = surf.surf(f, 4, 6, 2) print("Nr points:", len(spoints)) try: from sklearn.cluster import KMeans descrs = spoints[:, 5:] k = 5 values = KMeans(n_clusters=k).fit(descrs).labels_ colors = np.array([(255 - 52 * i, 25 + 52 * i, 37**i % 101) for i in range(k)]) except: values = np.zeros(100, int) colors = np.array([(255, 0, 0)]) f2 = surf.show_surf(f, spoints[:100], values, colors) fig, ax = plt.subplots() ax.imshow(f2) fig.show()
from skimage.color import rgb2gray import skimage.io as io from skimage.exposure import equalize_hist def show_corners(corners, image): fig = plt.figure() plt.gray() plt.imshow(image) y_corner, x_corner = zip(*corners) # inverse zip plt.plot(x_corner, y_corner, 'or') plt.xlim(0, image.shape[1]) plt.ylim(image.shape[0], 0) fig.set_size_inches(np.array(fig.get_size_inches()) * 1.5) plt.show() mandrill = io.imread('./data/mandrill.png') mandrill = equalize_hist(rgb2gray(mandrill)) corners = corner_peaks(corner_harris(mandrill), min_distance=2) show_corners(corners, mandrill) # SIFT and SURF import mahotas as mh from mahotas.features import surf image = mh.imread('./data/mandrill.png', as_grey=True) features = surf.surf(image) print(features[0])
def test_3d_image(): surf.surf(np.arange(8 * 8 * 16).reshape((16, 8, 8)), 6, 24, 1)
def test_3d_image(): with pytest.raises(ValueError): surf.surf(np.arange(8 * 8 * 16).reshape((16, 8, 8)), 6, 24, 1)
y_corner, x_corner = zip(*corners) plt.plot(x_corner, y_corner, 'or') plt.xlim(0, image.shape[1]) plt.ylim(image.shape[0], 0) fig.set_size_inches(np.array(fig.get_size_inches()) * 1.5) plt.show() mandrill = io.imread('mandrill.png') mandrill = equalize_hist(rgb2gray(mandrill)) corners = corner_peaks(corner_harris(mandrill), min_distance=2) show_corners(corners, mandrill) ################# Sample 16: SIFT and SURF ################# ### this code has some problem ### just because the package mahotas cannot be installed import mahotas as mh from mahotas.features import surf image = mh.imread('zipper.jpg', as_grey=True) print('The first SURF descriptor:\n', surf.surf(image)[0]) print('Extracted %s SURF descriptors' % len(surf.surf(image))) ################# Data standardization ################# from sklearn import preprocessing import numpy as np X = np.array([[0., 0., 5., 13., 9., 1.], [0., 0., 13., 15., 10., 15.], [0., 3., 15., 2., 0., 11.]]) print(preprocessing.scale(X))
# all_instance_filenames = [] all_instance_targets = [] for f in glob.glob(args.trainingSetPath + "*.jpg"): target = 1 if "cat" in f else 0 all_instance_filenames.append(f) all_instance_targets.append(target) # print f, target # surf_features = [] # image_counter = 0 saved_features = {} for filename in all_instance_filenames: image_counter += 1 imagename = os.path.basename(filename) # print "Processing image: ", image_counter, filename image = mh.imread(filename, as_grey=True) surf_data = surf.surf(image)[:, 5:] all_surf_data = "" for img_sa in surf_data: list2str_sa = ";".join(str(x) for x in img_sa) all_surf_data += list2str_sa + ":" # saved_features[imagename] = all_surf_data[:-1] # json.dump(saved_features, open(args.featuresFileName, "w")) #
def extractSURF(filename): import Image I = Image.open(filename) f = np.array(I.convert('L').getdata()).reshape(I.size[0],I.size[1]) f = f.astype(np.uint8) return surf.surf(f,descriptor_only=True)
def SIFT_SURF(): import mahotas as mh from mahotas.features import surf image = mh.imread('data/malpa.png',as_grey=True) print 'The 1st SURF descriptor is:\n',surf.surf(image)[0] print 'Extracted %s SURF descriptors' %len(surf.surf([image]))
from __future__ import print_function import mahotas.polygon from pylab import imshow, show import numpy as np from mahotas.features import surf f = np.zeros((1024, 1024)) Y, X = np.indices(f.shape) Y -= 768 X -= 768 f += 120 * np.exp(-Y**2 / 2048. - X**2 / 480.) Y += 512 X += 512 rho = .7 f += 120 * np.exp( -1. / (2 * (1 - rho**2)) * (Y**2 / 32 / 32. + X**2 / 24 / 24. + 2 * rho * X * Y / 32. / 24.)) fi = surf.integral(f.copy()) spoints = surf.surf(f, 6, 24, 1) f2 = surf.show_surf(f, spoints) imshow(f2) show()
print("Accuracy (5 fold x-val) with Logistic Regression [image features]: {:.1%}".format(scores0.mean())) from sklearn.cluster import KMeans from mahotas.features import surf print("Computing SURF descriptors...") alldescriptors = [] for im, _ in images(): im = mh.imread(im, as_grey=True) im = im.astype(np.uint8) # To use dense sampling, you can try the following line: # alldescriptors.append(surf.dense(im, spacing=16)) alldescriptors.append(surf.surf(im, descriptor_only=True)) print("Descriptor computation complete.") k = 256 km = KMeans(k) concatenated = np.concatenate(alldescriptors) print("Number of descriptors: {}".format(len(concatenated))) concatenated = concatenated[::64] print("Clustering with K-means...") km.fit(concatenated) sfeatures = [] for d in alldescriptors: c = km.predict(d) sfeatures.append(np.bincount(c, minlength=k)) sfeatures = np.array(sfeatures, dtype=float)
#concatenated = concatenated[::64] print('k-meaning...') km.fit(concatenated) features = [] basedir = 'test_dogs_vs_cats' images = glob('{}/*.jpg'.format(basedir)) ims = len(images) #for im in images: for i in range(1,ims+1): im = 'test_dogs_vs_cats/'+str(i)+'.jpg' print('processing ' +str(im) +' ...') im = mh.imread(im, as_grey=1) im = im.astype(np.uint8) d = surf.surf(im, descriptor_only=True) c = km.predict(d) features.append( np.array([np.sum(c == i) for i in xrange(k)]) ) features = np.array(features) np.savetxt("test_K-MEANS-ON_TRAINSET_featuresDogsCatsSURF.zat", features, delimiter=",") np.savetxt("test_SURF_concatenated2.zat", concatenated, delimiter=",")
def test_determinant_zero2(): img = np.zeros((128,28), np.uint8) points = surf(img, threshold=.0) assert type(points) == np.ndarray
def getSurfFeat(model_path, train_images, train_labels, test_images, test_labels): yield ('开始提取SURF特征') testFds = [] trainFds = [] if test_images.size: for data in test_images: gray = skimage.img_as_ubyte(rgb2gray(data)) fd = surf.surf(gray, nr_octaves=nr_octaves, nr_scales=nr_scales, initial_step_size=initial_step_size, threshold=threshold, max_points=max_points, descriptor_only=descriptor_only) testFds.append(fd) yield ('提取完测试特征') for data in train_images: gray = skimage.img_as_ubyte(rgb2gray(data)) fd = surf.surf(gray, nr_octaves=nr_octaves, nr_scales=nr_scales, initial_step_size=initial_step_size, threshold=threshold, max_points=max_points, descriptor_only=descriptor_only) trainFds.append(fd) yield ('提取完训练特征') #训练集聚类 yield ('特征聚类') train_surf_features = np.concatenate(trainFds) #print(train_surf_features.shape) estimator = MiniBatchKMeans(n_clusters=n_clusters) estimator.fit_transform(train_surf_features) #fit_transform()先拟合数据,再标准化 del train_surf_features gc.collect() yield ('正在生成训练词袋') #用kmeans的数据来预测训练集,生成词袋大小为301,词袋就是该图surf特征的别名 X_train = [] i = 0 for instance in trainFds: if instance.shape != (0, 64): clusters = estimator.predict(instance) features = np.bincount(clusters) #0-n的范围得到n+1个数分别表示0-n每个数出现的次数 if len(features) < n_clusters: features = np.append(features, np.zeros((1, n_clusters - len(features)))) X_train.append(features) else: train_labels_copy = np.delete(train_labels, i, axis=0) del train_labels gc.collect() train_labels = train_labels_copy i -= 1 i += 1 del trainFds gc.collect() yield ('正在生成测试词袋') #得到测试集surf特征的词袋 X_test = [] i = 0 for instance in testFds: if instance.shape != (0, 70): clusters = estimator.predict(instance) features = np.bincount(clusters) if len(features) < n_clusters: features = np.append(features, np.zeros((1, n_clusters - len(features)))) X_test.append(features) else: test_labels_copy = np.delete(test_labels, i, axis=0) del test_labels gc.collect() test_labels = test_labels_copy i -= 1 i += 1 del testFds gc.collect() sklearn.externals.joblib.dump(estimator, model_path + '.k') yield X_train, train_labels, X_test, test_labels
def extractSurf(filename): features_fname = filename descriptors = surf.surf(np.array(Image.open(filename).convert('L')),descriptor_only=True) print "Shape: ", descriptors.shape return descriptors
def getGreyPicsFeatures(path): image = array(Image.open(path).convert('L'), 'f') return surf.surf(image)[0]