Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import numpy as np
  2. import cv2.cv as cv
  3. import cv2
  4. import numpy as np
  5. font = cv2.FONT_HERSHEY_SIMPLEX
  6. a0=0
  7. a1=218
  8. a2=117
  9. a3=17
  10. a4=255
  11. a5=255
  12. a6=6
  13. a7=49
  14. a8=0
  15. a9=108
  16. a10=5
  17. def updateValue(new_value):
  18. # make sure to write the new value into the global variable
  19. global a0
  20. a0 = new_value
  21. return
  22. def updateValue1(new_value):
  23. # make sure to write the new value into he global variable
  24. global a1
  25. a1 = new_value
  26. return
  27. def updateValue2(new_value):
  28. # make sure to write the new value into the global variable
  29. global a2
  30. a2 = new_value
  31. return
  32. def updateValue3(new_value):
  33. # make sure to write the new value into the global variable
  34. global a3
  35. a3 = new_value
  36. return
  37. def updateValue4(new_value):
  38. # make sure to write the new value into the global variable
  39. global a4
  40. a4 = new_value
  41. return
  42. def updateValue5(new_value):
  43. # make sure to write the new value into the global variable
  44. global a5
  45. a5 = new_value
  46. return
  47. def updateValue6(new_value):
  48. # make sure to write the new value into the global variable
  49. global a6
  50. a6 = new_value
  51. return
  52. def updateValue7(new_value):
  53. # make sure to write the new value into the global variable
  54. global a7
  55. a7 = new_value
  56. return
  57. def updateValue8(new_value):
  58. # make sure to write the new value into the global variable
  59. global a8
  60. a8 = new_value
  61. return
  62. def updateValue9(new_value):
  63. # make sure to write the new value into the global variable
  64. global a9
  65. a9 = new_value
  66. return
  67. def updateValue10(new_value):
  68. # make sure to write the new value into the global variable
  69. global a10
  70. a10 = new_value
  71. return
  72.  
  73. cv2.namedWindow("OutputImage")
  74. cv2.createTrackbar("1", "OutputImage", a0, 255, updateValue)
  75. cv2.createTrackbar("2", "OutputImage", a1, 255, updateValue1)
  76. cv2.createTrackbar("3", "OutputImage", a2, 255, updateValue2)
  77. cv2.createTrackbar("4", "OutputImage", a3, 255, updateValue3)
  78. cv2.createTrackbar("5", "OutputImage", a4, 255, updateValue4)
  79. cv2.createTrackbar("6", "OutputImage", a5, 255, updateValue5)
  80. cv2.createTrackbar("param1", "OutputImage", a6, 255, updateValue6)
  81. cv2.createTrackbar("param2", "OutputImage", a7, 255, updateValue7)
  82. cv2.createTrackbar("minRadius", "OutputImage", a8, 255, updateValue8)
  83. cv2.createTrackbar("maxRadius", "OutputImage", a9, 255, updateValue9)
  84. cv2.createTrackbar("maxDist", "OutputImage", a10, 255, updateValue10)
  85. # open the camera
  86. cap = cv2.VideoCapture(0)
  87.  
  88. while True:
  89. #read the image from the camera
  90. ret, frame = cap.read()
  91.  
  92. #You will need this later
  93. #frame = cv2.GaussianBlur(frame,(5,5),0)
  94. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  95.  
  96. # color detection limits
  97. lowerLimits = np.array([a0, a1, a2])
  98. upperLimits = np.array([a3, a4, a5])
  99.  
  100. # Our operations on the frame come here
  101. thresholded = cv2.inRange(frame, lowerLimits, upperLimits)
  102. outimage = cv2.bitwise_and(frame, frame, mask = thresholded)
  103.  
  104. circles = cv2.HoughCircles(thresholded,cv.CV_HOUGH_GRADIENT,2,a10,
  105. param1=a6+1,param2=a7+1,minRadius=a8,maxRadius=a9)
  106. if circles != None:
  107. for i in circles[0,:]:
  108. cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2)
  109. cv2.putText(frame,"x: " + str(i[0])+ " y:" + str(i[1]),(i[0],i[1]), font, 0.5,(255,255,255),2)
  110. cv2.imshow('original', frame)
  111.  
  112. # Display the resulting frame
  113. cv2.imshow('processed',outimage)
  114.  
  115. # Quit the program when Q is pressed
  116. if cv2.waitKey(1) & 0xFF == ord('q'):
  117. break
  118.  
  119. # When everything done, release the capture
  120. print 'closing program'
  121. cap.release()
  122. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement