def cv2_to_imgmsg(self, img_bgr, encoding): bridge = cv_bridge.CvBridge() # resolve encoding if getCvType(encoding) == 0: # mono8 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) elif getCvType(encoding) == 2: # 16UC1 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) img = img / 255 * (2 ** 16) img = img.astype(np.uint16) elif getCvType(encoding) == 5: # 32FC1 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) img /= 255 elif getCvType(encoding) == 16: # 8UC3 if encoding in ('rgb8', 'rgb16'): # RGB img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) else: img = img_bgr else: rospy.logerr('unsupported encoding: {0}'.format(encoding)) return return bridge.cv2_to_imgmsg(img, encoding=encoding)
def cv2_to_imgmsg(self, img_bgr, encoding): bridge = cv_bridge.CvBridge() # resolve encoding if getCvType(encoding) == 0: # mono8 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) elif getCvType(encoding) == 2: # 16UC1 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) img = img / 255 * (2**16) img = img.astype(np.uint16) elif getCvType(encoding) == 5: # 32FC1 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) img /= 255 elif getCvType(encoding) == 16: # 8UC3 if encoding in ('rgb8', 'rgb16'): # RGB img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) else: img = img_bgr else: rospy.logerr('unsupported encoding: {0}'.format(encoding)) return return bridge.cv2_to_imgmsg(img, encoding=encoding)
def encoding_to_cvtype2(self, encoding): from cv_bridge.boost.cv_bridge_boost import getCvType try: return getCvType(encoding) except RuntimeError as e: raise CvBridgeError(e)
def publish(self): now = rospy.Time.now() bridge = cv_bridge.CvBridge() img_bgr = cv2.imread(self.file_name) if img_bgr is None: jsk_logwarn('cannot read the image at {}' .format(self.file_name)) return # resolve encoding encoding = rospy.get_param('~encoding', 'bgr8') if getCvType(encoding) == 0: # mono8 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) elif getCvType(encoding) == 2: # 16UC1 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) img = img / 255 * (2 ** 16) img = img.astype(np.uint16) elif getCvType(encoding) == 5: # 32FC1 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) img /= 255 elif getCvType(encoding) == 16: # 8UC3 if encoding in ('rgb8', 'rgb16'): # RGB img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) else: img = img_bgr else: jsk_logerr('unsupported encoding: {0}'.format(encoding)) return # setup ros message and publish imgmsg = bridge.cv2_to_imgmsg(img, encoding=encoding) imgmsg.header.stamp = now imgmsg.header.frame_id = self.frame_id self.pub.publish(imgmsg) if self.publish_info: info = CameraInfo() info.header.stamp = now info.header.frame_id = self.frame_id info.width = imgmsg.width info.height = imgmsg.height self.pub_info.publish(info)
def publish(self): now = rospy.Time.now() bridge = cv_bridge.CvBridge() img_bgr = cv2.imread(self.file_name) if img_bgr is None: jsk_logwarn('cannot read the image at {}'.format(self.file_name)) return # resolve encoding encoding = rospy.get_param('~encoding', 'bgr8') if getCvType(encoding) == 0: # mono8 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) elif getCvType(encoding) == 2: # 16UC1 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) img = img / 255 * (2**16) img = img.astype(np.uint16) elif getCvType(encoding) == 5: # 32FC1 img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) img /= 255 elif getCvType(encoding) == 16: # 8UC3 if encoding in ('rgb8', 'rgb16'): # RGB img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) else: img = img_bgr else: jsk_logerr('unsupported encoding: {0}'.format(encoding)) return # setup ros message and publish imgmsg = bridge.cv2_to_imgmsg(img, encoding=encoding) imgmsg.header.stamp = now imgmsg.header.frame_id = self.frame_id self.pub.publish(imgmsg) if self.publish_info: info = CameraInfo() info.header.stamp = now info.header.frame_id = self.frame_id info.width = imgmsg.width info.height = imgmsg.height self.pub_info.publish(info)
def encoding_as_cvtype2(self, encoding): from cv_bridge.boost.cv_bridge_boost import getCvType try: res = getCvType(encoding) except RuntimeError as e: raise CvBridgeError(e) import re vals = re.split('(.+)C(.+)', self.cvtype_to_name[res]) cvtype = vals[1] n_channels = vals[2] return self.numpy_type_to_cvtype[cvtype], eval(n_channels)
def cv2_to_imgmsg(self, img, encoding): bridge = cv_bridge.CvBridge() # resolve encoding if getCvType(encoding) in [cv2.CV_8UC1, cv2.CV_16UC1, cv2.CV_32FC1]: # mono8 if len(img.shape) == 3: if img.shape[2] == 4: code = cv2.COLOR_BGRA2GRAY else: code = cv2.COLOR_BGR2GRAY img = cv2.cvtColor(img, code) if getCvType(encoding) == cv2.CV_16UC1: # 16UC1 img = img.astype(np.float32) img = img / 255 * (2 ** 16) img = img.astype(np.uint16) elif getCvType(encoding) == cv2.CV_32FC1: # 32FC1 img = img.astype(np.float32) img /= 255 elif getCvType(encoding) == cv2.CV_8UC3 and len(img.shape) == 3: # 8UC3 # BGRA, BGR -> BGR img = img[:, :, :3] # BGR -> RGB if encoding in ('rgb8', 'rgb16'): img = img[:, :, ::-1] elif (getCvType(encoding) == cv2.CV_8UC4 and len(img.shape) == 3 and img.shape[2] == 4): # 8UC4 if encoding in ('rgba8', 'rgba16'): # BGRA -> RGBA img = img[:, :, [2, 1, 0, 3]] else: rospy.logerr('unsupported encoding: {0}'.format(encoding)) return return bridge.cv2_to_imgmsg(img, encoding=encoding)
def _cb_dyn_reconfig(self, config, level): file_name = config['file_name'] config['file_name'] = os.path.abspath(file_name) img = cv2.imread(file_name, cv2.IMREAD_UNCHANGED) # when file is gray scale but encoding is not grayscale, # load the image again in color if (len(img.shape) == 2 and getCvType(self.encoding) not in [ cv2.CV_8UC1, cv2.CV_16UC1, cv2.CV_32FC1]): img = cv2.imread(file_name, cv2.IMREAD_COLOR) if img is None: rospy.logwarn('Could not read image file: {}'.format(file_name)) with self.lock: self.imgmsg = None else: rospy.loginfo('Read the image file: {}'.format(file_name)) with self.lock: self.imgmsg = self.cv2_to_imgmsg(img, self.encoding) return config
def my_function(img): # image=pickle.loads(img) a=getCvType("8UC3") img = CvBridge().imgmsg_to_cv2(img,desired_encoding="bgr8") a=1 return a