title: ROS CvBridge on gray image not working because of channel information
body: <h1>What I want</h1>
  <p>I wish to resize a gray image and publish this so I can use this in another ROS node. However I encounter a problem with the channel information, which is either OpenCV or CvBridge.</p>
  <h2>Error</h2>
  <p>When listerning to a camera(webcam/kinect) and converting this to 'mono8' (gray) you get the following information(row, column, channels) in which channels = 1. For some reason if you save this image and read it again suddenly channels = 3. Why is this important? If you use cv2.resize(image,x,y) on an image with 3 channels the output image is (x,y,channels=3), however when there is only 1 channel, this information is lost and your output is (x,y). The problem with this is that CvBridge won't work without channel information.</p>
  <p>The following code works, because cv2.resize is performed on 3 channels:</p>
  <pre><code>#!/usr/bin/env python
  PKG = 'something'
  import roslib; roslib.load_manifest(PKG)
  import rospy
  import cv2
  import sys
  from sensor_msgs.msg import Image
  from cv_bridge import CvBridge, CvBridgeError
  
  class Test:
  
      def __init__(self):
          self.image_sub = rospy.Subscriber(&quot;/camera/rgb/image_raw&quot;,Image, self.callback)
          self.image_sub = rospy.Subscriber(&quot;test_image&quot;,Image, self.callback2)
          self.image_pub = rospy.Publisher(&quot;test_image&quot;, Image)
          self.bridge = CvBridge()
  
      def callback(self, image):
          try:
              cv_image = self.bridge.imgmsg_to_cv2(image, 'mono8')
          except CvBridgeError, e:
              print e
          
          print cv_image.shape ### output: (480, 640, 1)
          cv2.imshow(&quot;Test&quot;, cv_image)
          cv2.imwrite(&quot;Test.png&quot;, cv_image)
          cv2.waitKey(3)
  
          test2 = cv2.imread(&quot;Test.png&quot;)
          print test2.shape ### output: (480, 640, 3)
          cv2.imshow(&quot;Test 2&quot;,test2)
          cv2.waitKey(3)
          test2 = cv2.resize(test2,(250,240))
          print test2.shape ### output: (250, 240, 3)
          self.image_pub.publish(self.bridge.cv2_to_imgmsg(test2))
  
      def callback2(self, image):
          try:
              cv_image = self.bridge.imgmsg_to_cv2(image)
          except CvBridgeError, e:
              print e
          
          cv2.imshow(&quot;Test3&quot;, cv_image)
          cv2.waitKey(3)
  
  def main(args):
      test = Test()
      rospy.init_node('image_converter', anonymous=True)
  
      try:
          rospy.spin()
      except KeyboardInterrupt:
          print &quot;Shutting down&quot;
      cv2.destroyAllWindows()
  
  
  if __name__ == '__main__':
      main(sys.argv)
  </code></pre>
  <p>However the following doesn't work (this time trying to publish the resize):</p>
  <pre><code>    print cv_image.shape ### output: (480, 640, 1)
      cv2.imshow(&quot;Test&quot;, cv_image)
      cv2.imwrite(&quot;Test.png&quot;, cv_image)
      cv2.waitKey(3)
      
      test2 = cv2.resize(test2,(250,240))
      print test2.shape ### output: (250, 240)
      self.image_pub.publish(self.bridge.cv2_to_imgmsg(test2)) ### ERROR
  
      test2 = cv2.imread(&quot;Test.png&quot;)
      print test2.shape
      cv2.imshow(&quot;Test 2&quot;,test2)
      cv2.waitKey(3)
  </code></pre>
  <h2>Other Error</h2>
  <p>Changing 'mono8' to '8UC3' gives the following error: [yuv422] is a color format but [8UC3] is not so they must have the same OpenCV type, CV_8UC3, CV16UC1</p>
  <h1>My real question</h1>
  <p>How can I resize a gray image and publish this in ROS without either losing channel information or converting it somehow to 3 channels? My only concern is that I can send the resized information, the number of channels is not important to me.</p>
  <h2>Information</h2>
  <p>Ubuntu 12.04,
  ROS Hydro,
  OpenCV 2.4.9</p>