【问题标题】:ROS CvBridge on gray image not working because of channel information由于通道信息,灰色图像上的 ROS CvBridge 无法正常工作
【发布时间】:2014-10-28 13:58:01
【问题描述】:

我想要什么

我希望调整灰度图像的大小并发布它,以便我可以在另一个 ROS 节点中使用它。但是我遇到了通道信息的问题,它是 OpenCV 或 CvBridge。

错误

当收听摄像头(网络摄像头/kinect)并将其转换为“mono8”(灰色)时,您将获得以下信息(行、列、通道),其中通道 = 1。出于某种原因,如果您保存此图像并突然再读一遍channels = 3。为什么这很重要?如果您在具有 3 个通道的图像上使用 cv2.resize(image,x,y),则输出图像为 (x,y,channels=3),但是当只有 1 个通道时,此信息将丢失并且您的输出为 ( x,y)。这样做的问题是,如果没有频道信息,CvBridge 将无法工作。

以下代码有效,因为 cv2.resize 在 3 个通道上执行:

#!/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("/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(image, 'mono8')
        except CvBridgeError, e:
            print e
        
        print cv_image.shape ### output: (480, 640, 1)
        cv2.imshow("Test", cv_image)
        cv2.imwrite("Test.png", cv_image)
        cv2.waitKey(3)

        test2 = cv2.imread("Test.png")
        print test2.shape ### output: (480, 640, 3)
        cv2.imshow("Test 2",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("Test3", cv_image)
        cv2.waitKey(3)

def main(args):
    test = Test()
    rospy.init_node('image_converter', anonymous=True)

    try:
        rospy.spin()
    except KeyboardInterrupt:
        print "Shutting down"
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main(sys.argv)

但是以下方法不起作用(这次尝试发布调整大小):

    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 ### output: (250, 240)
    self.image_pub.publish(self.bridge.cv2_to_imgmsg(test2)) ### ERROR

    test2 = cv2.imread("Test.png")
    print test2.shape
    cv2.imshow("Test 2",test2)
    cv2.waitKey(3)

其他错误

将“mono8”更改为“8UC3”会出现以下错误:[yuv422] 是一种颜色格式,但 [8UC3] 不是,因此它们必须具有相同的 OpenCV 类型、CV_8UC3、CV16UC1

我真正的问题

如何调整灰度图像的大小并将其发布到 ROS 中,而不会丢失通道信息或以某种方式将其转换为 3 个通道?我唯一担心的是我可以发送调整大小的信息,频道数量对我来说并不重要。

信息

Ubuntu 12.04, ROS水电, OpenCV 2.4.9

【问题讨论】:

  • 在你说行不通的代码里,你不是先不看就发布test2吗? test2 在您发布时似乎为空。
  • 我应该更清楚我的例子,但这个问题大部分已经在这里解决了:link

标签: python image opencv ros


【解决方案1】:

在 CvBridge 的 master 分支中,现在修复了您可以在没有频道信息的情况下发送图像:https://github.com/ros-perception/vision_opencv/issues/49

对于不在master分支上的人:

#Ugly gray 3 channel hack 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')

现在你得到了一个 3 通道的灰度图像(但很丑)。

【讨论】:

    猜你喜欢
    • 2012-04-17
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多