Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import sys
  4. import rospy
  5. import cv2
  6. import numpy as np
  7. import math
  8. from sensor_msgs.msg import CameraInfo
  9. from geometry_msgs.msg import Point
  10. from sensor_msgs.msg import Image
  11. from cv_bridge import CvBridge
  12.  
  13. class image_converter:
  14. def __init__(self):
  15. # initialises the node, publisher, subscribers and variables
  16. self.pub = rospy.Publisher("target", Point, queue_size=1000)
  17. rospy.init_node('target', anonymous=True)
  18. self.bridge = CvBridge()
  19. self.Image_sub = rospy.Subscriber("/camera/rgb/image_color", Image, self.callback)
  20. self.Image_sub2 = rospy.Subscriber("/camera/depth/image_rect", Image, self.depth)
  21. self.cam_info = rospy.Subscriber("/camera/depth/camera_info", CameraInfo, self.fromCameraInfo)
  22. self.targety = 1
  23. self.targetx = 1
  24. self.z = 1
  25. self.P = 1
  26.  
  27. def callback(self, data):
  28. # gets image from /image_color topic, calls contour to get x, y
  29. # and the processed cv_image with a rectangle, shows the
  30. # cv_image with the live x, y and z values displayed on screen
  31. # and publishes x, y and z on the /target topic
  32. self.cv_image = self.bridge.imgmsg_to_cv2(data, 'mono8')
  33. self.x, self.y, self.z, self.cv_image = self.contour(self.cv_image)
  34. font = cv2.FONT_HERSHEY_SIMPLEX
  35. self.str_x = str(self.x)
  36. self.str_y = str(self.y)
  37. self.str_z = str(self.z)
  38. position = "x:{0} y:{1} z:{2}".format(self.str_x, self.str_y, self.str_z)
  39. cv2.putText(self.cv_image, position, (10,450), font, 0.5,(255,255,255),1,cv2.LINE_AA)
  40. cv2.imshow('Image', self.cv_image)
  41. self.pub.publish(self.x, self.y, self.z)
  42. cv2.waitKey(100)
  43.  
  44. def contour(self, cv_image):
  45. # blurs image to remove noise, thresholds image, shows the
  46. # threshold image, finds contours, selects the correct contour
  47. # for the door handle before drawing a rectangle around it and
  48. # returning the x and y position of the centre top of the
  49. # rectangle. Then uses projectPixelTo3dRay to get camera space
  50. # position of the handle referenced to the middle of the image
  51. img = cv_image
  52. img = cv2.GaussianBlur(img,(5,5),0)
  53. thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 7)
  54. cv2.imshow('thresh', thresh)
  55. image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  56. for cnt in contours:
  57. if len(cnt) > 10:
  58. x, y, w, h = cv2.boundingRect(cnt)
  59. if (w < 250) and (h < 100) and (w > 40) and (h > 3) and (y > 200) and (y < 350) and (x > 150) and (x < 300):
  60. good = cnt
  61. x, y, w, h = cv2.boundingRect(good)
  62. cv2.rectangle(cv_image, (x, y), (x + w, y + h), (0, 125, 8), 2)
  63. self.targetx = (x + (w / 2))
  64. self.targety = y
  65. self.x, self.y = self.projectPixelTo3dRay(self.targety, self.targetx)
  66. print (self.x, self.y, self.z)
  67. return self.x, self.y, self.z, self.cv_image
  68.  
  69.  
  70. def depth(self, data):
  71. # returns distance from sensor to target pixel from the depth
  72. # image
  73. #for i in range(1):
  74. depth_image = self.bridge.imgmsg_to_cv2(data, 'passthrough')
  75. np.clip(depth_image, 0, 2, depth_image)
  76. self.z = depth_image[self.targety][self.targetx]
  77. #if self.z < 1:
  78. return self.z
  79.  
  80. def fromCameraInfo(self, data):
  81. # returns matrix P from camera_info topic containing focal
  82. # length and centre of image
  83. self.P = data.P
  84.  
  85. def projectPixelTo3dRay(self, targety, targetx):
  86. # returns x and y in camera space calculated from x and y in
  87. # pixels, centre of image (cx, cy) and focal length (fx, fy)
  88. self.x = (self.targetx - self.cx()) / self.fx()
  89. self.y = (self.targety - self.cy()) / self.fy()
  90. return (self.x, self.y)
  91.  
  92. def cx(self):
  93. # returns centre of image in x
  94. return self.P[2]
  95.  
  96. def cy(self):
  97. # returns centre of image in y
  98. return self.P[6]
  99.  
  100. def fx(self):
  101. # returns focal length in x
  102. return self.P[0]
  103.  
  104. def fy(self):
  105. # returns focal length in y
  106. return self.P[5]
  107.  
  108.  
  109. def main(args):
  110. ic = image_converter()
  111. try:
  112. rospy.spin()
  113. except KeyboardInterrupt:
  114. print("Shutting down")
  115. cv2.destroyAllWindows()
  116.  
  117. if __name__ == '__main__':
  118. main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement