11 from std_msgs.msg
import Float32
12 from sensor_msgs.msg
import CompressedImage
13 from cv_bridge
import CvBridge, CvBridgeError
17 return 'unnamed' not in rospy.get_name()
20 rospy_loginfo = rospy.loginfo
25 click.echo(click.style(str(message), fg=
'green'))
26 rospy.loginfo = loginfo
28 rospy_logerr = rospy.logerr
33 click.echo(click.style(str(message), fg=
'red'))
37 img = cv2.imread(filename, -1)
39 rospy.logerr(
"ERROR image file didn't load or doesn't exist: %s" % filename)
45 img: The image to be resized. 46 width_px: The new width of the image in pixels relative to itself. Aspect ratio is not changed. 51 r = float(width_px) / img.shape[1]
52 dimensions = (int(width_px), int(img.shape[0] * r))
53 img = cv2.resize(img, dimensions, interpolation = cv2.INTER_CUBIC)
57 num_rows, num_cols = img.shape[:2]
58 rotation_matrix = cv2.getRotationMatrix2D((int(num_rows/2), int(num_cols/2)),rotation_angle,1)
59 img = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows))
63 """Warp image in 3D so it looks like its pointing into the image""" 64 compass_width = int(img.shape[1])
65 compass_height = int(img.shape[0])
66 square_corners = np.float32([[0,0], [compass_height,0], [compass_height,compass_width],[0,compass_width]])
67 quad_corners = np.float32([[153,0], [429,0], [600,286], [0,286]])
68 h, mask = cv2.findHomography(square_corners, quad_corners)
69 warped_img = cv2.warpPerspective(img, h, (600,286))
73 """Overlay a background image onto a foreground image (with transparency support)""" 76 bg_img[y:y+fg_img.shape[0], x:x+fg_img.shape[1], c] = fg_img[:,:,c] * (fg_img[:,:,3]/255.0) + bg_img[y:y+fg_img.shape[0], x:x+fg_img.shape[1], c] * (1.0 - fg_img[:,:,3]/255.0)
84 self.
img = np.ones((700,1000,3),dtype=np.uint8)*128
86 current_dir = os.path.dirname(os.path.abspath(__file__))
87 compass_filename =
'%s/compass.png' % current_dir
88 scale_filename =
'%s/scale.png' % current_dir
97 self.
img = CvBridge().compressed_imgmsg_to_cv2(data)
106 im_out = np.uint8(self.img.copy())
107 min_image_dimension = np.min([self.img.shape[1],self.img.shape[0]])
114 width_percentage = 0.6
115 width_px = width_percentage*min_image_dimension
122 x = im_out.shape[1] / 2 - compass_img.shape[1] / 2
123 y = im_out.shape[0] - int(compass_img.shape[0]) - 1
127 x = im_out.shape[1] / 2 - scale_img.shape[1] / 2
128 y = im_out.shape[0] / 2
132 scale_factor = (min_image_dimension / 350.0)
134 scale = 0.5 * scale_factor
135 thick = 1 * scale_factor
138 color = tuple(np.ones(im_out.shape[2])*255)
140 loc = (int(x-13*scale_factor),int(y+7*scale_factor))
141 cv2.putText(im_out,
"0", loc, font, scale, color, int(thick), line_type)
143 scale_text_pretty =
"%.1f cm" % self.
scale_text 144 loc = (x+scale_img.shape[1]+5,int(y+7*scale_factor))
145 cv2.putText(im_out, scale_text_pretty, loc, font, scale, color, int(thick), line_type)
155 publisher = rospy.Publisher(
'overlay/compressed', CompressedImage, queue_size=1)
156 rospy.Subscriber(
'camera/compressed', CompressedImage, self.
image_callback, queue_size=1)
161 rospy.Subscriber(
'scale', Float32, self.
scale_callback, queue_size=1)
163 rospy.loginfo(
'Setup complete. Image overlay process has started.')
165 framerate = rospy.Rate(rospy.get_param(
'~framerate',3))
166 while not rospy.is_shutdown():
168 compressed_image = CvBridge().cv2_to_compressed_imgmsg(img)
169 publisher.publish(compressed_image)
174 rospy.init_node(
'overlay_scale_and_compass')
176 imgOverlay.ros_loop()
180 @click.option(
'--input-image', required=
True, help=
'Path to input image file')
181 @click.option(
'--heading', required=
True, type=float, help=
'Current heading relative to north in degrees')
182 @click.option(
'--scale-text', required=
True, type=float, help=
'The value to be displayed on the right of the scale bar in centimeters')
183 @click.option(
'--output-file', default=
'output.png', help=
'Output filename to save result to [default=\'output.png\']')
184 def cli_main(input_image, heading, scale_text, output_file):
187 imgOverlay.heading = heading
188 imgOverlay.scale_text = scale_text
190 img = imgOverlay.overlay_telemetry()
191 cv2.imwrite(output_file,img)
193 if __name__ ==
'__main__':
def heading_callback(self, data)
def rotate_image(img, rotation_angle)
def image_callback(self, data)
def scale_callback(self, data)
def cli_main(input_image, heading, scale_text, output_file)
def overlay_image(fg_img, bg_img, x, y)
def overlay_telemetry(self)
def resize_image(img, width_px)