Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def choose_match_np(lab, img_db, LAB_db, usage_ary, tolerance=1, usage_penalty=1):
- '''
- Uses a numpy data structure to perform matching instead of very slow calls to SQLite. Because the number of images
- in the Images table is the same as the number of rows in the LabColors table, the association is maintained
- via the array indexes.
- :param lab:
- :param img_db: a Python list. Each row is a row from the sqlite Images database
- :param LAB_db: a numpy array of the LAB colors associated with an image.
- :param tolerance:
- :param usage_penalty:
- :return:
- '''
- JND = 2.3 # "just noticeable difference"
- tol = (JND * tolerance)**2
- lab = np.asarray(lab,dtype=np.float32)
- search_for = lab.flatten()
- num_imgs = len(LAB_db)
- rand_noise = np.random.uniform(low=-1.0,high=1.0, size=(num_imgs,1)) * tol
- use_pen = (usage_ary * usage_penalty) ** 2
- # distance value does not need square root in this case as we're only looking for minimum
- # value, not actual Euclidean Distance.
- dist = np.sum((LAB_db[:,3:] - search_for)**2,axis=1)[:,None] + rand_noise + use_pen
- # find the index corresponding to the minimum distance. This is far cheaper than a sort. Closer
- # to O(n) than O(n log n)
- min_idx = np.unravel_index(dist.argmin(),dist.shape)[0]
- # calculate dL
- L1 = LAB_db[min_idx][3]
- L2 = LAB_db[min_idx][6]
- L3 = LAB_db[min_idx][9]
- L4 = LAB_db[min_idx][12]
- nL1 =search_for[0]
- nL2 =search_for[3]
- nL3 =search_for[6]
- nL4 =search_for[9]
- dL = ((L1-nL1)+(L2-nL2)+(L3-nL3)+(L4-nL4))/4.0
- match = {'filename':img_db[min_idx][4], 'dL':dL,'image_id':min_idx}
- # increment the usuage index.
- usage_ary[min_idx] += 1
- return min_idx, match
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement