PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Alex/retrieve_features.py

https://gitlab.com/genehack-2/15a.ozerov.team_untitled
Python | 288 lines | 260 code | 19 blank | 9 comment | 2 complexity | d3b592e5174870cae46de6e5a8df4888 MD5 | raw file
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. import pandas as pd
  5. import seaborn as sns
  6. import sys
  7. HOME_DIR = '/Users/agalicina/Term10/genehack/' ##change for your machine
  8. sys.path.append(HOME_DIR+'genehack2016')
  9. from process import *
  10. from sklearn.metrics import roc_curve, auc
  11. import math
  12. def read_img(path):
  13. return cv2.imread(path, 0)
  14. ##########################
  15. # GAUSSIAN ASSESSMENT
  16. ##########################
  17. def get_spectrum(img):
  18. f = np.fft.fft2(img)
  19. fshift = np.fft.fftshift(f)
  20. magnitude_spectrum = fshift
  21. return magnitude_spectrum
  22. def plot_doub(num1, num2, img, spec):
  23. plt.subplot(num1),plt.imshow(img, cmap = 'gray')
  24. plt.title('Input Image'), plt.xticks([]), plt.yticks([])
  25. plt.subplot(num2),plt.imshow(spec, cmap = 'gray')
  26. plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
  27. def plot(num1,spec):
  28. plt.subplot(num1),plt.imshow(spec, cmap = 'gray')
  29. plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
  30. def compute_sum_gaussian(img_mask, img_pic, sigma=10, size=150, mode='abs'):
  31. #img_mask = cv2.resize(cv2.imread(img_path_mask,0), (150,150))
  32. #img_pic = cv2.resize(cv2.imread(img_path_pic, 0), (150,150))
  33. img_mask = cv2.resize(img_mask, (150,150))
  34. img_pic = cv2.resize(img_pic, (150,150))
  35. spec_mask = get_spectrum(img_mask)
  36. spec_pic = get_spectrum(img_pic)
  37. x = cv2.getGaussianKernel(size,sigma)
  38. gaussian = x*x.T
  39. if mode=='abs':
  40. res = gaussian*(np.abs(spec_pic-spec_mask))
  41. else:
  42. res = gaussian*(spec_pic)
  43. return np.sum(res)
  44. def plot_gaussian_steps():
  45. #### TO DO: check and rewrite
  46. img_mask = cv2.resize(cv2.imread(img_path_mask,0), (150,150))
  47. img_pic = cv2.resize(cv2.imread(img_path_pic, 0), (150,150))
  48. spec_mask = get_spectrum(img_mask)
  49. spec_pic = get_spectrum(img_pic)
  50. pl1 = 20*np.log(np.abs(spec_mask))
  51. pl2 = 20*np.log(np.abs(spec_pic))
  52. pl3 = 20*np.log(np.abs(spec_pic-spec_mask))
  53. plt.figure(figsize=(10,10))
  54. plot_doub(321,322,img_mask, pl1)
  55. plot_doub(323,324,img_pic, pl2)
  56. plot(326, pl3)
  57. plt.show()
  58. ##########################
  59. # CONTOUR ASSESSMENT
  60. ##########################
  61. def find_contour(img):
  62. ret, thresh = cv2.threshold(img,127,255,0)
  63. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
  64. return contours[0]
  65. def get_perimeter(cnt):
  66. perimeter = cv2.arcLength(cnt,True)
  67. return perimeter
  68. def plot_contour(img, cnt, path):
  69. img1 = img.copy()
  70. cv2.drawContours(img1,[cnt],0,(0,0,255),2)
  71. cv2.imwrite(path, img1)
  72. '''cv2.namedWindow("imc")
  73. cv2.imshow("imc", img1)
  74. cv2.waitKey(0)'''
  75. def get_defects(cnt):
  76. hull = cv2.convexHull(cnt,returnPoints = False)
  77. defects = cv2.convexityDefects(cnt,hull)
  78. return defects
  79. def plot_and_count_defects(img, defects, cnt, depth_tresh):
  80. img1 = img.copy()
  81. true_def_count = 0
  82. max_def = 0
  83. n=0
  84. for i in range(defects.shape[0]):
  85. n+=1
  86. s,e,f,d = defects[i,0]
  87. start = tuple(cnt[s][0])
  88. end = tuple(cnt[e][0])
  89. far = tuple(cnt[f][0])
  90. med = (min(start[0],end[0])+(abs(start[0]-end[0])/2), min(start[1],end[1])+(abs(start[1]-end[1])/2))
  91. depth = math.sqrt(((med[0]-far[0])**2)+((med[1]-far[1])**2))
  92. if n != 1:
  93. max_def = max(max_def, depth)
  94. if depth > depth_tresh:
  95. true_def_count += 1
  96. cv2.circle(img1,far,2,[0,0,255],-1) #red!
  97. else:
  98. cv2.circle(img1,far,2,[255,0,0],-1) #blue!
  99. cv2.line(img1,start,end,[0,255,0],1)
  100. '''cv2.imshow('img',img1)
  101. cv2.waitKey(0)
  102. cv2.destroyAllWindows()'''
  103. #cv2.imwrite(path, img1)
  104. return true_def_count, max_def
  105. def solidity(cnt, area):
  106. hull = cv2.convexHull(cnt)
  107. hull_area = cv2.contourArea(hull)
  108. return float(area)/hull_area
  109. def ellipse_jaccard(cnt, imgray):
  110. #doesn't work
  111. ellipse = cv2.fitEllipse(cnt)
  112. blank = imgray.copy()
  113. blank[:] = 0
  114. e_img = cv2.ellipse(blank, ellipse, 255, -1)
  115. union_img = np.logical_or(imgray, e_img)
  116. inter_img = np.logical_and(imgray, e_img)
  117. union = sum([sum(x) for x in union_img])
  118. inter = sum([sum(x) for x in inter_img])
  119. return inter/float(union)
  120. def calculate_all(path, depth_tresh):
  121. img = cv2.imread(path)
  122. imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  123. cnt = find_contour(imgray)
  124. area = cv2.contourArea(cnt)
  125. defects = get_defects(cnt)
  126. true_def_count, max_def = plot_and_count_defects(imgray, defects, cnt, depth_tresh)
  127. s = solidity(cnt, area)
  128. #jac = ellipse_jaccard(cnt, imgray)
  129. return true_def_count, max_def, s, 0
  130. #########################
  131. # Hough transform
  132. #########################
  133. def get_Hough_lines_lens(img, par_len=3, par_gap=0.5):
  134. edges = cv2.Canny(img,0,0)
  135. P = get_perimeter(find_contour(img))
  136. minLineLength = par_len*P
  137. maxLineGap = par_gap*P
  138. lines = cv2.HoughLinesP(edges, 1, np.pi/90, 10, 100, minLineLength=minLineLength, maxLineGap=maxLineGap)
  139. len_sum = 0
  140. if lines is None:
  141. return 0
  142. for x1,y1,x2,y2 in lines[0]:
  143. len_sum += math.sqrt(((x1-x2)**2)+((y1-y2)**2))
  144. return len_sum/float(P)
  145. def plot_Hough(img, saveto, par_len=3, par_gap=0.5):
  146. edges = cv2.Canny(img,0,0)
  147. P = get_perimeter(find_contour(img))
  148. minLineLength = par_len*P
  149. maxLineGap = par_gap*P
  150. img1 = img.copy()
  151. img1.fill(1)
  152. len_sum = 0
  153. lines = cv2.HoughLinesP(edges, 1, np.pi/90, 10, 100, minLineLength=minLineLength, maxLineGap=maxLineGap)
  154. if lines is None:
  155. return 0
  156. for x1,y1,x2,y2 in lines[0]:
  157. cv2.line(img1,(x1,y1),(x2,y2),(0,255,0),1)
  158. len_sum += math.sqrt(((x1-x2)**2)+((y1-y2)**2))
  159. plt.clf()
  160. plt.figure(figsize=(10,10))
  161. plt.subplot(121)
  162. plt.imshow(img1)
  163. plt.title(
  164. 'Hough transformation with:\n minLen = {} ({} of P), \n maxGap = {} ({} of P), \n len_sum = {}, norm = {}'.format(
  165. minLineLength, par_len, maxLineGap, par_gap, len_sum, len_sum/float(P))),\
  166. plt.xticks([]), plt.yticks([])
  167. plt.subplot(122)
  168. plt.imshow(img, cmap = 'gray')
  169. plt.title('Input Image'), plt.xticks([]), plt.yticks([])
  170. plt.savefig(saveto)
  171. #########################
  172. # Intensity measurements
  173. #########################
  174. def get_intensity_max(img):
  175. return np.max(img)
  176. def get_intensity_sum(img):
  177. return np.sum(img)
  178. def get_intensity_mean(img):
  179. cnt = find_contour_my(img)
  180. return np.sum(img)/get_area(cnt)
  181. #########################
  182. # Apply to images and compute features FINALLY!
  183. #########################
  184. ### Load images from file, apply function and return array with values
  185. #img_dir = HOME_DIR+'processed_images'
  186. def my_apply(func, img_dir = 'processed_images/', all_imgs=None, y_test=None, **kwargs_dict):
  187. if all_imgs is None:
  188. all_imgs, y_test = load_data(img_dir)
  189. y_score = [func(all_imgs[i], **kwargs_dict) for i in range(len(y_test))]
  190. return [np.array(l) for l in (all_imgs, y_score, y_test)] # (imgs, ys_score, ys_test)
  191. def my_apply_tolist(func, all_imgs=None, y_test=None, **kwargs_dict):
  192. y_score = [func(all_imgs[i], **kwargs_dict) for i in range(len(y_test))]
  193. return [np.array(l) for l in (all_imgs, y_score, y_test)] # (imgs, ys_score, ys_test)
  194. def plot_hist(y_test, y_score, saveto, **kwargs):
  195. '''
  196. :param x: all values after you applied your function
  197. :param y: all categories list. must be 0 or 1
  198. :return: histogram saved into file
  199. Example usage:
  200. rf.plot_hist([1,2,3,4,5,6], [1,0,1,0,1,0], 'ex.png', bins=1, hist_kws={"alpha":0.5})
  201. '''
  202. #df = pd.DataFrame({'x':x, 'y':y})
  203. #sns_plot =
  204. plt.clf()
  205. plt.close()
  206. plt.figure()
  207. sns.set(style="white", palette="muted", color_codes=True)
  208. sns_plot = sns.distplot(np.array(y_score)[(np.array(y_test)==1)], color='green', **kwargs)
  209. sns.distplot(np.array(y_score)[np.array(y_test)==0], color='red', **kwargs)
  210. plt.savefig(saveto)
  211. def plot_roc(y_test, y_score, saveto):
  212. # Compute ROC curve and ROC area for each class
  213. fpr = dict()
  214. tpr = dict()
  215. roc_auc = dict()
  216. fpr, tpr, _ = roc_curve(y_test, y_score)
  217. roc_auc = auc(fpr, tpr)
  218. ##############################################################################
  219. # Plot of a ROC curve for a specific class
  220. plt.figure()
  221. plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
  222. plt.plot([0, 1], [0, 1], 'k--')
  223. plt.xlim([0.0, 1.0])
  224. plt.ylim([0.0, 1.05])
  225. plt.xlabel('False Positive Rate')
  226. plt.ylabel('True Positive Rate')
  227. plt.title('ROC')
  228. plt.legend(loc="lower right")
  229. plt.savefig(saveto)
  230. return roc_auc