python - ROS CvBridge on gray image not working because of channel information -


what do i want

I want to resize a gray image and publish it so that I can use it another ROS node, though I have a problem in channel information, which is OpenCV or CVBridge.

Error

When Learner is on a camera (Webcam / Connect) and converts it to 'Mono 8' Gray) You get the following information (row, column, Channel) in which channels = 1. For some reasons if you save this image and read it again, then suddenly the channel 3. Why is it important? If you use cv2.resize (image, x, y) on an image with 3 channels (x, y, channel = 3), but only having 1 channel, this information is lost and your output (x, why). The problem with this is that the CV Bridge channel will not work without the information.

The following code works because cv2.resize is done on 3 channels:

  #! / Usr / bin / env dragon pkg = 'import' some roslib; Def __init __ (self): cv_bridge import CvBridge, CvBridgeError class Test sensor_msgs.msg import image roslib.load_manifest (PKG) import rospy import CV2 import sys self.image_sub = rospy.Subscriber ( "/ camera / RGB / image_raw", image, self.callback) self.image_sub = rospy.Subscriber ( "test_image", image, self.callback2) self.image_pub = rospy.Publisher ( "test_image", image) self.bridge = CvBridge () def Callback (self, image): try: cv_image = self.bridge.imgmsg_to_cv2 (except the image, 'mono8') CvBridgeError, e: print e print cv_image.shape ### production: (480, 640, 1) cv2.imshow ( "Test ", Cv_image) cv2.imwrite (" Test.png ", cv_image) cv2.waitKey (3) test2 = cv2.imread (" Test.png ") print test Received 2ksap ### Output: (480, 640, 3) cv2. Imshow ( "Test 2" Test 2) cv2.waitKey (3) test2 = cv2.resize (test2, (250,240)) print2.shape ### Output: (250, 2) 40, 3) self.image_pub.publish (self.bridge.cv2_to_imgmsg (test2)) def callback2 (self-image): try: cv_image = self.bridge.imgmsg_to_cv2 CvBridgeError (image), excluding e: print e cv2.imshow ( "test3", cv_image) cv2 .waitKey (3) def main (args): test = test () rospy.init_node ( 'image_converter', anonymous = Try true): rospy.spin () except KeyboardInterrupt: printing "shutdown" Cv2. DestroyAllWindows () if __name__ == '__main__': main (sys.argv)  

Although the following does not work (this time size Is trying to publish):

  print cv_image.shape ### Output: (480, 640, 1) cv2.imshow ("test", cv_image) cv2.imwrite ( "Test.png", cv_image) cv2.waitKey (3) test2 = cv2.resize (test2, (250,240)) print test2.shape ### Product: (250, 240) self.image_pub.publish (self.bridge. Cv2_to_imgmsg (test2)) ### Error test2 = CV2. Imread ("Test.png") Print2.shape cv2.imshow ("Test 2", Exam 2) cv2.waitKey (3)  

Other error

Change from 'mono8' to '8UC3' gives the following error: [yuv422] is a color format but [8UC3] is not so they have the same OpenCV type, CV_8UC3, CV16UC1

my real question < / H1>

How can I change the size of a gray image and publish it in ROS without losing channel information or changing it in any way in 3 channels? My only concern is that I can send the resized information, the number of channels is not important to me.

Notice

Ubuntu 12.04, ROS Hydro, OpenCV 2.4.9

For those who are not in the main branch:

  # Gray channel 3 hack only For CVBridge (old version) # Save image cv2.imwrite (self.dir_image_save + 'tempface.png', Cv2_image) # load cv2_image = cv2.imread (self.dir_image_save + 'tempface.png')  

Now you have got a 3 channel gray image (but ugly).


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -