<s> import numpy as np <EOL> import mahotas as mh <EOL> image = mh . imread ( '<STR_LIT>' ) <EOL> from matplotlib import pyplot as plt <EOL> plt . imshow ( image ) <EOL> plt . show ( ) <EOL> image = mh . colors . rgb2grey ( image , dtype = np . uint8 ) <EOL> plt . imshow ( image ) <EOL> plt . gray ( ) <EOL> thresh = mh . thresholding . otsu ( image ) <EOL> print ( '<STR_LIT>' . format ( thresh ) ) <EOL> plt . imshow ( image > thresh ) <EOL> im16 = mh . gaussian_filter ( image , <NUM_LIT:16> ) <EOL> im = mh . demos . load ( '<STR_LIT>' ) <EOL> r , g , b = im . transpose ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> r12 = mh . gaussian_filter ( r , <NUM_LIT> ) <EOL> g12 = mh . gaussian_filter ( g , <NUM_LIT> ) <EOL> b12 = mh . gaussian_filter ( b , <NUM_LIT> ) <EOL> im12 = mh . as_rgb ( r12 , g12 , b12 ) <EOL> h , w = r . shape <EOL> Y , X = np . mgrid [ : h , : w ] <EOL> Y = Y - h / <NUM_LIT> <EOL> Y = Y / Y . max ( ) <EOL> X = X - w / <NUM_LIT> <EOL> X = X / X . max ( ) <EOL> C = np . exp ( - <NUM_LIT> * ( X ** <NUM_LIT:2> + Y ** <NUM_LIT:2> ) ) <EOL> C = C - C . min ( ) <EOL> C = C / C . ptp ( ) <EOL> C = C [ : , : , None ] <EOL> ringed = mh . stretch ( im * C + ( <NUM_LIT:1> - C ) * im12 ) <EOL> haralick_features = mh . features . haralick ( image ) <EOL> haralick_features_mean = np . mean ( haralick_features , axis = <NUM_LIT:0> ) <EOL> haralick_features_all = np . ravel ( haralick_features ) <EOL> from glob import glob <EOL> images = glob ( '<STR_LIT>' ) <EOL> features = [ ] <EOL> labels = [ ] <EOL> for im in images : <EOL> labels . append ( im [ : - len ( '<STR_LIT>' ) ] ) <EOL> im = mh . imread ( im ) <EOL> im = mh . colors . rgb2gray ( im , dtype = np . uint8 ) <EOL> features . append ( mh . features . haralick ( im ) . ravel ( ) ) <EOL> features = np . array ( features ) <EOL> labels = np . array ( labels ) <EOL> from sklearn . pipeline import Pipeline <EOL> from sklearn . preprocessing import StandardScaler <EOL> from sklearn . linear_model import LogisticRegression <EOL> clf = Pipeline ( [ ( '<STR_LIT>' , StandardScaler ( ) ) , <EOL> ( '<STR_LIT>' , LogisticRegression ( ) ) ] ) <EOL> from sklearn import cross_validation <EOL> cv = cross_validation . LeaveOneOut ( len ( images ) ) <EOL> scores = cross_validation . cross_val_score ( <EOL> clf , features , labels , cv = cv ) <EOL> print ( '<STR_LIT>' . format ( scores . mean ( ) ) ) <EOL> def chist ( im ) : <EOL> im = im // <NUM_LIT:64> <EOL> r , g , b = im . transpose ( ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> pixels = <NUM_LIT:1> * r + <NUM_LIT:4> * b + <NUM_LIT:16> * g <EOL> hist = np . bincount ( pixels . ravel ( ) , minlength = <NUM_LIT:64> ) <EOL> hist = hist . astype ( float ) <EOL> hist = np . log1p ( hist ) <EOL> return hist <EOL> features = [ ] <EOL> for im in images : <EOL> im = mh . imread ( im ) <EOL> features . append ( chist ( im ) ) <EOL> features = [ ] <EOL> for im in images : <EOL> imcolor = mh . imread ( im ) <EOL> im = mh . colors . rgb2gray ( imcolor , dtype = np . uint8 ) <EOL> features . append ( np . concatenate ( [ <EOL> mh . features . haralick ( im ) . ravel ( ) , <EOL> chist ( imcolor ) , <EOL> ] ) ) <EOL> scores = cross_validation . cross_val_score ( <EOL> clf , features , labels , cv = cv ) <EOL> print ( '<STR_LIT>' . format ( scores . mean ( ) ) ) <EOL> features = [ ] <EOL> for im in images : <EOL> imcolor = mh . imread ( im ) <EOL> imcolor = imcolor [ <NUM_LIT:200> : - <NUM_LIT:200> , <NUM_LIT:200> : - <NUM_LIT:200> ] <EOL> im = mh . colors . rgb2gray ( imcolor , dtype = np . uint8 ) <EOL> features . append ( np . concatenate ( [ <EOL> mh . features . haralick ( im ) . ravel ( ) , <EOL> chist ( imcolor ) , <EOL> ] ) ) <EOL> sc = StandardScaler ( ) <EOL> features = sc . fit_transform ( features ) <EOL> from scipy . spatial import distance <EOL> dists = distance . squareform ( distance . pdist ( features ) ) <EOL> fig , axes = plt . subplots ( <NUM_LIT:2> , <NUM_LIT:9> ) <EOL> for ci , i in enumerate ( range ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:10> ) ) : <EOL> left = images [ i ] <EOL> dists_left = dists [ i ] <EOL> right = dists_left . argsort ( ) <EOL> right = right [ <NUM_LIT:1> ] <EOL> right = images [ right ] <EOL> left = mh . imread ( left ) <EOL> right = mh . imread ( right ) <EOL> axes [ <NUM_LIT:0> , ci ] . imshow ( left ) <EOL> axes [ <NUM_LIT:1> , ci ] . imshow ( right ) <EOL> from sklearn . grid_search import GridSearchCV <EOL> C_range = <NUM_LIT> ** np . arange ( - <NUM_LIT:4> , <NUM_LIT:3> ) <EOL> grid = GridSearchCV ( LogisticRegression ( ) , param_grid = { '<STR_LIT:C>' : C_range } ) <EOL> clf = Pipeline ( [ ( '<STR_LIT>' , StandardScaler ( ) ) , <EOL> ( '<STR_LIT>' , grid ) ] ) <EOL> cv = cross_validation . KFold ( len ( features ) , <NUM_LIT:5> , <EOL> shuffle = True , random_state = <NUM_LIT> ) <EOL> scores = cross_validation . cross_val_score ( <EOL> clf , features , labels , cv = cv ) <EOL> print ( '<STR_LIT>' . format ( scores . mean ( ) ) ) <EOL> from mahotas . features import surf <EOL> image = mh . demos . load ( '<STR_LIT>' ) <EOL> image = mh . colors . rgb2gray ( image , dtype = np . uint8 ) <EOL> descriptors = surf . surf ( image , descriptor_only = True ) <EOL> from mahotas . features import surf <EOL> descriptors = surf . dense ( image , spacing = <NUM_LIT:16> ) <EOL> alldescriptors = [ ] <EOL> for im in images : <EOL> im = mh . imread ( im , as_grey = True ) <EOL> im = im . astype ( np . uint8 ) <EOL> alldescriptors . append ( surf . dense ( image , spacing = <NUM_LIT:16> ) ) <EOL> concatenated = np . concatenate ( alldescriptors ) <EOL> print ( '<STR_LIT>' . format ( <EOL> len ( concatenated ) ) ) <EOL> concatenated = concatenated [ : : <NUM_LIT:64> ] <EOL> from sklearn . cluster import KMeans <EOL> k = <NUM_LIT> <EOL> km = KMeans ( k ) <EOL> km . fit ( concatenated ) <EOL> features = [ ] <EOL> for d in alldescriptors : <EOL> c = km . predict ( d ) <EOL> features . append ( <EOL> np . array ( [ np . sum ( c == ci ) for ci in range ( k ) ] ) <EOL> ) <EOL> features = np . array ( features , dtype = float ) <EOL> scores = cross_validation . cross_val_score ( <EOL> clf , features , labels , cv = cv ) <EOL> print ( '<STR_LIT>' . format ( scores . mean ( ) ) ) </s>
