imv_view.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 import rospy
4 import math
5 import struct
6 import numpy as np
7 import cv2
8 from sensor_msgs.msg import CompressedImage
9 from raspicam_node.msg import MotionVectors
10 from cv_bridge import CvBridge, CvBridgeError
11 
12 # Motion vectors with SAD values above threshold will be ignored:
13 SAD_THRESHOLD = 650
14 
15 bridge = CvBridge()
16 color_map = None
17 last_imv = None
18 
19 def create_colormap(num_elements):
20  size = ( num_elements, 1, 1 )
21  im = np.zeros(size, dtype=np.uint8)
22 
23  for i in range(0, num_elements):
24  im[i,0,0] = i
25 
26  return cv2.applyColorMap(im, cv2.COLORMAP_JET)
27 
28 def draw_imv(img, imv):
29  height, width, channels = img.shape
30  mbx = int(math.ceil(width/16.0))
31  mby = int(math.ceil(height/16.0))
32 
33  # Draw colored arrow per each macroblock:
34  for j in range(0, imv.mby):
35  for i in range(0, imv.mbx):
36  idx = (i + (imv.mbx + 1) * j)
37  dx = imv.x[idx]
38  dy = imv.y[idx]
39  sad = imv.sad[idx]
40 
41  x = i*16 + 8;
42  y = j*16 + 8;
43 
44  if dx == 0 and dy == 0:
45  continue
46 
47  if sad >= SAD_THRESHOLD:
48  continue
49 
50  color = ( int(color_map[sad,0,0]),
51  int(color_map[sad,0,1]),
52  int(color_map[sad,0,2]) )
53  cv2.arrowedLine(img, (x+dx, y+dy), (x, y), color, 1)
54 
55 def img_callback(msg):
56  try:
57  img = bridge.compressed_imgmsg_to_cv2(msg, 'bgr8')
58  except CvBridgeError as e:
59  print(e)
60  else:
61  if last_imv is not None:
62  draw_imv(img, last_imv)
63  cv2.imshow('raspicam_node motion vectors visualization', img)
64  cv2.waitKey(25)
65 
66 def imv_callback(msg):
67  global last_imv
68  last_imv = msg
69 
70 def main():
71  global color_map
72  color_map = create_colormap(SAD_THRESHOLD)
73 
74  img_topic = '/raspicam_node/image/compressed'
75  imv_topic = '/raspicam_node/motion_vectors'
76 
77  rospy.init_node('imv_view')
78  rospy.Subscriber(img_topic, CompressedImage, img_callback)
79  rospy.Subscriber(imv_topic, MotionVectors, imv_callback)
80  rospy.spin()
81 
82 if __name__ == '__main__':
83  main()
def create_colormap(num_elements)
Definition: imv_view.py:19
def img_callback(msg)
Definition: imv_view.py:55
def draw_imv(img, imv)
Definition: imv_view.py:28
def imv_callback(msg)
Definition: imv_view.py:66
def main()
Definition: imv_view.py:70


raspicam_node
Author(s):
autogenerated on Sun Jul 5 2020 03:45:18