"cv2.imdecode(numpyArray, cv2.CV_LOAD_IMAGE_COLOR)"返回无 [英] "cv2.imdecode(numpyArray, cv2.CV_LOAD_IMAGE_COLOR)" returns None

查看:65
本文介绍了"cv2.imdecode(numpyArray, cv2.CV_LOAD_IMAGE_COLOR)"返回无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像转换为 Opencv(转换为 numpy 数组)并使用该数组通过 ROS 节点发布消息.我尝试通过以下代码做同样的事情

I'm trying to convert an image into Opencv (into numpy array) and use the array to publish the message over a ROS node. I tried doing the same through the following code

    fig.canvas.draw()
    nparr = np.fromstring ( fig.canvas.tostring_argb(), np.uint8 )
    print nparr
    img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
    print img_np
    image_message = bridge.cv2_to_imgmsg(img_np, encoding="passthrough")
    pub.publish(image_message)

但是,当我尝试这样做时,我收到一条错误消息

But, when I tried doing this I get an error message

AttributeError: 'NoneType' object has no attribute 'shape'

因此,我尝试打印值为 [255 191 191 ..., 191 191 191] 的 numpy 数组的值.我不明白的是 img_np 值是 None.我不知道我哪里错了.任何帮助表示赞赏.

So, I tried printing the values of both the numpy array whose values were [255 191 191 ..., 191 191 191]. And what i didn't understand is img_np value was None. I don't know where I went wrong. Any help is appreciated.

推荐答案

我最近也遇到过类似的问题.

I have encountered similar problems recently.

np.fromstring() 方法从参数字符串返回一个 1-D np.array,不管原始资源是什么.要将 np.array 用作 OpenCV 中的图像数组,您可能需要根据图像的宽度和高度对其进行整形.

The np.fromstring() method returns an 1-D np.array from the parameter string, regardless of the original resource. To use the np.array as an image array in OpenCV, you may need to reshape it according to the image width and height.

试试这个:

img_str = np.fromstring ( fig.canvas.tostring_argb(), np.uint8 )
ncols, nrows = fig.canvas.get_width_height()
nparr = np.fromstring(img_str, dtype=np.uint8).reshape(nrows, ncols, 3)
img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)

这篇关于"cv2.imdecode(numpyArray, cv2.CV_LOAD_IMAGE_COLOR)"返回无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆