コード例 #1
0
ファイル: bag_utils.py プロジェクト: subokita/pybot
def compressed_imgmsg_to_cv2(cmprs_img_msg, desired_encoding="passthrough"):
    """
    Convert a sensor_msgs::CompressedImage message to an OpenCV :cpp:type:`cv::Mat`.

    :param cmprs_img_msg:   A :cpp:type:`sensor_msgs::CompressedImage` message
    :param desired_encoding:  The encoding of the image data, one of the following strings:

       * ``"passthrough"``
       * one of the standard strings in sensor_msgs/image_encodings.h

    :rtype: :cpp:type:`cv::Mat`
    :raises CvBridgeError: when conversion is not possible.

    If desired_encoding is ``"passthrough"``, then the returned image has the same format as img_msg.
    Otherwise desired_encoding must be one of the standard image encodings

    This function returns an OpenCV :cpp:type:`cv::Mat` message on success, or raises :exc:`cv_bridge.CvBridgeError` on failure.

    If the image only has one channel, the shape has size 2 (width and height)
    """
    str_msg = cmprs_img_msg.data
    buf = np.ndarray(shape=(1, len(str_msg)),
                     dtype=np.uint8,
                     buffer=cmprs_img_msg.data)
    im = cv2.imdecode(buf, cv2.IMREAD_ANYCOLOR)

    if desired_encoding == "passthrough":
        return im

    try:
        res = cvtColor2(im, "bgr8", desired_encoding)
    except RuntimeError as e:
        raise CvBridgeError(e)

    return res
コード例 #2
0
def cv2_to_imgmsg(self, cvim, encoding = "passthrough"):

    if not isinstance(cvim, (np.ndarray, np.generic)):
        raise TypeError('Your input type is not a numpy array')
    img_msg = sensor_msgs.msg.Image()
    img_msg.height = cvim.shape[0]
    img_msg.width = cvim.shape[1]
    print("here2")
    if len(cvim.shape) < 3:
        cv_type = self.dtype_with_channels_to_cvtype2(cvim.dtype, 1)
    else:
        cv_type = self.dtype_with_channels_to_cvtype2(cvim.dtype, cvim.shape[2])
    if encoding == "passthrough":
        img_msg.encoding = cv_type
    else:
        img_msg.encoding = encoding
        # Verify that the supplied encoding is compatible with the type of the OpenCV image
        if self.cvtype_to_name[self.encoding_to_cvtype2(encoding)] != cv_type:
            raise CvBridgeError("encoding specified as %s, but image has incompatible type %s" % (encoding, cv_type))
    print("here3")
    if cvim.dtype.byteorder == '>':
        img_msg.is_bigendian = True
    img_msg.data = cvim.tostring()
    img_msg.step = len(img_msg.data) // img_msg.height

    return img_msg
コード例 #3
0
 def convert_image(self,image):
     # Use cv_bridge() to convert the ROS image to OpenCV format
     try:
         cv_image = self.bridge.imgmsg_to_cv2(image, "bgr8")
         return np.array(cv_image, dtype=np.uint8)
     except CvBridgeError(e):
         print(e)
コード例 #4
0
    def process(self, args, images):
        dir = Path(args.dir)
        image_bridge = CvBridge()
        idx = 0

        dir.mkdir(parents=True, exist_ok=True)
        for topic, img_msg, t in images:
            img = image_bridge.imgmsg_to_cv2(img_msg)
            if args.encoding != 'passthrough':
                if img_msg.encoding.startswith('bayer_') and args.demosaicing:
                    # use custom debayering
                    # cvtColor2 unfortunately doesn't expose a demosaicing algorithm parameter
                    conv_code = conversion_code(
                        img_msg.encoding, args.encoding, args.demosaicing)
                    img = cv.cvtColor(img, conv_code)
                else:
                    try:
                        img = cvtColor2(img, img_msg.encoding, args.encoding)
                    except RuntimeError as e:
                        raise CvBridgeError(e)

            tpc_path = topic.lstrip('/').replace('/', '_')
            filename = args.name.replace('%tpc', tpc_path)
            filename = filename.replace('%t', str(t))
            filename = filename.replace('%i', str(idx).zfill(8))
            img_path = dir / filename
            cv.imwrite(str(img_path), img)
            idx += 1
コード例 #5
0
def collect_raw_observation(rate=0.5,
                            output_path='data/sample',
                            start_index=0,
                            n_of_samples=10):

    rospy.init_node(NODE_NAME)
    rospy.wait_for_service('current_observation')
    get_current_observation = rospy.ServiceProxy('current_observation',
                                                 CurrentObservation)
    bridge = CvBridge()
    rate = rospy.Rate(rate)
    saving_dir = os.path.join(output_path)
    if not os.path.exists(saving_dir):
        os.mkdir(saving_dir)

    index = start_index
    while True:
        response = get_current_observation()
        try:
            cv2_img = bridge.imgmsg_to_cv2(response.img, "bgr8")
        except CvBridgeError(e):
            print(e)
        else:
            cv2.imwrite(
                os.path.join(saving_dir, 'obs_' + str(index) + '.jpeg'),
                cv2_img)
            print('Done dumping %dth observation' % (index))
        index += 1
        rate.sleep()
コード例 #6
0
    def raw_to_image(self, data):
        header = data.header
        data = np.array(data.data,
                        dtype=np.uint16).reshape(data.height, data.width)
        minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(data)

        img = self.raw_to_8bit(data)

        if self.display_min_max:
            self.display_temperature(img, minVal, minLoc, (255, 0, 0))
            self.display_temperature(img, maxVal, maxLoc, (0, 0, 255))

        try:
            img_msg = self.bridge.cv2_to_imgmsg(img, 'bgr8')
            img_msg.header = header
            self.pub_image.publish(img_msg)
        except CvBridgeError() as e:
            rospy.logerr(e)
コード例 #7
0
def encoding_as_cvtype(encoding):
    from cv_bridge.boost.cv_bridge_boost import getCvType
    try:
        return getCvType(encoding)
    except RuntimeError as e:
        raise CvBridgeError(e)