Camera provides GRAYSCALE data while deep learning model requires BGR. Is it possible to create artificial bgr image by "stacking" the grayscale image using opencv or numpy or anything?
I am using two ROS packages created by other people. I have tried the following:
Attempted altering the model to accept grayscale as described by the original repo.
Attempted to convert grayscale to bgr using
np_image = cv2.cvtColor(np_image, cv2.COLOR_GRAY2BGR)
Attempted to create an array consisting of the image times three
np_image = np.array([np_image, np_image, np_image])
The following code retrieves the 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()
I expect the model to retrieve the data, evaluate it and return an object id and mask. However, I get the following error:
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
I followed the trace as best I could but can't find the point where the float is introduced.
merged_image = cv2.merge((np_image, np_image, np_image))
should give you what you're looking for.