使用 GRAYSCALE 图像创建 BGR 图像

Creating BGR image using GRAYSCALE image

相机提供 GRAYSCALE 数据,而深度学习模型需要 BGR。是否可以使用 opencv 或 numpy 或任何东西通过 "stacking" 灰度图像创建人工 bgr 图像?

我正在使用其他人创建的两个 ROS 包。 我尝试了以下方法:

已尝试更改模型以接受原始存储库中描述的灰度。

尝试使用

将灰度转换为 bgr
np_image = cv2.cvtColor(np_image, cv2.COLOR_GRAY2BGR)

试图创建一个包含图像乘以三的数组

np_image = np.array([np_image, np_image, np_image])

以下代码检索图像

    def run(self):
        self._result_pub = rospy.Publisher('~result', Result, queue_size=1)
        vis_pub = rospy.Publisher('~visualization', Image, queue_size=1)
        sub = rospy.Subscriber('~input', Image,
                               self._image_callback, queue_size=1) 

        rate = rospy.Rate(self._publish_rate)
        while not rospy.is_shutdown():
            if self._msg_lock.acquire(False):
                msg = self._last_msg
                self._last_msg = None
                self._msg_lock.release()
            else:
                rate.sleep()
                continue

            if msg is not None:
                np_image = self._cv_bridge.imgmsg_to_cv2(msg, 
                    desired_encoding='mono8')
                #np_image = cv2.cvtColor(np_image, cv2.COLOR_GRAY2BGR) #(fail)
                np_image = np.array([np_image, np_image, np_image])
                # Run detection
                results = self._model.detect([np_image], verbose=0)
                result = results[0]
                result_msg = self._build_result_msg(msg, result)
                self._result_pub.publish(result_msg)
def _image_callback(self, msg):
        rospy.logdebug("Get an image")
        if self._msg_lock.acquire(False):
            self._last_msg = msg
            self._msg_lock.release()

我希望模型检索数据、对其进行评估以及 return 对象 ID 和掩码。但是,我收到以下错误:

Traceback (most recent call last):
  File "/home/riwo-rack-pc/ROS_Mask_rcnn/src/mask_rcnn_ros/nodes/mask_rcnn_node", line 196, in <module>
    main()
  File "/home/riwo-rack-pc/ROS_Mask_rcnn/src/mask_rcnn_ros/nodes/mask_rcnn_node", line 192, in main
    node.run()
  File "/home/riwo-rack-pc/ROS_Mask_rcnn/src/mask_rcnn_ros/nodes/mask_rcnn_node", line 116, in run
    results = self._model.detect([np_image], verbose=0)
  File "/home/riwo-rack-pc/ROS_Mask_rcnn/src/mask_rcnn_ros/src/mask_rcnn_ros/model.py", line 2333, in detect
    molded_images, image_metas, windows = self.mold_inputs(images)
  File "/home/riwo-rack-pc/ROS_Mask_rcnn/src/mask_rcnn_ros/src/mask_rcnn_ros/model.py", line 2236, in mold_inputs
    padding=self.config.IMAGE_PADDING)
  File "/home/riwo-rack-pc/ROS_Mask_rcnn/src/mask_rcnn_ros/src/mask_rcnn_ros/utils.py", line 409, in resize_image
    image, (round(h * scale), round(w * scale)))
  File "/home/riwo-rack-pc/.local/lib/python2.7/site-packages/scipy/misc/pilutil.py", line 490, in imresize
    imnew = im.resize(size, resample=func[interp])
  File "/home/riwo-rack-pc/.local/lib/python2.7/site-packages/PIL/Image.py", line 1806, in resize
    return self._new(self.im.resize(size, resample, box))
TypeError: integer argument expected, got float

我尽我所能跟踪跟踪,但找不到引入浮动的点。

merged_image = cv2.merge((np_image, np_image, np_image))

应该会给你想要的东西。