Advertisement
Guest User

photomosaic search

a guest
Nov 27th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. def choose_match_np(lab, img_db, LAB_db, usage_ary, tolerance=1, usage_penalty=1):
  2.     '''
  3.    Uses a numpy data structure to perform matching instead of very slow calls to SQLite. Because the number of images
  4.    in the Images table is the same as the number of rows in the LabColors table, the association is maintained
  5.    via the array indexes.
  6.    :param lab:
  7.    :param img_db: a Python list. Each row is a  row from the sqlite Images database
  8.    :param LAB_db: a numpy array of the LAB colors associated with an image.
  9.    :param tolerance:
  10.    :param usage_penalty:
  11.    :return:
  12.    '''
  13.     JND = 2.3 # "just noticeable difference"
  14.     tol = (JND * tolerance)**2
  15.  
  16.     lab         = np.asarray(lab,dtype=np.float32)
  17.     search_for  = lab.flatten()
  18.  
  19.     num_imgs = len(LAB_db)
  20.  
  21.     rand_noise = np.random.uniform(low=-1.0,high=1.0, size=(num_imgs,1)) * tol
  22.     use_pen    = (usage_ary * usage_penalty) ** 2
  23.  
  24.     # distance value does not need square root in this case as we're only looking for minimum
  25.     # value, not actual Euclidean Distance.
  26.     dist = np.sum((LAB_db[:,3:] - search_for)**2,axis=1)[:,None] + rand_noise + use_pen
  27.  
  28.     # find the index corresponding to the minimum distance. This is far cheaper than a sort. Closer
  29.     # to O(n) than O(n log n)
  30.     min_idx = np.unravel_index(dist.argmin(),dist.shape)[0]
  31.  
  32.     # calculate dL
  33.     L1 = LAB_db[min_idx][3]
  34.     L2 = LAB_db[min_idx][6]
  35.     L3 = LAB_db[min_idx][9]
  36.     L4 = LAB_db[min_idx][12]
  37.     nL1 =search_for[0]
  38.     nL2 =search_for[3]
  39.     nL3 =search_for[6]
  40.     nL4 =search_for[9]
  41.  
  42.     dL = ((L1-nL1)+(L2-nL2)+(L3-nL3)+(L4-nL4))/4.0
  43.     match =  {'filename':img_db[min_idx][4], 'dL':dL,'image_id':min_idx}
  44.     # increment the usuage index.
  45.     usage_ary[min_idx] += 1
  46.     return min_idx, match
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement