Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4. import cv2
  5. import numpy
  6. from sensor_msgs.msg import Image
  7. from std_msgs.msg import String
  8. from cv_bridge import CvBridge, CvBridgeError
  9.  
  10.  
  11. class image_converter:
  12.  
  13. def __init__(self):
  14.  
  15. cv2.startWindowThread()
  16. self.bridge = CvBridge()
  17. self.image_sub = rospy.Subscriber("/camera/rgb/image_raw",
  18. Image, self.callback)
  19. self.image_pub = rospy.Publisher('chatter', String, queue_size = 1)
  20. # self.image_sub = rospy.Subscriber("/camera/rgb/image_raw",
  21. # Image, self.callback)
  22.  
  23. def callback(self, data):
  24. cv2.namedWindow("Image window", 1)
  25. try:
  26. cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
  27. except CvBridgeError, e:
  28. print e
  29.  
  30. bgr_thresh = cv2.inRange(cv_image,
  31. numpy.array((17,29,99)),
  32. numpy.array((81,118,204)))
  33.  
  34. hsv_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
  35.  
  36. hsv_thresh = cv2.inRange(hsv_img,
  37. numpy.array((48,67,134)),
  38. numpy.array((102, 136,228)))
  39.  
  40. print numpy.mean(hsv_img[:, :, 0])
  41. print numpy.mean(hsv_img[:, :, 1])
  42. print numpy.mean(hsv_img[:, :, 2])
  43.  
  44. _, bgr_contours, hierachy = cv2.findContours(
  45. bgr_thresh.copy(),
  46. cv2.RETR_TREE,
  47. cv2.CHAIN_APPROX_SIMPLE)
  48.  
  49. _, hsv_contours, hierachy = cv2.findContours(
  50. hsv_thresh.copy(),
  51. cv2.RETR_TREE,
  52. cv2.CHAIN_APPROX_SIMPLE)
  53. for c in bgr_contours:
  54. a = cv2.contourArea(c)
  55. if a > 100.0:
  56. cv2.drawContours(cv_image, c, -1, (255, 0, 0))
  57. print '===='
  58.  
  59. self.image_pub.publish(str(numpy.mean(cv_image)))
  60.  
  61. cv2.imshow("Image window", cv_image)
  62. cv2.waitKey(1)
  63.  
  64.  
  65. image_converter()
  66. rospy.init_node('image_converter', anonymous=True)
  67. rospy.spin()
  68. cv2.destroyAllWindows()
  69.  
  70. #this is the colour.py file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement