Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. '''
  2. Cell counting.
  3.  
  4. '''
  5.  
  6. import cv2
  7. import cv2.cv
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10. import math
  11.  
  12.  
  13. def detect(img):
  14. '''
  15. Do the detection.
  16. '''
  17. # create a gray scale version of the image, with as type an unsigned 8bit integer
  18. img_g = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8)
  19. img_g[:, :] = img[:, :, 0]
  20. cv2.imshow('img', img_g)
  21. cv2.waitKey(0)
  22.  
  23. # 1. Do canny (determine the right parameters) on the gray scale image
  24. edges = cv2.Canny(img_g,50,110) # TODO!
  25.  
  26. # Show the results of canny
  27. canny_result = np.copy(img_g)
  28. canny_result[edges.astype(np.bool)] = 0
  29. cv2.imshow('img', canny_result)
  30. cv2.waitKey(0)
  31.  
  32. # 2. Do hough transform on the gray scale image
  33. print "circles"
  34. circles = cv2.HoughCircles(img_g, cv2.cv.CV_HOUGH_GRADIENT,1,100, param1=80,param2=35,minRadius=0,maxRadius=0)
  35. print "done"
  36. circles = circles[0, :, :]
  37.  
  38. # Show hough transform result
  39. print "showing"
  40. showCircles(img, circles)
  41.  
  42. # 3.a Get a feature vector (the average color) for each circle
  43. nbCircles = circles.shape[0]
  44. features = np.zeros((nbCircles, 3), dtype=np.int)
  45. for i in range(nbCircles):
  46. features[i, :] = getAverageColorInCircle(None, int(circles[i, 0]), int(circles[i, 1]),
  47. int(circles[i, 2])) # TODO!
  48.  
  49. # 3.b Show the image with the features (just to provide some help with selecting the parameters)
  50. showCircles(img, circles, [str(features[i, :]) for i in range(nbCircles)])
  51.  
  52. # 3.c Remove circles based on the features
  53. selectedCircles = np.zeros((nbCircles), np.bool)
  54. for i in range(nbCircles):
  55. if True: # TODO
  56. selectedCircles[i] = 1
  57. circles = circles[selectedCircles]
  58.  
  59. # Show final result
  60. showCircles(img, circles)
  61. return circles
  62.  
  63.  
  64. def getAverageColorInCircle(img, cx, cy, radius):
  65. '''
  66. Get the average color of img inside the circle located at (cx,cy) with radius.
  67. '''
  68. maxy, maxx, channels = img.shape
  69. nbVoxels = 0
  70. C = np.zeros((3))
  71. # TODO!
  72. return C
  73.  
  74.  
  75. def showCircles(img, circles, text=None):
  76. '''
  77. Show circles on an image.
  78. @param img: numpy array
  79. @param circles: numpy array
  80. shape = (nb_circles, 3)
  81. contains for each circle: center_x, center_y, radius
  82. @param text: optional parameter, list of strings to be plotted in the circles
  83. '''
  84. # make a copy of img
  85. img = np.copy(img)
  86. # draw the circles
  87. nbCircles = circles.shape[0]
  88. for i in range(nbCircles):
  89. cv2.circle(img, (int(circles[i, 0]), int(circles[i, 1])), int(circles[i, 2]), cv2.cv.CV_RGB(255, 0, 0), 2, 8, 0)
  90. # draw text
  91. print "drawing"
  92. if text != None:
  93. for i in range(nbCircles):
  94. cv2.putText(img, text[i], (int(circles[i, 0]), int(circles[i, 1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
  95. cv2.cv.CV_RGB(0, 0, 255))
  96. # show the result
  97. print "imshow"
  98. cv2.imshow('img', img)
  99. cv2.waitKey(0)
  100.  
  101.  
  102. if __name__ == '__main__':
  103. # read an image
  104. img = cv2.imread('normal.jpg')
  105.  
  106. # print the dimension of the image
  107. print img.shape
  108.  
  109. # show the image
  110. cv2.imshow('img', img)
  111. cv2.waitKey(0)
  112.  
  113. # do detection
  114. circles = detect(img)
  115.  
  116. # print result
  117. print "We counted " + str(circles.shape[0]) + " cells."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement