Advertisement
Guest User

Untitled

a guest
May 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. import code
  2. import numpy as np
  3. import math
  4. from skimage.transform import ProjectiveTransform
  5. from sklearn.metrics import roc_auc_score
  6. from skimage import io
  7. import scipy
  8. import matplotlib.pyplot as plt
  9. import random
  10. from random import randint
  11. import scipy.ndimage.filters as fi
  12. import scipy.stats as st
  13.  
  14. BINS = 15
  15. SIGMA = 1.5
  16.  
  17. def gkern(kernlen=21, nsig=2):
  18. interval = (2*nsig+1.)/(kernlen)
  19. x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
  20. kern1d = np.diff(st.norm.cdf(x))
  21. kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
  22. kernel = kernel_raw/kernel_raw.sum()
  23. return kernel
  24.  
  25. def extract(image, keypoints):
  26. image = image / np.max(image)
  27. image = image * 255
  28. image = image.astype(np.uint8)
  29. tab = []
  30. imgs = []
  31. angles = []
  32. for point in keypoints:
  33. img = get_img_around(image, point)
  34. tab.append(descriptor(img))
  35. imgs.append(img)
  36. return tab
  37.  
  38. def get_img_around(image, point):
  39. x, y = point
  40. img = image[x-31:x+32, y-31:y+32]
  41. return img
  42.  
  43. def split_image(image):
  44. imgs = []
  45. for i in range(4, 60, 8):
  46. for j in range(4, 60, 8):
  47. imgs.append(image[i:i+8, j:j+8])
  48. return imgs
  49.  
  50. def normalize(img):
  51. img = img - np.min(img)
  52. img = (img / float(np.max(img))) * 255
  53. return img
  54.  
  55. def descriptor(img):
  56. desc = []
  57. img = fi.gaussian_filter(img, sigma=SIGMA)
  58. img = normalize(img)
  59. for i in split_image(img):
  60. desc.append(small_descriptor(i))
  61. desc = np.array(desc)
  62. return desc.flatten()
  63.  
  64. def small_descriptor(image):
  65. image = image.flatten()
  66. return np.histogram(image, bins = BINS, range = (0, 256))[0]
  67.  
  68. def distance(d1, d2):
  69. return np.sum(np.absolute(d1 - d2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement