Guest User

Untitled

a guest
Jan 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. ###############################
  2. #Read Image
  3. ###############################
  4. image = cv2.imread('Celulas/celulas.jpg')
  5. origin=image
  6. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  7. image = cv2.GaussianBlur(image, (5, 5), 0)
  8.  
  9. sobelX = cv2.Sobel(image, cv2.CV_64F, 1, 0)
  10. sobelY = cv2.Sobel(image, cv2.CV_64F, 0, 1)
  11. sobelX = np.uint8(np.absolute(sobelX))
  12. sobelY = np.uint8(np.absolute(sobelY))
  13. sobelCombined = cv2.bitwise_or(sobelX, sobelY)
  14.  
  15. image = sobelCombined
  16. binary_global = image > threshold_otsu(image)
  17.  
  18. img_bin=binary_global
  19. img_8=bin2uint8(img_bin)
  20. image=img_8
  21.  
  22.  
  23. ###############################
  24. #Circle detection
  25. ###############################
  26. height, width = image.shape
  27.  
  28. circles = cv2.HoughCircles(image,cv2.HOUGH_GRADIENT,.3,20,
  29. param1=100,param2=100,minRadius=int(min(width,height)/3)
  30. maxRadius=int(min(width,height)))
  31.  
  32. circles = np.uint16(np.around(circles))
  33.  
  34. cimg=origin
  35. for i in circles[0,:]:
  36. cv2.circle(cimg,(i[0],i[1]),i[2],(255,0,0),1) #DRAW ALL CIRCLES IN BLUE
  37. cv2.circle(cimg,(i[0],i[1]),2,(255,0,0),1)
  38.  
  39.  
  40. ###############################
  41. #FIND HIGHER CIRCLE
  42. ###############################
  43. max_index=0
  44. max_i=circles[0,max_index,2]
  45. for indx, i in enumerate(circles[0,:]):
  46. print(i)
  47. print("----")
  48. if i[2]>max_i:
  49. max_i=i[2]
  50. max_index=indx #indx of higher circle
  51. print("MAXIMO:", max_i)
  52.  
  53. circle_max=max_i
  54. x_max=circles[0,max_index,0]
  55. y_max=circles[0,max_index,1]
  56. r_max=circles[0,max_index,2]
  57.  
  58.  
  59. cv2.circle(cimg,(x_max,y_max),r_max,(0,0,255),1) #DRAW HIGHER CIRCLE IN RED
  60. cv2.circle(cimg,(x_max,y_max),2,(0,0,255),3)
  61.  
  62. ###############################
  63. #FIND INSIDE CIRCLE
  64. ###############################
  65. encontrado=0
  66. relacion=.9105 #the relationship between the radios is 91.05%
  67. tol=0.01
  68. ponderacion = 0.3
  69. while (1):
  70. r_min_menor = (r_max * relacion) * (1 - tol)
  71. r_min_mayor = (r_max * relacion) * (1 + tol)
  72. x_min_menor = x_max * (1 - tol) * ponderacion
  73. x_min_mayor = x_max * (1 + tol) * ponderacion
  74. y_min_menor = y_max * (1 - tol) * ponderacion
  75. y_min_mayor = y_max * (1 + tol) * ponderacion
  76. for indx, m in enumerate(circles[0,:]):
  77. if indx !=max_index: #para que no compare con el mismo circulo
  78. if m[2]>r_min_menor and m[2]<r_min_mayor and m[0]>x_min_menor
  79. and m[0]<x_min_mayor and m[1]>y_min_menor
  80. and m[1]<y_min_mayor:
  81. circle_min=m
  82. print("Encontro un circulo")
  83. cv2.circle(cimg,(m[0],m[1]),m[2],(0,255,0),1)
  84. cv2.circle(cimg,(m[0],m[1]),2,(0,255,0),3)
  85. cv2.imshow('detected circles',cimg)
  86. cv2.waitKey(0)
  87. if encontrado == 0:
  88. encontrado=1
  89. if encontrado:
  90. break
  91. tol=tol*1.05
  92.  
  93. cimg=origin
  94. cv2.circle(cimg,(x_min,y_min),r_min,(0,255,0),1) #DRAW INSIDE CIRCLE IN GREEN
  95. cv2.circle(cimg,(x_min,y_min),2,(0,255,0),3)
Add Comment
Please, Sign In to add comment