支持compressed image消息的kalibr,错误TypeError: Conversion is only valid for arrays with 1 or 2 dimensions

kalibr是标定camera和imu外参的重要工具,但是原配的只支持ros的image消息。这里通过修改源代码使其支持compressed image消息。

  1. 搜索文件:ImageDatasetReader.py
  2. 修改函数def getImage为:
  3.     def getImage(self,idx):
            
            topic, data, stamp = self.bag._read_message(self.index[idx].position)
            ts = acv.Time( data.header.stamp.secs, data.header.stamp.nsecs )
            if data._type == 'mv_cameras/ImageSnappyMsg':
                if self.uncompress is None:
                    from snappy import uncompress
                    self.uncompress = uncompress
                img_data = np.reshape(self.uncompress(np.fromstring(data.data, dtype='uint8')),(data.height,data.width), order="C")
            elif data._type == 'sensor_msgs/CompressedImage':
                np_arr = np.fromstring(data.data, np.uint8)
                img_data_rgb = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
                img_data = cv2.cvtColor(img_data_rgb,cv2.COLOR_BGR2GRAY)
            else:
                img_data = np.array(self.CVB.imgmsg_to_cv2(data, "mono8"))
            return (ts, img_data)

     

  4. 如果出现这个错误:TypeError: Conversion is only valid for arrays with 1 or 2 dimensions. Argument has 3 dimensions

  5. 修改上面代码为:

        def getImage(self,idx):
            
            topic, data, stamp = self.bag._read_message(self.index[idx].position)
            ts = acv.Time( data.header.stamp.secs, data.header.stamp.nsecs )
            if data._type == 'mv_cameras/ImageSnappyMsg':
                if self.uncompress is None:
                    from snappy import uncompress
                    self.uncompress = uncompress
                img_data = np.reshape(self.uncompress(np.fromstring(data.data, dtype='uint8')),(data.height,data.width), order="C")
            elif data._type == 'sensor_msgs/CompressedImage':
                np_arr = np.fromstring(data.data, np.uint8)
                img_data_rgb = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
                img_data = cv2.cvtColor(img_data_rgb,cv2.COLOR_BGR2GRAY)
            else:
                img_data = np.array(self.CVB.imgmsg_to_cv2(data, "mono8"))
                img_data=np.squeeze(img_data)
            return (ts, img_data)

你可能感兴趣的:(建图和定位)