anandvgeorge

image_conv.py

May 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import roslib
  4. roslib.load_manifest('camera')
  5. import sys
  6. import rospy
  7. import cv2
  8. from std_msgs.msg import Int16
  9. from sensor_msgs.msg import Image
  10. from cv_bridge import CvBridge, CvBridgeError
  11. import numpy as np
  12. import cv2.cv as cv
  13. from camera.msg import ball
  14.  
  15. kernel = np.ones((5,5),np.uint8)
  16.  
  17. STATE = 1 #PICK -- initial state
  18.  
  19. bridge = CvBridge()
  20.  
  21. CB_COUNT = 0
  22. RUN_COUNT = 0
  23.  
  24. # Initial values(of ball)
  25. hmn = 21 #3
  26. hmx = 37 #28
  27. smn = 118 #77
  28. smx = 255
  29. vmn = 107 #186
  30. vmx = 255
  31.  
  32. def nothing(x):
  33.     pass
  34.  
  35. def state(data):
  36.   if data == 1:
  37.     print ("Find ball")# red
  38.     hmn = 3 #39
  39.     hmx = 28 #80
  40.     smn = 77 #77
  41.     smx = 255
  42.     vmn = 186 #186
  43.     vmx = 255
  44.   elif data == 0:
  45.     print ("Find target") #green
  46.     hmn = 39 #3
  47.     hmx = 80 #28
  48.     smn = 77 #80
  49.     smx = 255
  50.     vmn = 186 #154
  51.     vmx = 255
  52.   else:
  53.     pass
  54.  
  55. def run():
  56.   global cv_image
  57.   pub = rospy.Publisher('ball_pos', ball, queue_size=1)
  58.   msg = ball()
  59.   msg.x = 50
  60.   msg.y = 50
  61.   msg.radius = 50
  62.  
  63.   # (rows,cols,channels) = cv_image.shape
  64.   # if cols > 60 and rows > 60 :
  65.   #   cv2.circle(cv_image, (50,50), 10, 255)
  66.  
  67.   ######################################################################
  68.   #converting to HSV
  69.   hsv = cv2.cvtColor(cv_image,cv2.COLOR_BGR2HSV)
  70.   hue,sat,val = cv2.split(hsv)
  71.  
  72.   # get info from track bar and appy to result
  73.   # hmn = cv2.getTrackbarPos('hmin','HueComp')
  74.   # hmx = cv2.getTrackbarPos('hmax','HueComp')
  75.  
  76.   # smn = cv2.getTrackbarPos('smin','SatComp')
  77.   # smx = cv2.getTrackbarPos('smax','SatComp')
  78.  
  79.   # vmn = cv2.getTrackbarPos('vmin','ValComp')
  80.   # vmx = cv2.getTrackbarPos('vmax','ValComp')
  81.  
  82.   # Apply thresholding
  83.   hthresh = cv2.inRange(np.array(hue),np.array(hmn),np.array(hmx))
  84.   sthresh = cv2.inRange(np.array(sat),np.array(smn),np.array(smx))
  85.   vthresh = cv2.inRange(np.array(val),np.array(vmn),np.array(vmx))
  86.  
  87.   # AND h s and v
  88.   tracking = cv2.bitwise_and(hthresh,cv2.bitwise_and(sthresh,vthresh))
  89.  
  90.   # Some morpholigical filtering
  91.   dilation = cv2.dilate(tracking,kernel,iterations = 1)
  92.   closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
  93.   closing = cv2.GaussianBlur(closing,(5,5),0)
  94.  
  95.   # Detect circles using HoughCircles
  96.   circles = cv2.HoughCircles(closing,cv.CV_HOUGH_GRADIENT,2,120,param1=120,param2=50,minRadius=10,maxRadius=0)
  97.   # circles = np.uint16(np.around(circles))
  98.  
  99.   #Draw Circles
  100.   if circles is not None:
  101.     for i in circles[0,:]:
  102.       # If the ball is far, draw it in green
  103.       if int(round(i[2])) < 30:
  104.           cv2.circle(cv_image,(int(round(i[0])),int(round(i[1]))),int(round(i[2])),(0,255,0),5) # image, center, radius, colour, thickness
  105.           cv2.circle(cv_image,(int(round(i[0])),int(round(i[1]))),2,(0,255,0),10)
  106.       # else draw it in red
  107.       elif int(round(i[2])) > 35:
  108.           cv2.circle(cv_image,(int(round(i[0])),int(round(i[1]))),int(round(i[2])),(0,0,255),5)
  109.           cv2.circle(cv_image,(int(round(i[0])),int(round(i[1]))),2,(0,0,255),10)
  110.           # buzz = 1
  111.       print ("center: %d, %d" % (int(round(i[0])), int(round(i[1]))))
  112.  
  113.       # publish msg
  114.       msg.x = int(round(i[0]))
  115.       msg.y = int(round(i[1]) )
  116.       msg.radius = int(round(i[2]))
  117.       pub.publish(msg)
  118.   global RUN_COUNT
  119.   RUN_COUNT += 1
  120.   #print("run=%d" %RUN_COUNT)
  121.   #you can use the 'buzz' variable as a trigger to switch some GPIO lines on Rpi :)
  122.   # print buzz                    
  123.   # if buzz:
  124.       # put your GPIO line here
  125.  
  126.  
  127.   #Show the result in frames
  128.   # cv2.imshow('HueComp',hthresh)
  129.   # cv2.imshow('SatComp',sthresh)
  130.   # cv2.imshow('ValComp',vthresh)
  131.   # cv2.imshow('closing',closing)
  132.   # cv2.imshow('tracking',cv_image)
  133.  
  134.   #######################################################################
  135.   # cv2.imshow("Image window:py", cv_image)
  136.   # cv2.waitKey(3)
  137.  
  138.   # try:
  139.   #   self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))
  140.   # except CvBridgeError as e:
  141.   #   print(e)
  142.  
  143. def callback(data):
  144.   print ("callback OKAY!")
  145.   global bridge
  146.   global cv_image
  147.   try:
  148.     cv_image = bridge.imgmsg_to_cv2(data, "bgr8")
  149.   except CvBridgeError as e:
  150.     print(e)
  151.   global CB_COUNT
  152.   CB_COUNT += 1
  153.   #print("CB=%d" %CB_COUNT)
  154.   run()
  155.  
  156. def image_process():
  157.   # bridge = CvBridge()
  158.   print ("1")
  159.   image_sub = rospy.Subscriber("/usb_cam/image_raw", Image, callback)
  160.   state_sub = rospy.Subscriber("/robot_state", Int16 , state)
  161.   print("\nImage_converter started")
  162.  
  163.   ###################################################################
  164.   # cv2.namedWindow('HueComp')
  165.   # cv2.namedWindow('SatComp')
  166.   # cv2.namedWindow('ValComp')
  167.   # cv2.namedWindow('closing')
  168.   # cv2.namedWindow('tracking')
  169.  
  170.   # Creating track bar for min and max for hue, saturation and value
  171.   # You can adjust the defaults as you like
  172.   # cv2.createTrackbar('hmin', 'HueComp',3,179,nothing)
  173.   # cv2.createTrackbar('hmax', 'HueComp',28,179,nothing)
  174.  
  175.   # cv2.createTrackbar('smin', 'SatComp',77,255,nothing)
  176.   # cv2.createTrackbar('smax', 'SatComp',255,255,nothing)
  177.  
  178.   # cv2.createTrackbar('vmin', 'ValComp',186,255,nothing)
  179.   # cv2.createTrackbar('vmax', 'ValComp',255,255,nothing)
  180.   print ("windows created")
  181.  
  182.   #run()
  183.   ###################################################################
  184.   # spin() simply keeps python from exiting until this node is stopped
  185.   rospy.spin()
  186.  
  187. if __name__ == '__main__':
  188.   rospy.init_node('image_converter', anonymous=True)
  189.   image_process()
Add Comment
Please, Sign In to add comment