Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. img = cv2.imread('chest.jpg',cv2.CV_LOAD_IMAGE_GRAYSCALE)
  6. a = cv2.equalizeHist(img)
  7. b = 255
  8. arr_b = [i for i in range(b)]
  9. trans_thresh = [float(i)/float(b) for i in arr_b]
  10. result4_trans1 = []
  11.  
  12. def euler_number(image):
  13. contours, hierarchy = cv2.findContours(a,cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
  14. objects = len(contours)
  15. holes = 0
  16. for h in hierarchy[0]:
  17. if h[2] == -1:
  18. holes += 1
  19. eulerNumber = objects - holes
  20. return eulerNumber
  21.  
  22. def convert_to_binary_image(image, threshold, maxVal):
  23. ret, thresh = cv2.threshold(image, threshold, maxVal, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  24. return thresh
  25.  
  26.  
  27. for j in range(b):
  28. value_trans1 = trans_thresh[j]
  29. binary_image = convert_to_binary_image(a, value_trans1, b)
  30. result4_trans1.append(euler_number(binary_image))
  31.  
  32. max_result4_trans1 = max(result4_trans1)
  33. min_result4_trans1 = min(result4_trans1)
  34. m = 0
  35. n = 0
  36. max_sum_result4_trans1 = 0
  37. min_sum_result4_trans1 = 0
  38.  
  39. for j in range(b):
  40. if result4_trans1[j] == max_result4_trans1:
  41. m += 1
  42. max_sum_result4_trans1 += j
  43. elif result4_trans1[j] == min_result4_trans1:
  44. n += 1
  45. min_sum_result4_trans1 += j
  46.  
  47. threshold_I1 = (float(max_sum_result4_trans1)+float(min_sum_result4_trans1))/float(m+n)
  48. c = convert_to_binary_image(a, threshold_I1/float(b+1), b)
  49.  
  50. plt.imshow(c, cmap='gray')
  51. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement