<s> from optparse import OptionParser <EOL> from scipy . spatial import distance as dist <EOL> import matplotlib . pyplot as plt <EOL> import numpy as np <EOL> import argparse <EOL> import glob <EOL> import cv2 <EOL> import sys <EOL> import pickle <EOL> def image_match_histogram ( all_files , options ) : <EOL> histograms = { } <EOL> image_files = [ ] <EOL> for ( i , fname ) in enumerate ( all_files ) : <EOL> if options . ipath : <EOL> path_fname = options . ipath + '<STR_LIT:/>' + fname <EOL> else : <EOL> path_fname = fname <EOL> image = cv2 . imread ( path_fname ) ; <EOL> if image is None : <EOL> print path_fname + "<STR_LIT>" <EOL> continue <EOL> image_files . append ( fname ) <EOL> if image . shape [ <NUM_LIT:2> ] == <NUM_LIT:1> : <EOL> image = cv2 . cvtColor ( image , cv2 . COLOR_GRAY2RGB ) <EOL> print i , path_fname , image . shape <EOL> v = cv2 . calcHist ( [ image ] , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] , None , [ <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:8> ] , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> ] ) <EOL> v = v . flatten ( ) <EOL> hist = v / sum ( v ) <EOL> histograms [ fname ] = hist <EOL> pickle . dump ( histograms , open ( options . opath + "<STR_LIT>" , "<STR_LIT:wb>" ) ) <EOL> feature_matrix = np . zeros ( ( len ( histograms ) , len ( hist ) ) ) <EOL> for ( i , fi ) in enumerate ( image_files ) : <EOL> feature_matrix [ i , : ] = histograms [ image_files [ i ] ] <EOL> pickle . dump ( feature_matrix , open ( options . opath + "<STR_LIT>" , "<STR_LIT:wb>" ) ) <EOL> dists = np . zeros ( ( len ( image_files ) , len ( image_files ) ) ) <EOL> knn = { } <EOL> for ( i , fi ) in enumerate ( image_files ) : <EOL> for ( j , fj ) in enumerate ( image_files ) : <EOL> if i <= j : <EOL> d = cv2 . compareHist ( histograms [ fi ] , histograms [ fj ] , cv2 . cv . CV_COMP_INTERSECT ) <EOL> dists [ i , j ] = d <EOL> dists [ j , i ] = d <EOL> pickle . dump ( dists , open ( options . opath + "<STR_LIT>" , "<STR_LIT:wb>" ) ) <EOL> k = int ( options . top ) <EOL> print '<STR_LIT>' <EOL> for ( i , fi ) in enumerate ( image_files ) : <EOL> vec = sorted ( zip ( dists [ i , : ] , image_files ) , reverse = True ) <EOL> knn [ fi ] = vec [ : k ] <EOL> print knn [ fi ] <EOL> pickle . dump ( knn , open ( options . opath + "<STR_LIT>" , "<STR_LIT:wb>" ) ) <EOL> term_crit = ( cv2 . TERM_CRITERIA_EPS , <NUM_LIT:100> , <NUM_LIT> ) <EOL> print feature_matrix <EOL> ret , labels , centers = cv2 . kmeans ( np . float32 ( feature_matrix ) , int ( options . cluster_count ) , term_crit , <NUM_LIT:10> , cv2 . KMEANS_RANDOM_CENTERS ) <EOL> label_list = [ ] <EOL> for ( i , l ) in enumerate ( labels ) : <EOL> label_list . append ( l [ <NUM_LIT:0> ] ) <EOL> print label_list <EOL> image_label = zip ( image_files , label_list ) <EOL> print image_label <EOL> pickle . dump ( image_label , open ( options . opath + "<STR_LIT>" , "<STR_LIT:wb>" ) ) <EOL> def main ( ) : <EOL> usage = "<STR_LIT>" <EOL> usage += "<STR_LIT>" <EOL> parser = OptionParser ( usage = usage ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT:.>" , <EOL> action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT:5>" , <EOL> action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , default = "<STR_LIT:3>" , <EOL> action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT:0>" , <EOL> action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> if len ( args ) < <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> image_files = [ line . strip ( ) for line in open ( args [ <NUM_LIT:0> ] ) ] <EOL> if options . feature == "<STR_LIT>" : <EOL> image_match_histogram ( image_files , options ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
